我正在测试下面给出的控制器
@Controller
public class MasterController {
@GetMapping("/")
public String goLoginPage(){
return "index";
}
}
我正在按照这个Spring文档来测试我的控制器。 现在,我想通过仅实例化Web层而不是整个Spring上下文来测试我的控制器,就像文档中所述。以下是我编写的代码:
@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"));
}
}
当我运行这个测试时,我遇到了错误
Unable to find @SpringBootConfiguration,...etc
。但我很困惑为什么它要求Spring配置,因为我们不想实例化它,而只想使用Web层。请指导我正确的方向以弄清楚发生了什么,并告诉我如何解决。谢谢。
@WebMvcTest
中给出控制器名称,它仍然无法工作? - Meena Chaudhary