// Import statements and package declarations
public class ClassToTest{private int decrement(int toDecrement) {toDecrement--;return toDecrement;}
// Constructor and the rest of the class
public static class StaticInnerTest extends TestCase{public StaticInnerTest(){super();}
public void testDecrement(){int number = 10;ClassToTest toTest= new ClassToTest();int decremented = toTest.decrement(number);assertEquals(9, decremented);}
public static void main(String[] args) {junit.textui.TestRunner.run(StaticInnerTest.class);}}}
public class ClassToTest {
private final String first = "first";private final List<String> second = new ArrayList<>();...}
我会用这个:
public class ClassToTest {
private final String first;private final List<String> second;
public ClassToTest() {this("first", new ArrayList<>());}
public ClassToTest(final String first, final List<String> second) {this.first = first;this.second = second;}...}
MyClient classUnderTest = PowerMockito.spy(new MyClient());
// Set the expected return valuePowerMockito.doReturn(20).when(classUnderTest, "myPrivateMethod", anyString(), anyInt());// This is very important. Otherwise, it will not workclassUnderTest.myPrivateMethod();
// Setting the private field value as someValue:Whitebox.setInternalState(classUnderTest, "privateField", someValue);
// To get the value of a private fieldMyClass obj = Whitebox.getInternalState(classUnderTest, "foo");assertThat(obj, is(notNull(MyClass.class))); // Or test value
// prod.h: some production code header
// forward declaration is enough// we should not include testing headers into production codeclass FooTest;
class Foo{// that does not affect Foo's functionality// but now we have access to Foo's members from FooTestfriend FooTest;public:Foo();private:bool veryComplicatedPrivateFuncThatReallyRequiresTesting();}
// test.cpp: some test#include <prod.h>
class FooTest{public:void complicatedFisture() {Foo foo;ASSERT_TRUE(foo.veryComplicatedPrivateFuncThatReallyRequiresTesting());}}
int main(int /*argc*/, char* argv[]){FooTest test;test.complicatedFixture(); // and it really works!}
namespace my_namespace {#ifdef UNIT_TESTclass test_class;#endif
class my_class {public:#ifdef UNIT_TESTfriend class test_class;#endifprivate:void fun() { cout << "I am private" << endl; }}}
在单元测试中:
#ifndef UNIT_TEST#define UNIT_TEST#endif
#include "my_class.h"
class my_namespace::test_class {public:void fun() { my_obj.fun(); }private:my_class my_obj;}
void my_unit_test() {test_class test_obj;test_obj.fun(); // here you accessed the private function ;)}
public class ConwaysGameOfLife {
private boolean[][] generationData = new boolean[128][128];
/*** Compute the next generation and return the new state* Also saving the new state in generationData*/public boolean[][] computeNextGeneration() {boolean[][] tempData = new boolean[128][128];
for (int yPos=0; yPos<=generationData.length; yPos++) {for (int xPos=0; xPos<=generationData[yPos].length; xPos++) {int neighbors = countNeighbors(yPos, xPos);tempData[yPos][xPos] = determineCellState(neighbors, yPos, xPos);}}
generationData = tempData;return generationData;}
/*** Counting the neighbors for a cell on given position considering all the edge cases** @return the amount of found neighbors for a cell*/private int countNeighbors(int yPos, int xPos) {}
/*** Determine the cell state depending on the amount of neighbors of a cell and on a current state of the cell** @return the new cell state*/private boolean determineCellState(int neighborsAmount, int yPos, int xPos) {}}