Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

JavaSpring MvcSpring BootSpring Test

Java Problem Overview


I am testing my controller given below

@Controller
public class MasterController {

@GetMapping("/")
public String goLoginPage(){
	return "index";
}
}

I am following [this][1] Spring documentation to test my controller. [1]: https://spring.io/guides/gs/testing-web/

Now, I want to test my controller by just instantiating the web layer and not the whole Spring context as given in the documentation. Below is my code for the same.

@RunWith(SpringRunner.class)
@WebMvcTest
public class MasterControllerTestWithWebLayer {

@Autowired
MockMvc mockMvc;

@Autowired
MasterController masterController;


@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testLoginHome() throws Exception{
	mockMvc.perform(get("/"))
	.andExpect(status().isOk())
	.andExpect(view().name("index"));
}

}

When I run this test I get the error Unable to find @SpringBootConfiguration,...etc. But I am confused why it is asking for Spring configuration when we do not want it to instantiate it but want to use only the web layer. Kindly point me to the right direction what is happening here. And also how to fix this. Thanks

Java Solutions


Solution 1 - Java

So here is the solution:

The documentation on detecting test configuration says:

> The search algorithm works up from the package that contains the test until it finds a @SpringBootApplication or @SpringBootConfiguration annotated class. As long as you’ve structure your code in a sensible way your main configuration is usually found.

So the @SpringBootApplication class should be higher in the package hierarchy than the test class e.g if test class is in package com.zerosolutions.controller then @SpringBootApplication class should be in a package higher than com.zerosolutions.controller package i.e com.zerosolutions or com.

Problem

But in case the @SpringBootApplication class is at the same level as test class it won't be able to find it i.e com.zerosolutions.general. In this case you'll get the following error:

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

Solution

If you are running an integrated test, you can explicitly mention the @SpringBootApplication class like this

@RunWith(SpringRunner.class)
@SpringBootTest(classes={SpringBootApp.class})

But if you want to do unit testing of a controller you don't need to fire up the whole Spring context. You can rather replace @SpringBootTest with @WebMvcTest(MasterController.class). This will instantiate only the web layer with MasterController and not the whole Spring context.

Problem

But the problem is you will again run into the error we faced earlier: > java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

And @WebMvtTest does not have a classes attribute like @SpringBootTest to explicitly mention the @SpringBootApplication class. So there are two solutions to this.

Solution

First: Move your application class to a package higher than the test class i.e com.zerosolutions or com package.

Second: Mention your @SpringBootApplication class explicitly like below

@RunWith(SpringRunner.class)
@WebMvcTest(MasterController.class)
@ContextConfiguration(classes={SpringBootApp.class})

Hope that clears the Spring Test Configuration confusion. Thanks

Solution 2 - Java

If your Application.java class (in src/main/java) is located under

com.A.B

Your test class ApplicationTest.java (in src/test/java) need to be under

com.A.B or com.A.B.C or com.A.B.C.D

You will get this error if the test class is located under the following packages

com.A or com.A.C or com.A.D

In Spring boot THE GENERAL RULE IS TEST CLASS PACKAGE NAME NEED TO START WITH THE PACKAGE NAME OF THE JAVA CLASS PACKAGE THAT IS GOING TO BE TESTED

Solution 3 - Java

I had the same error, and found that when I had generated the project my pom.xml showed my groupId as com.example rather than with my actual domain:
<groupId>com.example</groupId>
I corrected the pom.xml to be:
<groupId>com.mydomain</groupId>
Next, I changed the file structure from:
src/test/java/com/example to src/test/java/com/mydomain
Last, I had to update the package declaration inside my
SampleProjectApplicationTest.java
file to match the correct file structure. Once that was all in place, the tests worked fine.

I am not sure how I ended up with com.example where the rest of my project was correct, but the fix was that simple in my case.

Hopefully this helps someone.

Solution 4 - Java

check if src/test/java has same package name as the main class package. src/test/java/com/example/abc is same as src/test/java/com/example/abc

Solution 5 - Java

Just replace the Test package name with the main Application class package name.

Example:- package com.kotlin.dealerMainApplication this is my main app package name so, I will put the same name in my Test package

This worked for me !

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMeena ChaudharyView Question on Stackoverflow
Solution 1 - JavaMeena ChaudharyView Answer on Stackoverflow
Solution 2 - JavaTadele AyelegnView Answer on Stackoverflow
Solution 3 - JavastockView Answer on Stackoverflow
Solution 4 - JavaBeginnerView Answer on Stackoverflow
Solution 5 - JavaiamkdblueView Answer on Stackoverflow