Dave's Brain

Browse - Programming Tips - How do I use junit in my code?

Date: 2014oct29
Language: Java
Product: junit

Q.  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); } }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.