![](/img/trans.png)
[英]Spring Test returning 404 instead of 200 in testing Rest API in Spring Boot
[英]Spring Boot Unit Test returns 404 instead of 200
我是springboot的新手。 我只是在看Spring 的实际操作和编程跟随作者。
然后当我只阅读第 1 章时,事情变得很困难。我需要测试 controller。 本书的代码是:
package tacos;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
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.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class) // <1>
public class HomeControllerTest {
@Autowired
private MockMvc mockMvc; // <2>
@Test
public void testHomePage() throws Exception {
mockMvc.perform(get("/")) // <3>
.andExpect(status().isOk()) // <4>
.andExpect(view().name("home")) // <5>
.andExpect(content().string( // <6>
containsString("Welcome to...")));
}
}
我正在使用 springboot 2.7.3,所以我只需从我的代码中删除@RunWith(SpringRunner.class)
。 我在做测试时立即得到一个错误:
在谷歌之后我意识到它可能找不到我的 controller 所以我将@Import @Import(HomeController.class)
添加到测试 class 中。 看起来像:
package com.qph.tacos;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@WebMvcTest(HomeControllerTest.class)
@Import(HomeController.class)
public class HomeControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomePage() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("home"))
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("Welcome to...")));
}
}
那么它就通过了测试!!!
现在我只想知道为什么作者写的代码不用@Import(HomeController.class)
也能通过测试?
我的目录结构是这样的:
待测controller为:
package com.qph.tacos;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
// return the name of template
return "home";
}
}
我是springboot的新手。 也许这是一个简单的问题,但我真的花了很多时间。
谢谢你的帮助!
改成
WebMvcTest(HomeController.class)
您应该使用此注释引用正在测试的 controller,否则它将不会加载到应用程序上下文中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.