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:
@Controller
, @RestController
, @JsonComponent
etc)now why i need to configure a "super" class
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
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
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With