The following TestNG (6.3) test case generates the error "Invalid context for the recording of expectations"
@Listeners({ Initializer.class })
public final class ClassUnderTestTest {
private ClassUnderTest cut;
@SuppressWarnings("unused")
@BeforeMethod
private void initialise() {
cut = new ClassUnderTest();
}
@Test
public void doSomething() {
new Expectations() {
MockedClass tmc;
{
tmc.doMethod("Hello"); result = "Hello";
}
};
String result = cut.doSomething();
assertEquals(result, "Hello");
}
}
The class under test is below.
public class ClassUnderTest {
MockedClass service = new MockedClass();
MockedInterface ifce = new MockedInterfaceImpl();
public String doSomething() {
return (String) service.doMethod("Hello");
}
public String doSomethingElse() {
return (String) ifce.testMethod("Hello again");
}
}
I am making the assumption that because I am using the @Listeners annotation that I do not require the javaagent command line argument. This assumption may be wrong....
Can anyone point out what I have missed?
The JMockit-TestNG Initializer
must run once for the whole test run, so using @Listeners
on individual test classes won't work.
Instead, simply upgrade to JMockit 0.999.11, which works transparently with TestNG 6.2+, without any need to specify a listener or the -javaagent
parameter (unless running on JDK 1.5).