Terminal

Unable to find a @SpringBootConfiguration,
	you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

The Spring Boot tests @SpringBootTest or @DataJpaTest need to find the @SpringBootConfiguration application class to launch the entire application and do the tests. And if Spring Boot can’t find the SpringBootConfiguration, it throws errors and stops the tests.

A test class.

HelloControllerTest.java

package com.favtuts;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerTest {

	@Autowired
	private TestRestTemplate template;

	@Test
	public void hello_ok() throws Exception {
			ResponseEntity<String> response = template.getForEntity("/", String.class);
			assertThat(response.getBody()).isEqualTo("Hello World, Spring Boot!");
	}

}

@SpringBootApplication class.

MyWebApplication.java

package com.favtuts.app;

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

@SpringBootApplication
public class MyWebApplication {

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

}

1. Solution – Redesign the directory structure

Spring Boot finds @SpringBootConfiguration annotation in the current package, and if they cannot find it there, they find it by traversing up the directory (package) hierarchy until they find it.

Review the below project structure. The MyWebApplication or @SpringBootConfiguration class is at the package com.favtuts.app; and the test HelloControllerTest is at the package com.favtuts, different package level.

If we run the test, Spring Boot first try to find the @SpringBootConfiguration class at the package com.favtuts and if the file is not there, it finds at package com, and keeps traversing up the directory hierarchy until they find it; In this case, Spring Boot can’t find the @SpringBootConfiguration and throw the error Unable to find a @SpringBootConfiguration.

Terminal

Unable to find a @SpringBootConfiguration,
	you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

The solution is to redesign the directory structure or package so that the tests can find the @SpringBootConfiguration.

For examples:

Move com.favtuts.app.MyWebApplication to com.favtuts.MyWebApplication; both application and tests are at the same package level.

  • com.favtuts.MyWebApplication
  • com.favtuts.HelloControllerTest

Or, we can move the test class com.favtuts.HelloControllerTest to com.favtuts.app.HelloControllerTest.

  • com.favtuts.app.MyWebApplication
  • com.favtuts.app.HelloControllerTest

2. Solution – @SpringBootTest(classes =…)

Alternatively, if we can’t move the @SpringBootConfiguration or test classes, we can tell the test where to find the @SpringBootConfiguration class via @SpringBootTest(classes = MyWebApplication.class).

HelloControllerTest.java

package com.favtuts;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(classes = MyWebApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerTest {

	@Autowired
	private TestRestTemplate template;

	@Test
	public void hello_ok() throws Exception {
			ResponseEntity<String> response = template.getForEntity("/", String.class);
			assertThat(response.getBody()).isEqualTo("Hello World, Spring Boot!");
	}

}

Download Source Code

$ git clone https://github.com/favtuts/java-spring-boot-tutorials
$ cd spring-boot-hello-world
$ mvn spring-boot:run

References

Leave a Reply

Your email address will not be published. Required fields are marked *