I would like to be able to mock a class used by a webapp in a JUnit test running embedded Tomcat.
public interface Foo {
void bar();
}
@RunWith(JMockit.class)
public class IntegrationTest {
@Test
public void mockingFoo(@Capturing final Foo foo)
throws ServletException, LifecycleException {
new Expectations() {{
foo.bar(); result = new RuntimeException();
}};
final Tomcat tomcat = new Tomcat();
// MyApp will invoke a Foo instance
tomcat.addWebapp("/MyApp", "/path/to/MyApp.war");
tomcat.start();
/*
invoke the webapp (via Selenium, for example)
The call to Foo::bar in the webapp should throw the runtime exception
*/
tomcat.stop();
}
}
What can be done so that the Foo
instances within the webapp are mocked via JMockit?
UPDATE - 20150821
I don't yet know the answer to my question. However, I managed to mock classes in a webapp running within Jetty.