Spring boot 1.4 Testing : Configuration error: found multiple declarations of @BootstrapWith
Asked Answered
S

10

64

By following the official doc here: http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing

i wanted to test one of my REST API method like this:

@RunWith(SpringRunner.class)
@WebMvcTest(LoginController.class)
@SpringBootTest(classes = Application.class)
public class AuthorizationServiceTest {
    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void test() {
        Object returnedObject=his.restTemplate.getForObject("/login", Object.class);
    }
}

As stated in the doc :

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.

I have structured my code properly(atleast i think ):

AuthorizationService : is under package com.xxx.yyy.zzz.authorization;

AuthorizationServiceTest : is under package com.xxx.yyy.zzz.authorizationTest;

I am getting this exception(Full Trace):

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.orangeraid.rasberry.gateway.authorizationTest.AuthorizationServiceTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]
    at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:155)
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:126)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
    at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Please help me with this, i have already spent more than 2-3 hours without any luck.

Thanks.

Stomatic answered 9/12, 2016 at 9:54 Comment(3)
@SpringBootTest and @WebMvcTest are mutually exclusive... Either you test the whole application or you test a slice but not both.Plasticizer
@M.Deinum : its big hint, thanksStomatic
I had a similar problem with both @SpringBootTest and @DataMongoTest. Removing @SpringBootTest solved the problem. The error message could be clearer though...Crosse
P
42

This exception occurs when spring test can not find main configuration class. Try to add @ContextConfiguration anootation to your test class. Follow the spring test documention for more details (section Detecting test configuration)

My example Test class is like this:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
    ...
}
Pardue answered 27/4, 2017 at 13:28 Comment(3)
Worked perfectly!Comedy
Note: This is also necessary if you move the Application.class from the default main package to a sub-package.Cochise
Also fixes the 2 application classes issue when you have one separate for tests.Critique
L
30

Just Remove the @SpringBootTest and everything work fine. I had the same problem with using @SpringBootTest and @DataJpaTest, this error raised when I upgraded the pom.xml parent springboot version to 2.1.0 as below. When I was using version 2.0.5, this error was not raising.

@RunWith(SpringRunner.class)
//@SpringBootTest//(classes = KalahApplication.class)
// DataJpaTest supports rollback after running every test case
@DataJpaTest
public class GameRepositoryTest {

pom.xml

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
   </parent>
Lumen answered 20/11, 2018 at 0:16 Comment(0)
J
16

I know its too late to answer this question, but it may help someone in the future so... I had the same issue and after some research, i found that there shouldn't be @WebMvcTest if there is @SpringBootTest. so just remove @WebMvcTest and @SpringBootTest is taking care of the rest.

Jedthus answered 3/2, 2019 at 5:35 Comment(2)
From the documentation: In this test, the full Spring application context is started, but without the server. We can narrow down the tests to just the web layer by using @WebMvcTest Which means @WebMvcTest is useful to make tests a bit lighter by not loading a server neither a full application context. Thus Nesrin's answer is more accurate.Frightened
This solution has some drawbacks. Keep in mind that @SpringBootTest always loads the full application context. Here is a thread about the differences: #39866096Wayfarer
J
7

It happens because you declared both @WebMvcTest and @SpringBootTest, I resolve my same problem by removing @SpringBootTest

Jeer answered 6/4, 2020 at 15:40 Comment(0)
O
3

You must use only @DataJpaTest. Remove @SpringBootTest in your Testclass

Olinger answered 11/9, 2021 at 16:56 Comment(0)
P
0

Hope this will help. It worked for me after taking it off @SpringBootTest, and was testing the web later for the same @AutoWired would throw an error as i'd used interface. Rather mock the same it worked.

 package com.naveen.productreview;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.naveen.productreview.service.IProductReviewService;

@RunWith(SpringRunner.class)
@WebMvcTest
//@SpringBootTest
public class WebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IProductReviewService productReviewService; 

    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.mockMvc.perform(get("/api/product-review")).andDo(print()).andExpect(status().isOk())
        .andExpect(content().string(containsString("Naveen"))); 
    }
}
Papilloma answered 4/6, 2020 at 5:36 Comment(0)
C
0

Looks like you want to write integration test. That's why i recommend use only

@SpringBootTest(classes = Application.class)

If you want to use junit4, then add

@RunWith(SpringRunner.class)
Colloid answered 11/10, 2020 at 22:3 Comment(0)
O
0

in Junit 5 could be:

@ExtendWith(SpringExtension.class)
@DataJpaTest
class DeliveryRepositoryTest {

    @Autowired
    private DeliveryRepository repository;
    ...
}
Obstipation answered 18/12, 2020 at 14:33 Comment(0)
S
0

I had a similar issue with this set up:

@WebMvcTest
@SpringBootTest
@ContextConfiguration(classes = {Controller.class})
public class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

...}

and I have a Spring service bean autowired in the controller.

The final correct set up is to just leave @WebMvcTest, which provide the MockMvc, and add a @MockBean for the service. My final, working, test is:

@WebMvcTest(Controller.class)
public class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CustomerService customerService; 

...}
Scathing answered 15/5, 2021 at 8:47 Comment(0)
I
0

When you specify a server.port for your app you should define one also in your test main application

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Ingoing answered 27/6, 2022 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.