Week 55 — What is JUnit and what is it used for?

Question of the Week #55
What is JUnit and what is it used for?
6 Replies
Eric McIntyre
Eric McIntyre11mo ago
In order to verify a program working correctly, it is possible to write additional programs that test the functionality the program. Since programs often are fairly complex, it can become unreasonable to test a whole program as a black box hence developers can write "unit tests" that test small parts of the program. These unit tests can be compiled together with the program and run specific methods of the said program. JUnit is a framework that simplifies writing unit tests in Java. With JUnit, test methods are annotated with @Test and have access to various methods like assertEquals that check whether results are as expected. For example, a program could contain a method that adds two numbers:
public class SimpleMathOperations{
public int add(int first, int second){
return first + second;
}
}
public class SimpleMathOperations{
public int add(int first, int second){
return first + second;
}
}
Then, one can write a test class with test methods that runs the add method with different parameters and check the results:
class SimpleMathTest{

private SimpleMathOperations op = new SimpleMathOperations();

@Test
void testAddPositiveNumbers(){
assertEquals(2, op.add(1,1));
assertEquals(3, op.add(1,2));
assertEquals(50, op.add(13,37));
}

@Test
void testAddWithZero(){
assertEquals(1, op.add(1,0));
assertEquals(1, op.add(0,1));
assertEquals(0, op.add(0,0));
}

@Test
void testAddNegativeNumber(){
assertEquals(0, op.add(1,-1));
assertEquals(0, op.add(-1,1));
assertEquals(-2, op.add(-1,-1));
assertEquals(-1, op.add(1,-2));
assertEquals(1, op.add(2,-1));
}
}
class SimpleMathTest{

private SimpleMathOperations op = new SimpleMathOperations();

@Test
void testAddPositiveNumbers(){
assertEquals(2, op.add(1,1));
assertEquals(3, op.add(1,2));
assertEquals(50, op.add(13,37));
}

@Test
void testAddWithZero(){
assertEquals(1, op.add(1,0));
assertEquals(1, op.add(0,1));
assertEquals(0, op.add(0,0));
}

@Test
void testAddNegativeNumber(){
assertEquals(0, op.add(1,-1));
assertEquals(0, op.add(-1,1));
assertEquals(-2, op.add(-1,-1));
assertEquals(-1, op.add(1,-2));
assertEquals(1, op.add(2,-1));
}
}
Eric McIntyre
Eric McIntyre11mo ago
IDEs can then run tests and report which tests run successfully and which fail. In the above example, all tests run successfully.
No description
Eric McIntyre
Eric McIntyre11mo ago
However, we can also add failing tests like the following:
@Test
void failingTest() {
assertEquals(0, op.add(1,1));
}
@Test
void failingTest() {
assertEquals(0, op.add(1,1));
}
Running this test will fail and IDEs can report that. When tests fails, developers should investigate the test failures and fix the issues in the code if the fails were caused by a bug in the program.
📖 Sample answer from dan1st
No description
Eric McIntyre
Eric McIntyre11mo ago
JUnit is a testing framework. You can use it to write tests, to see if the code you wrote still does exactly what it's supposed to do. IDEs or build tools can then use the junit engine to run those tests, and optionally export the results to a file for further documentation, or let the build fail if any tests failed. All JUnit test methods have the @Test annotation, to signify they should be executed by the junit engine. If a JUnit test method runs without any exceptions, it is considered successful, and the test passes. If a test method throws an AssertionFailedError, the test is considered failed. If any other exceptions get thrown, the test is also considered failed, but for the reason that the test itself has an error somewhere. JUnit provides the Assertions class, which has utility methods for writing assertions, which fail with an AssertionFailedError. You can use the methods provided by that class to test the output of library functions.
Calculator calc;
@BeforeAll // tells junit to run this method before any other test method in this class
public void init() {
// set up calculator
this.calc = new Calculator();
}
@Test
public void testAddition() {
int result = calc.add(1, 2);
// assert that the result equals 3
// order matters, the exception thrown if the assertion fails can be used by IDEs to give you more detailed info about which assertion failed
// if the order is reversed (eg assertEquals(result, 3)), the IDE might show the result variable as "expected" output, and 3 as "actual" output, which is not what we want
Assertions.assertEquals(3, result);
}
Calculator calc;
@BeforeAll // tells junit to run this method before any other test method in this class
public void init() {
// set up calculator
this.calc = new Calculator();
}
@Test
public void testAddition() {
int result = calc.add(1, 2);
// assert that the result equals 3
// order matters, the exception thrown if the assertion fails can be used by IDEs to give you more detailed info about which assertion failed
// if the order is reversed (eg assertEquals(result, 3)), the IDE might show the result variable as "expected" output, and 3 as "actual" output, which is not what we want
Assertions.assertEquals(3, result);
}
There are also ways of making sure code throws / does not throw a certain exception:
Assertions.assertThrows(RuntimeException.class, () -> throw new RuntimeException("hello world")); // this would pass
Assertions.assertDoesNotThrow(() -> 1/0); // fails, this throws ArithmeticException
Assertions.assertThrows(RuntimeException.class, () -> throw new RuntimeException("hello world")); // this would pass
Assertions.assertDoesNotThrow(() -> 1/0); // fails, this throws ArithmeticException
Eric McIntyre
Eric McIntyre11mo ago
there are a lot of other features in junit that make writing tests easier, and all of those can be read about here: https://junit.org/junit5/docs/current/user-guide/
⭐ Submission from 0x150
Eric McIntyre
Eric McIntyre11mo ago
The JUnit API is one of the Unit testing frameworks for Java programming, means testing a unit of code by writing "TestCases" and Check whether the output matching the expected value using "Assertion methods"
Submission from n.karthi
Want results from more Discord servers?
Add your server