Testing against Java EE 6 API

Not sure this will solve your problem but GlassFish Embedded provides a Java EE 6 implementation. Add this to your pom.xml: <project> … <repositories> <repository> <id>glassfish-extras-repository</id> <url>http://download.java.net/maven/glassfish/org/glassfish/extras</url> </repository> </repositories> … <dependencies> <dependency> <groupId>org.glassfish.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>3.0.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> … </dependencies> … </project> It’s important to declare the glassfish-embedded-all artifact before … Read more

Cleanup after all junit tests

I recommend to use org.junit.runner.notification.RunListener, example: public class TestListener extends RunListener { @Override public void testRunStarted(Description description) throws Exception { // Called before any tests have been run. } @Override public void testRunFinished(Result result) throws Exception { // Called when all tests have finished } } Read more directly in JUnit java doc. You can … Read more

How to unit test a Spring MVC controller using @PathVariable?

I’d call what you’re after an integration test based on the terminology in the Spring reference manual. How about doing something like: import static org.springframework.test.web.ModelAndViewAssert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({/* include live config here e.g. “file:web/WEB-INF/application-context.xml”, “file:web/WEB-INF/dispatcher-servlet.xml” */}) public class MyControllerIntegrationTest { @Inject private ApplicationContext applicationContext; private MockHttpServletRequest request; private MockHttpServletResponse response; private HandlerAdapter handlerAdapter; private MyController controller; … Read more

Testing @Scheduled in spring

You can test the actual method execution using the regular JUnit, but to test if the @Scheduled(cron = “0 * * * * *”) you specified is correct you can use: @Test public void testScheduler(){ // to test if a cron expression runs only from Monday to Friday org.springframework.scheduling.support.CronTrigger trigger = new CronTrigger(“0 0 1 … Read more

When is a Test not a Unit-test?

See Michael Feathers’ definition A test is not a unit test if: It talks to the database It communicates across the network It touches the file system It can’t run at the same time as any of your other unit tests You have to do special things to your environment (such as editing config files) … Read more

What’s the difference between Mockito Matchers isA, any, eq, and same?

any() checks absolutely nothing. Since Mockito 2.0, any(T.class) shares isA semantics to mean “any T” or properly “any instance of type T“. This is a change compared to Mockito 1.x, where any(T.class) checked absolutely nothing but saved a cast prior to Java 8: “Any kind object, not necessary of the given class. The class argument … Read more

How to verify static void method has been called with power mockito

If you are mocking the behavior (with something like doNothing()) there should really be no need to call to verify*(). That said, here’s my stab at re-writing your test method: @PrepareForTest({InternalUtils.class}) @RunWith(PowerMockRunner.class) public class InternalServiceTest { //Note the renaming of the test class. public void testProcessOrder() { //Variables InternalService is = new InternalService(); Order order … Read more