To give the answer you asked (using JMockit, partial bullying):
public class MyClassTest { @Tested MyClass myClass; @Test public void test_MyClass_methodA_enters_if_condition() { final CustomObject object1 = new CustomObject("input1"); final CustomObject object2 = new CustomObject("input2"); new NonStrictExpectations(myClass) {{ invoke(myClass, "methodB", object1, object2); result = true; }}; assertEquals("Result", myClass.methodA(object1, object2)); } @Test public void test_MyClass_methodA_skips_if_condition() { final CustomObject object1 = new CustomObject("input1"); final CustomObject object2 = new CustomObject("input2"); new NonStrictExpectations(myClass) {{ invoke(myClass, "methodB", object1, object2); result = false; }}; assertEquals("Different Result", myClass.methodA(object1, object2)); } }
However, I would not recommend doing so. In general, private methods should not be mocked. Instead, make fun of the actual external dependency of your device under test (in this case, CustomObject ):
public class MyTestClass { @Tested MyClass myClass; @Mocked CustomObject object1; @Mocked CustomObject object2; @Test public void test_MyClass_methodA_enters_if_condition() { new NonStrictExpectations() {{ Something thing = new Something(); object1.getSomething(); result = thing; object2.getSomething(); result = thing; }}; assertEquals("Result", myClass.methodA(object1, object2)); } @Test public void test_MyClass_methodA_skips_if_condition() { new NonStrictExpectations() {{ object1.getSomething(); result = new Something(); object2.getSomething(); result = new Something(); }}; assertEquals("Different Result", myClass.methodA(object1, object2)); } }
source share