Skip to content

Java JUnit Tests

JUnit tests are used to test for the result of a Java program. We often use them to check whether the learner has created a method that returns the correct results.

Test Structure

In Author, the Java JUnit Test type has a single template. This template has a lot of boilerplate code in it. Not all of it will be necessary for every test, but the template is a good place to start to understand the structure of a JUnit test.

The template starts with many import statements. While your test might not require every one of these, it should be fine to leave those import statements as is.

The test then defines a class called LETest. Within this class, you will see some variables declared, followed by methods labeled with @Before, @After, and @Test. The declared variables, @Before, and @After methods are rarely used. They can be used to test output to the console, but if you wanted to do that, it would probably make more sense to use a ComponentTest.

Most JUnit tests delete the variables, @Before, and @After methods, and only work with the @Test method. This is the method where you can check whether a learner's method is acting as expected.

There are many JUnit methods that come in handy here. We'll highlight some of those below, but as an example, consider the assertTrue method takes a String followed by a boolean expression. If that boolean expression evaluates to False, then the learner will be shown the given String as an error message. For example.

public void someTest(){
  int expected = 5;
  assertTrue("It looks like your `addOne` method doesn't correctly add one to the value passed to the method", learnerCode.addOne(4) == 5);

In this example, we're asking the learner to write an addOne() method (in the learnerCode class). In the test, we're calling the addOne() method using a specific input, and seeing if the output is what we expect. If that boolean evaluates to False, we know that the learner incorrectly implemented addOne(), and we should throw an error.

If there were any edge cases we wanted to check, we could run multiple tests within the @Test method. For example, if we wanted to make sure that the learner's method worked with negative numbers, we could write another assertTrue line that uses the boolean addOne(-2) == -1.

Example Tests

In this example, we check whether the learner has correctly written the constructor for the SavingsAccount class. This includes checking the value of several different instance variables.

import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.containsString;

public class LETest {
  public void someTest() {
    SavingsAccount a = new SavingsAccount("Alex", 1000);
    assertNotNull("Make sure you give `this.owner` value.", a.owner);
    assertTrue("`this.owner` should be set equal to `owner` in the constructor", a.owner.equals("Alex"));
    assertTrue("`this.balanceDollar` should be set equal to `balanceDollar` in the constructor", a.balanceDollar == 1000);
    assertTrue("`this.balanceEuro` should be set equal to `balanceDollar * 0.85` in the constructor", a.balanceEuro == 850.0);  

In this second example, we're testing whether the getTotal() method works as intended. We test the method with two different test inputs.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class LETest {
  public void firstTest() {
     int[][] testArray = {{17, 13, 19, 22}, {12, 18, 25, 20}, {15, 18, 21, 24}, {19, 23, 23, 22}, {18, 20, 21, 26}};
    int testResult = KoalaCounting.getTotal(testArray);
    int expected = 396;
    assertEquals("When we called the `getTotal()` method using `koalaSpottings` as a parameter, we expected the method to return `396`, but we got `" + testResult +"` instead.",testResult,expected);
  public void secondTest() {
    int[][] testArray = {{1, 2, 3}, {4, 5, 6}};
    int testResult = KoalaCounting.getTotal(testArray);
    int expected = 21;
    assertEquals("When we called the `getTotal()` method using `{{1, 2, 3}, {4, 5, 6}}` as a parameter, we expected the method to return `21`, but we got `" + testResult +"` instead.",testResult,expected);


You have access to the entire JUnit testing library, which gives you many options. We have found that the Assert class gives you many of the tools you might need for testing Java code. Some of the methods that we find most useful are:

  • assertTrue — used in the first example above, assertTrue() takes a boolean and returns the error message if that boolean evaluates to False.
  • assertEquals — used to check if two numbers are equal. You can give this method a third value to check if two values are within a certain range of each other as well. For example, checking to see if two numbers are within .1 of each other.
  • assertArrayEquals — useful to check if all items in two arrays match. This would prevent you from needing to use a loop to look at all items in an array.