Date: 2014oct29
Language: Java
Product: junit
Q. Java: How do I use junit in my code?
A. First you need to install it.
On an a RedHat/Fedora/CentOS box that is:
dnf install junit
Next, add the .jar to your compile classpath. If you are using ant:
<path id="master-classpath">
<fileset dir="/usr/share/java">
...
<include name="junit.jar"/>
</fileset>
</path>
Make your first test code, in a file called FirstTest.java
Add these imports:
import static org.junit.Assert.*;
import org.junit.*;
Here is the remainder of the class:
// Needs to be public
public class FirstTest {
// Needs one constructor that takes no parameters
public FirstTest() {
}
// Use the @Test annotation and call it should*
@Test public void shouldDoSomething() {
// Do some tests...
int i = 3;
// Use the assert* methods
assertEquals(3, i);
assertNotNull(i);
assertTrue(i == 3);
}
}