Home  >  Spring 5

@ExtendWith + SpringExtension in Spring 5 Test

By Arvind Rai, January 11, 2019
SpringExtension introduced in Spring 5, is used to integrate Spring TestContext with JUnit 5 Jupiter Test. SpringExtension is used with JUnit 5 Jupiter @ExtendWith annotation as following.
@ExtendWith(SpringExtension.class)
public class MyAppTest {
  -------
} 
Here we will create a Spring Boot application to test Spring SpringExtension class with JUnit @ExtendWith annotation.

Technologies Used

Find the technologies being used in our example.
1. Java 11
2. Spring 5.1.3.RELEASE
3. Spring Boot 2.1.1.RELEASE
4. JUnit 5
5. Maven 3.5.2
6. Eclipse 2018-09

@ExtendWith(SpringExtension.class) Example

pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.concretepage</groupId>
	<artifactId>spring-boot-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>spring-demo</name>
	<description>Spring Boot Demo Project</description>
	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>2.1.1.RELEASE</version>
	</parent>
	<properties>
	    <context.path>spring-app</context.path>
	    <java.version>11</java.version>
	</properties>
	<dependencies>
	    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	    </dependency>
	    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
	    </dependency>	
	    <dependency>
	        <groupId>org.junit.jupiter</groupId>
	        <artifactId>junit-jupiter-api</artifactId>
	        <version>5.3.2</version>
	        <scope>test</scope>
	    </dependency>	
	    <dependency>
	        <groupId>org.junit.jupiter</groupId>
	        <artifactId>junit-jupiter-engine</artifactId>
	        <version>5.3.2</version>
	        <scope>test</scope>
	    </dependency>	
	    <dependency>
	        <groupId>org.junit.jupiter</groupId>
	        <artifactId>junit-jupiter-params</artifactId>
	        <version>5.3.2</version>
	        <scope>test</scope>
	    </dependency>		    
	    <dependency>
	        <groupId>org.junit.platform</groupId>
	        <artifactId>junit-platform-launcher</artifactId>
	        <version>1.3.2</version>
	        <scope>test</scope>
	    </dependency>	    	    		
	</dependencies> 
	<build>
	  <plugins>
	    <plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
	    </plugin>
	  </plugins>
	  <finalName>${context.path}</finalName>
	</build>
</project> 
MyAppTest.java
package com.concretepage;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.concretepage.service.MyService;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MyAppTest {
	@Autowired
	private MyService myService;

	@BeforeAll
	static void initAll() {
	   System.out.println("---Inside initAll---");
	}

	@BeforeEach
	void init(TestInfo testInfo) {
	   System.out.println("Start..." + testInfo.getDisplayName());
	}

	@Autowired
	private TestRestTemplate restTemplate;

	@Test
	public void multiplyNumTest() {
	   int val = myService.multiplyNum(5, 10);
	   assertEquals(50, val);
	}

	@Test
	public void idAvailabilityTest() {
	   boolean val = myService.isIdAvailable(100);
	   assertTrue(val);
	}

	@Test
	public void webAppTest() {
	   String url = "http://localhost:8585/spring-boot/";
	   String body = this.restTemplate.getForObject(url, String.class);
	   assertEquals("Hello World!", body);
	}

	@AfterEach
	void tearDown(TestInfo testInfo) {
	   System.out.println("Finished..." + testInfo.getDisplayName());
	}

	@AfterAll
	static void tearDownAll() {
	   System.out.println("---Inside tearDownAll---");
	}
} 
@SpringBootTest: It is used to run test cases for Spring Boot based application.
Now find other Java files.
MyService.java
package com.concretepage.service;
import org.springframework.stereotype.Component;

@Component
public class MyService {
    public String getMessage() {
    	return "Hello World!";
    }
    public int multiplyNum(int num1, int num2) {
    	return num1 * num2;
    }
    public boolean isIdAvailable(long id) {
        if (id == 100) {
        	return true;
        }
        return false;
    }    
} 
HelloController.java
package com.concretepage.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.concretepage.service.MyService;

@RestController
public class HelloController {
	@Autowired
	private MyService myService;

	@GetMapping("/")
	public String getMessage() {
	   return myService.getMessage();
	}
} 
AppCustomizer.java
package com.concretepage.config;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class AppCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
   @Override
   public void customize(ConfigurableServletWebServerFactory factory) {
	factory.setContextPath("/spring-boot");
	factory.setPort(8585);		
   }
} 
MyApplication.java
package com.concretepage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {
	private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);

	public static void main(String[] args) {
	   SpringApplication.run(MyApplication.class, args);
	   logger.debug("--Application Started--");
	}
} 
Run the MyAppTest class in Eclipse and we will get following output.
@ExtendWith + SpringExtension in Spring 5 Test

References

Spring Doc: SpringExtension
JUnit 5 @BeforeAll and @AfterAll Example

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us