Master java skills

Spring Boot + Junit5

Create a spring starter project and add below dependencies

pom.xml. Please make sure you use parent version as 2.1.2.RELEASE for this project, in order to avoid any compatibility issues with other dependencies.

Note -> This example is compatible with spring-boot-starter-parent version 2.1.2.RELEASE and may not work with other recent versions of spring boot. Therefore, make sure that you keep your version of spring boot as 2.1.2.RELEASE

<?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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<groupId>com.example</groupId>
	<artifactId>spring-boot-junit</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-junit</name>
	<description>Demo project for Spring Boot</description>


	<properties>
		<!-- <java.version>1.8</java.version> -->
		<junit-jupiter.version>5.3.2</junit-jupiter.version>
		<mockito.version>2.24.0</mockito.version>
	</properties>


	<dependencies>

		<!-- mvc -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- exclude junit 4 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>junit</groupId>
					<artifactId>junit</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- junit 5 -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>${junit-jupiter.version}</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.0</version>
			</plugin>

		</plugins>
	</build>

</project>

Create a java file ExampleServiceImpl.java

package com.javatrainingschool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExampleServiceImpl {

        @Autowired
	private ExampleRepositoryImpl repo;
	
	public String getMessage() {
		return repo.get().toUpperCase();
	}
	
	public int getAddtion(int num1, int num2) {
		return num1 + num2;
	}
}

Create another class ExampleRepositoryImpl

package com.javatrainingschool;

import org.springframework.stereotype.Repository;

@Repository
public class ExampleRepositoryImpl {
	
   public String get() {
        return "Hello from Junit 5";
   }

}

Finally, create a test class for service class

package com.javatrainingschool;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class ExampleServiceTest {

	@Autowired
	private ExampleServiceImpl service;
	
	@Test
	public void testGetMessage() {
		String actualMessage = service.getMessage();
		String expectedMessage = "HELLO FROM JUNIT 5";
		
		assertEquals(expectedMessage, actualMessage);
	}
	
	@Test
	public void testGetAddition() {
		int num1 = 10;
		int num2 = 20;
		
		int actualResult = service.getAddtion(num1, num2);
		
		int expectedResult = 30;
		
		assertEquals(expectedResult, actualResult);
	}
}

Run the test case. It should run successfully as shown below