I need to write a test, where-in, the behaviour of a class can be verified, for different behaviours of a Mocked class. To support multiple behaviours for the same method, we need to mock the class multiple times, in the same TestClass.
Is it possible using JMockIt?
Here is an example of what I want. This is the main class to be tested and its test:
public class MyClass {
private Foo fooObj = null;
public setfooObj(Foo obj) {
fooObj = obj;
}
public boolean process() {
// uses fooObj.getName() to process
}
}
public class MyClassTest {
@MockClass(realClass = mypackage.Foo.class)
public static class MockFoo {
@Mock
public static boolean getName() {
return "test";
}
}
@Test
public void validateProcessing() {
MyClass testObj = new MyClass();
assertEquals(false, testObj.process);
}
}
Now, I want to verify testObj.process()
method's behaviour, when MockFoo.getName()
returns different values. For my first assertion, I want to use its value as "test"(as returned by the mocked function), but for the following ones, I want to check with different values.