A very simple JUnit5 test

 A very simple JUnit test would be a case where we are testing a method or a class that has no dependency on any other class.

For example below is a simple calculator class that calculates sum of two numbers.

package com.devnips.mockitojunit5.service;
/**
* A simple calculator class, with no external dependencies.
*/
public class SimpleCalculator {
/**
* Calculate sum of two numbers and return the calculates sum as response.
*
* @param a
* @param b
* @return
*/
public int add(int a, int b) {
return a + b;
}
}

To write a unit test for this method, we simply call the add method and verify the response.

package com.devnips.mockitojunit5.service;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* A simple JUnit test
*/
class SimpleCalculatorTest {
private final SimpleCalculator simpleCalculator = new SimpleCalculator();
@Test
void test_add() {
Assertions.assertEquals(6, simpleCalculator.add(2, 4));
}
}

This test does not require any use of Mockito framework and can be created by using just the JUnit framework.

0 comments:

Post a Comment