JUnit is the most popular Java Unit testing framework

We typically work in large projects - some of these projects have more than 2000 source files or sometimes it might be as big as 10000 files with one million lines of code.

Before unit testing, we depend on deploying the entire app and checking if the screens look great. But that’s not very efficient. And it is manual.

Unit Testing focuses on writing automated tests for individual classes and methods.

JUnit is a framework which will help you call a method and check (or assert) whether the output is as expected.

The important thing about automation testing is that these tests can be run with continuous integration - as soon as some code changes.

package com.example.demo.controller;

public class TestController {
	public int sum(int[] numbers) {
		int sum = 0;
		for (int i : numbers) {
			sum += i;
		return sum;

SpringJUnitTestApplicationTests .java
package com.example.demo;

import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;

import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.controller.TestController;

class SpringJUnitTestApplicationTests {

	TestController myMath = new TestController();

	// MyMath.sum
	// 1,2,3 => 6
	public void sum_with3numbers() {
		assertEquals(6,myMath.sum(new int[] { 1, 2, 3 }));
	public void sum_with1number() {
		assertEquals(3, myMath.sum(new int[] { 3 }));


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringJUnitTestApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringJUnitTestApplication.class, args);


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>






How to Run
Right click on project and select Run As  -> JUnit Test

11 views0 comments