Reputation: 659
I'm looking to mock a static method of a support class and in order to do that, I need to mock a method of the class under test using jMockit. IN the example below, I want mock the method canContinue in order to always get inside the if condition. I also wnt to mock the static method and verify everything that happens after that.
public class UnitToTest {
public void execute() {
Foo foo = //
Bar bar = //
if (canContinue(foo, bar)) {
Support.runStaticMethod(f);
// Do other stuff here that I would like to verify
}
}
public boolean canContinue(Foo f, Bar b) {
//Logic which returns boolean
}
}
My test method looks something like this:
@Test
public void testExecuteMethod() {
// I would expect any invocations of the canContinue method to
// always return true for the duration of the test
new NonStrictExpectations(classToTest) {{
invoke(classToTest, "canContinue" , new Foo(), new Bar());
result = true;
}};
// I would assume that all invocations of the static method
// runStaticMethod return true for the duration of the test
new NonStrictExpectations(Support.class) {{
Support.runStaticMethod(new Foo());
result = true;
}};
new UnitToTest().execute();
//Verify change in state after running execute() method
}
What am I doing wrong here? Changing the first expectation for the canContinue method to return false doesnt influence whether the execution of code goes inside the if condition.
Upvotes: 0
Views: 647
Reputation: 16380
You are mocking one instance (classToTest
), and then exercising another (new UnitToTest().execute()
) which is not mocked; that's one thing wrong.
Also, the test shouldn't use invoke(..."canContinue"...)
, since the canContinue
method is public
. But really, this method should not be mocked at all; the test should prepare whatever state is needed so that canContinue(foo, bar)
returns the desired value.
Upvotes: 1