How to execute junit test cases parallely using maven surefire plugin?

Junit test parallel execution can bring down the test execution time considerably. The only condition is that your test cases must be independent. The state shall not be shared across test cases. Today, I shall share a maven project with parallel test execution configured using maven surefire plugin. The complete source code can be found […]
Continue reading…

 

Separate unit and integration testcases using JUnit Category.

This post is about separating Unit and Integration testcases using JUnit category and running those separately using maven. This post in NOT about moving unit and integration testcases into separate test source folders. With the introduction of org.junit.experimental.categories.Category annotation in JUnit 4.x we are in a position to separate integration testcases using annotation. Basically Category […]
Continue reading…

 

JUnit best practices – Use BeforeClass annotation to initialize any resource.

JUnit 4 provides BeforeClass annotation to initialize costly resources for the use of all test cases in the test class. One important thing to remember is that the method marked as BeforeClass would run only once and the resource initialized by it would be shared by all test methods. The method marked as BeforeClass would […]
Continue reading…

 

JUnit Best Practices – Use expected exception instead of catching the exception

Use JUnit expected exception feature to test a unit that might throw an exception in given situation or for a given input. package com.ourownjava.unit.under.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.List; import org.junit.Before; import org.junit.Test; import com.ourownjava.exception.NoAppleException; import com.ourownjava.model.Apple; /** * * @author Sanju Thomas * */ public […]
Continue reading…

 

JUnit Best Practices – Use correct assertion

Always do a strong assertion against the returned state from a unit. Avoid assertion like the collection size shall be greater than zero. A better assertion could be checking the size of the collection and content of the collection. Complete source code can be found here. package com.ourownjava.unit.under.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import […]
Continue reading…