Connecting the JUnit framework

For testing Java code, we have a great framework called JUnit . It works great, it's constantly updated, it's very popular and of course Intellij IDEA is very tightly integrated with it.

Now everyone is using the fifth version of this framework - JUnit 5 , although in many projects you can still find its fourth version. They're not much different, but we'll take a look at the latest one anyway. I think by the time you start actively writing tests, you will approve of my choice.

So, how to add JUnit to the project? After learning Maven it will be easy: just add this code to your pom.xml:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
	<version>5.8.1</version>
	<scope>test</scope>
</dependency>

By the way, if you want Maven to run your tests at the build stage ( test stage ), then you need to add one more fragment to pom.xml:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
	<version>2.19.1</version>
	<dependencies>
    	<dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.3.2</version>
    	</dependency>
	</dependencies>
</plugin>

@Test annotation

Let's say you have a class that you want to test. What is the best way to do this? Let's start with some example, because it's hard to test an abstract class :)

Suppose we have a Calculator class that can perform 4 basic operations: addition, subtraction, multiplication and division. Let's write it:

class Calculator {
    	public int add(int a, int b) {
        	return a+b;
    	}

    	public int sub(int a, int b) {
        	return a-b;
    	}

    	public int mul (int a, int b) {
        	return a*b;
    	}

    	public int div(int a, int b) {
        	return a/b;
    	}
}

We want to test the methods of this class. You never know, in the future they change something and everything will stop working. How can we write tests for it?

We need to create another class. To do this, they usually take the same name and add the Test suffix . For each method, you must add at least one test method. Example:

class CalculatorTest {
   	@Test
    	public void add() {
    	}
   	@Test
    	public void sub() {
        }
   	@Test
    	public void mul() {
    	}
   	@Test
    	public void div() {
    	}
}

There used to be a requirement that the method name start with the word test , but this is no longer required.

JUnit test examples

Let's write some examples in which we will test the methods of our CalculatorTest class :

class CalculatorTest {
   	@Test
    	public void add() {
        	Calculator calc = new Calculator();
        	int result = calc.add(2, 3);
        	assertEquals(5, result);
    	}

   	@Test
    	public void sub() {
        	Calculator calc = new Calculator();
        	int result = calc.sub(10, 10);
        	assertEquals(0, result);
        }

   	@Test
    	public void mul() {
        	Calculator calc = new Calculator();
        	int result = calc.mul(-5, -3);
        	assertEquals(15, result);
    	}

   	@Test
    	public void div() {
        	Calculator calc = new Calculator();
        	int result = calc.div(2, 3);
        	assertEquals(0, result);
    	}
}

This is what a typical JUnit test looks like. From the interesting: the special method assertEquals() is used here : it compares the parameters passed to it, as indicated by the word equals in its name.

If the parameters passed to assertEquals() are not equal, the method will throw an exception and the test will fail. This exception will not prevent other tests from running.