2

I am trying to write an integration for Spring Boot application:

import org.springframework.boot.SpringApplication; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = { "com.lapots.tree.model.web" }) public class TreeModelApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(TreeModelApplication.class, args); } @Bean public ServletRegistrationBean servletRegistrationBean() throws Exception { ServletRegistrationBean registrationBean = new ServletRegistrationBean(new RootServlet(), "/tree-model-app"); registrationBean.setLoadOnStartup(1); registrationBean.setAsyncSupported(true); return registrationBean; } } 

The test looks like this

import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.web.client.RestTemplate; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TreeModelApplication.class) @ExtendWith(SpringExtension.class) public class TreeModelApplicationIntegrationTest { private static final String PING_URL = "http://localhost:8080/tree-model-app/ping"; private RestTemplate restTemplate = new RestTemplate(); @Test public void routerReturnsTrueOnPing() { String response = restTemplate.getForObject(PING_URL, String.class); assertEquals("false", response, "Ping response should be the same as expected."); } } 

However when I run gradlew test - nothing happens - build successful and reports are empty. What is the problem?

My build.gradle is

buildscript { ext { springBootVersion = '2.0.0.BUILD-SNAPSHOT' } repositories { mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'war' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { jcenter() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } maven { url 'https://jitpack.io' } } dependencies { compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-webflux') compile('org.springframework.boot:spring-boot-starter-websocket') compile('org.springframework.boot:spring-boot-starter-jetty') runtime('org.springframework.boot:spring-boot-devtools') testCompile('org.springframework.boot:spring-boot-starter-test') testCompile("org.springframework:spring-test") testCompile("org.junit.platform:junit-platform-runner:$junitPlatformVersion") testCompile("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion") testCompile("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion") testCompile("com.github.sbrannen:spring-test-junit5:1.0.0.M4") } 
14
  • Is it correct the @Test import? I was expecting to see org.junit.Test. Looks like spring is not finding any tests Commented May 11, 2017 at 9:46
  • Well I am using Junit5 so I used it Commented May 11, 2017 at 9:47
  • can u run the tests individually? I mean from the IDE Commented May 11, 2017 at 9:51
  • 1
    I mean - do you have the gradle jUnit5 plugin set? apply plugin: 'org.junit.platform.gradle.plugin' Commented May 11, 2017 at 10:57
  • 1
    Great, so tests are OK. Commented May 11, 2017 at 13:32

2 Answers 2

1

So the issue is gradle plugin was not set up to run jUnit 5 tests

Here the conf needed pasted directly from JUnit5 website :

buildscript { repositories { mavenCentral() // The following is only necessary if you want to use SNAPSHOT releases. // maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } } dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4' } } apply plugin: 'org.junit.platform.gradle.plugin' 
Sign up to request clarification or add additional context in comments.

1 Comment

'org.junit.platform.junit-platform-gradle-plugin' is marked as deprecated mvnrepository.com/artifact/org.junit.platform/…. So, this answer is probably not correct for last versons of junit and gradle
0

Try to add this @RunWith(SpringRunner.class) to the test class declaration

1 Comment

this is jUnit5, Runner is replaced with @ExtendWith(SpringExtension.class)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.