Error: Cannot find @SpringBootConfiguration while executing @WebMvcTest for Spring controller

I am testing my controller below

@Controller
public class MasterController {

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

I follow this Spring documentation to test my controller. Now I want to test my controller by simply creating a web layer, and not the entire Spring context, as indicated 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 an error Unable to find @SpringBootConfiguration,...etc. But I'm confused why it is requesting the Spring configuration when we don't want it to instantiate, but you only want to use the web tier. Please indicate to me in the right direction what is happening here. And also how to fix it. Thanks

+4
3

, :

:

, , @SpringBootApplication @SpringBootConfiguration. , .

, @SpringBootApplication , , , com.zerosolutions.controller, @SpringBootApplication com.zerosolutions.controller, .. com.zerosolutions com.

@SpringBootApplication , , i.e com.zerosolutions.general. :

java.lang.IllegalStateException: @SpringBootConfiguration, @ContextConfiguration @SpringBootTest (classes =...)

, @SpringBootApplication,

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

, Spring. @SpringBootTest @WebMvcTest(MasterController.class). - MasterController, Spring.

, , :

java.lang.IllegalStateException: @SpringBootConfiguration, @ContextConfiguration @SpringBootTest (classes =...)

@WebMvtTest classes, @SpringBootTest, @SpringBootApplication. , .

. , test.e com.zerosolutions com.

. @SpringBootApplication .

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

, Spring.

+24

Application.java( src/main/java) com.A.B

ApplicationTest.java( src/test/java) com.A.B com.A.B.C com.A.B.C.D

,   com.A com.A.C com.A.D

Spring JAVA CLASS,

+3

, , , pom.xml groupId com.example, :
<groupId>com.example</groupId>
pom.xml:
<groupId>com.mydomain</groupId>
:
src/test/java/com/example src/test/java/com/mydomain
, SampleProjectApplicationTest.java
. , , .

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

Hope this helps someone.

0
source

Source: https://habr.com/ru/post/1675243/


All Articles