Creating Tests with JUnit

Course Home | Code Example(s)

So as mentioned in the first JUnit lesson, JUnit provides us with the framework to mark code as tests, provides assertion capabilities and a mechanism for running tests. Now aside from assertions, the tests themselves contain little else from JUnit. The majority of the code will be calls to other classes and libraries to help us interact with our application/codebase.

So let’s assume we’ve written some code that we want to turn into a test. The bare minimum we can do it simply add @Test above the method name.

package junitlessons;

import org.junit.Test;

public class A_CreatingTests
{
    //So I typed @Test and pressed enter
    //IntelliJ automatically imported JUnit for me
    //See above it says 'import org.junit.Test;'
    @Test
    public void MultipleNumbers()
    {
        int c = 5 * 6;
    }
}

This block of code will now be recognised as a test by the test runner. We look at running them in a few lessons time.

Next Lesson ➔

Resources

JUnit - Writing Tests

Richard Bradshaw's photo
Author

Richard Bradshaw

@FriendlyTester

Software Tester, speaker and trainer at Friendly Testing. BossBoss at Ministry of Testing. Whiteboard Testing creator. Striving to improve the testing craft.

Comments