I am new to Junit and Jmockit. I wrote an example and want to test that using mocking, but I was stuck with an error.
public class First {
public static int square(int number) {
int result = number * number;
return result;
}
}
public class Second {
public static void main(String[] args) {
int number = 5;
number = new Second().xi(number, number);
System.out.println(number);
}
public int xi(int number, int number1) {
number = First.square(number) + First.square(number1);
return number;
}
}
public class SecondTest {
Second second = new Second();
@Test
public void testXi() {
new Expectations() {
{
First.square(5);
result = 25;
}
};
int mk = second.xi(5, 5);
assertEquals(50, mk);
}
}
When I tried to run this SecondTest.java then i got the following error.
java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter at SecondTest$1.(SecondTest.java:17) at SecondTest.testXi(SecondTest.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:606) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:606) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
I couldn't find any error in the code.