Unit Testing with Junit
Posted in Testing on August 15, 2016 by Adrian Wyssmann ‐ 3 min read
Unit testing refers to testing of individual units of source code or as written in Wikipedia
unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.
Let’s do a simple example. Let’s create our own shell/console in java with expects a simple user input and the does a desired action based on the entered command. Ideally you would follow the TDD approach where you first write your test, implement the productive code, but for simplicity I just write some code and then write the tests for it.
As you can see there is a method parse_input(String input) which we can test. In order to do so you need a unit test framework, which provides you the necessary harness to implement and run units tests for your code. In case of Java it is JUnit. Unit tests are usually written in a separate class and annotated with @Test. The class needs to import the appropriate package org.junit.Test:
To run the tests, a test runner is required. usually your IDE has a native graphical runner built in.
You can also run it on the command line
Calling it from command line may be a bit tricky. First lensure that junit-x.x.jar and hamcrest-core-x.x.jar are in your classpath. Here some common errors and appropriate measures
- ensure
.
is part of your classpath
- for packages, call the package from package base instead of calling the test class from within a package folder