How to Write Unit Tests for a Java Project

Every developer knows that testing your code is an extremely important -- but tedious -- part of the development process. Manually testing your application by executing it can suffice well enough for small or simple projects, but when the project grows to any level of complexity it becomes difficult to catch every execution path, and it becomes tedious to repeat the same testing process over and over. Writing unit tests can definitely cover much of the test process, and it can also point out bugs and design flaws you may not have noticed until it was too late.

Things You'll Need

  • Java SDK
  • JUnit testing framework
  • Java development environment, such as Eclipse or NetBeans
Show More

Instructions

    • 1

      Create a class to test. For now, a simple one will suffice.

      public class TestClass {

      public int addInts(int a, int b) {

      return a + b;

      }

      }

    • 2

      Add a new unit test to your project. In Eclipse, you can do this simply by right clicking on your source folder, selecting "New" from the context menu, and selecting "JUnit Test Case" from the sub-menu. In the dialog, ensure "New JUnit 4 Test" is selected, name the test "TestClassTests" and click "Finish." You may be prompted to add JUnit 4 to your build path; if so, go ahead and add it.

    • 3

      Modify your TestClassTests file to reflect the following:

      import static org.junit.Assert.assertEquals;

      import org.junit.Test;

      public class TestClassTests {

      @Test

      public void ensureAddIntsReturnsCorrectValue() {

      final TestClass tc = new TestClass();

      final int exp = 11;

      final int res = tc.addInts(5, 6);

      assertEquals(exp, res);

      }

      }

      This represents the basic framework of a unit test. You identify a method as your "test" (using the @Test attribute), initialize what you're testing, execute the test, and then validate the results using the assertEquals method. Note the "import static..." line at the top; this allows you to reference the assertEquals method as though it were a static method of the current class, even though it's part of JUnit's Assert class.

    • 4

      Run your test, by either right clicking in your source code, selecting "Run As" from the context menu, then selecting "JUnit Test" from the sub menu; or by holding "Alt" and "Shift" and press "X," then release all and press "T."

Tips & Warnings

  • Unit testing is useful for any developer -- and the tip of the iceberg. Make sure to review the JUnit documentation to see all the different types of assertion methods available, such as "assertTrue" and "assertNotNull", so you can really get the full benefit of your testing framework. Also, look into the other annotations, such as @Before and @After, which execute the annotated method before and after each test run respectively, and @BeforeClass and @AfterClass, which execute before and after the entire test suite (i.e. all the @Test methods in your class) is executed.

Related Searches:

Resources

Comments

Related Ads

Featured