Writing Tests

Write a JUnit test by marking it with @Test, giving it a descriptive name, and laying it out as Arrange, Act, Assert.

Apprentice · Java · Unit Testing · Updated

By

Annotations

JUnit executes unit tests using method annotations. The most common annotation is @Test, which identifies a method as one that the JUnit test runner should execute.

@Test
public void channelUp_should_increaseChannel()
{
    // ...
}

Naming tests

Tests should have meaningful names. In fact, unit test names should be long and descriptive. Test names should describe the scenario that is being tested. A unit test name should read like a sentence. This is because you will have many unit tests. When you run all your unit tests, if one test fails, it is important to have as much information about which test failed as possible.

There are several popular unit test naming conventions. Read more here: https://medium.com/@stefanovskyi/unit-test-naming-conventions-dd9208eadbea

In our examples we will use the methodName_should_expectedBehavior naming convention, for example channelUp_should_increaseChannel.

Unit Test 3A’s

A Unit test should contain 3 distinct sections, each beginning with the letter A: Arrange, Act, Assert.

@Test
public void channelUp_should_increaseChannel()
{
    // arrange

    // act

    // assert

}

Arrange

The Arrange section of your unit test is where you set up the pre-condition of your scenario.

Arranging Scenario 1

@Test
public void channelUp_should_increaseChannel()
{
    // arrange
    Television tv = new Television();
    tv.turnOn();
    int expectedChannel = 4;

    // act

    // assert
}

Act

The Act section of your unit test is where you execute the one method that your scenario is testing.

Acting on Scenario 1

@Test
public void channelUp_should_increaseChannel()
{
    // arrange
    Television tv = new Television();
    tv.turnOn();
    int expectedChannel = 4;

    // act
    int returnedChannel = tv.channelUp();

    // assert
}

Assert

The Assert section of your unit test is where you verify that the action you performed (the method you called) had the effect that you were expecting.

If there is a return value, you verify that the return value is what you expected. But you should verify that any properties in the object were changed the way you expected as well.

Asserting new values for Scenario 1

@Test
public void channelUp_should_increaseChannel()
{
    // arrange
    Television tv = new Television();
    tv.turnOn();
    int expectedChannel = 4;

    // act
    int returnedChannel = tv.channelUp();

    // assert
    assertEquals(expectedChannel, returnedChannel,
            "Because the TV was set to channel 3.");

    int newChannel = tv.getCurrentChannel();
    assertEquals(expectedChannel, newChannel,
            "Because the TV was set to channel 3.");

}

The Assertions class

JUnit 5 provides an Assertions class (import static org.junit.jupiter.api.Assertions.*;) that lets you verify your test's results. Below are a few of its methods.

  • assertEquals(expected, actual): compares the values of two variables to check for equality
  • assertTrue(actual): checks a boolean value for a true value.
  • assertFalse(actual): checks a boolean value for a false value.
  • assertNotNull(object): verifies that an object is not null
  • assertNull(object): verifies that an object is null

JUnit 5 assertions: https://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions

Do it: arrange and act for channelUp

Every test of channelUp() starts the same way: set the TV up (Arrange), then call the one method being tested (Act). Write those two parts once, as a method. The box's tests then do the Assert part: they check the channel your method gives back.

The method gets one value:

  • startChannel: the channel the TV is on before channelUp() is called, such as 14.

The Television class is given below your method. Its channels go from 3 to 15; on the highest channel, channelUp() stays on 15. Don't change the class.

For example, starting on channel 14:

Part What happens The TV afterward
Arrange Make a new Television, turn it on, change to channel 14 On, channel 14
Act Call channelUp() once: it returns 15 On, channel 15

So channelAfterUp(14) returns 15.

Your job: write the method channelAfterUp(int startChannel) so it returns an int: the value channelUp() returns.

  • Arrange: make a new Television, call turnOn(), then call changeChannel(startChannel).
  • Act: call channelUp() once.
  • Return the value that channelUp() returned.
  • Starting on 15, the highest channel, it returns 15.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets