Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find a SpringBootConfiguration in Spring Boot Test 1.4

I'm not able to run a simple test in spring boot 1.4. I followed the tutorial from the official site testing-the-spring-mvc-slice but I didn't get it to work.

every time i get the following error:

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

any ideas, hints?

Thanks in advance

Edit:

this is the controller

@Controller
public class UserManagementController {

@GetMapping(value = "/gs/users/getUsers")
    public @ResponseBody String getAllUsers() {
        return "test";
    }
}

this is the test

@RunWith(SpringRunner.class)
@WebMvcTest(UserManagementController.class)
public class UserManagementControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void showUserView() throws Exception {
        this.mvc.perform(get("/gs/users/getUsers"))
            .andExpect(status().isOk())
            .andDo(print());
    }
}

From my point of view it's exactly the same like this post from the site.

the @WebMvcTest will do:

  • Auto-configure Spring MVC, Jackson, Gson, Message converters etc.
  • Load relevant components (@Controller, @RestController, @JsonComponent etc)
  • Configure MockMVC

now why i need to configure a "super" class

like image 431
Manu Zi Avatar asked Oct 24 '16 10:10

Manu Zi


People also ask

Why can't I find @springbootconfiguration?

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

How to validate the Spring Boot Configuration of an annotated class?

Now that we have an annotated class, Spring is able to bootstrap our tests. To validate our setup, let's inject a TestEntityManager and validate that it is set: This test succeeds when Spring can find the @SpringBootConfiguration in its own package or one of its parent packages. 4. Conclusion

What is the general rule for testing in Spring Boot?

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 Show activity on this post. file to match the correct file structure.

How to add @springbootapplication class to @webmvttest?

And @WebMvtTest does not have a classes attribute like @SpringBootTest to explicitly mention the @SpringBootApplication class. So there are two solutions to this. First: Move your application class to a package higher than the test class i.e com.zerosolutions or com package.


1 Answers

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 you have annotated your test with @*Test. It run, checked for configuration in subclasses, haven't found any, thrown an exception.

You have to have a config in a package or subpackage of test class or directly pass config class to @ContextConfiguration or @SpringBootTest or have class annotated with @SpringBootApplication.

According to @SpringBootApplication. I have tested controller in way you have mentioned with @WebMvcTest: it works if application has class annotated as @SpringBootApplication and fails with exception you've mentioned if not. There is remark it the article you mentioned:

In this example, we’ve omitted classes which means that the test will first attempt to load @Configuration from any inner-classes, and if that fails, it will search for your primary @SpringBootApplication class.

Github discussion about the same point.

Spring Boot Documentation

like image 159
Sergii Getman Avatar answered Nov 15 '22 05:11

Sergii Getman