Newer
Older
labs / tiddlers / content / labs / lab02 / _Labs_02_The CalculatorTest Class.md

Let's take a look at the generated code.

Default constructor

You will see a default constructor that looks like:

public CalculatorTest() {
}

We are not going to be using this constructor, so delete it.

@BeforeEach and @AfterEach methods

You will also see two methods named setUp and tearDown. The method names really don't matter --- we tend to use these names through convention since early versions of JUnit required the methods to have these names.

It is the @BeforeEach and @AfterEach annotations above the methods that identify the method to JUnit. As mentioned in lecture 2 it is very important that each test is independent from every other test since we don't want tests to interfere with each other. These two methods will be run before and after each test. We can add set-up code to the @BeforeEach method and clean-up code to the @AfterEach method.

@Test methods

You will see two methods named testAdd and testMultiply. Again, the names don't matter, althought the methods do need to be public and void. For historical reasons we use the 'test' prefix for test methods. It is the @Test annotation that tells JUnit that this method is a test.

You have probably already guessed that the code for testing the calculator's add method should go in the testAdd method, and the code for the multiply method should go in the testMultiply method.