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…

 

How to add JUnit library into a java project in Eclipse?

1. Right click on the project and select properties. 2. In the properties window, go to Java Build Path -> Libraries 3. Click on “Add Library” 4. In the Add Library window, select JUnit and click Next Button. 5. In the next window select the desired JUnit version. Select JUnit 4 for annotation driven tests […]
Continue reading…