我想知道我能看到这个错误(无法自动装配。找不到“MockMvc”类型的 bean。)
这是我的代码
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.test.web.servlet.MockMvc;
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.*;
@WebMvcTest(HomeController.class)
public class HomeControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomePage() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("home"))
.andExpect(content().string(
containsString("Welcome to...")));
}
}
Run Code Online (Sandbox Code Playgroud)
此测试代码运行成功。但mockMVC显示有关自动接线的错误。
如何删除这个错误?
请任何人帮忙。
我正在使用 IntelliJ IDEA 2022.1.1(终极版)、java、spring、junit5。
谢谢
我正在学习科特林。我有一个关于使用异常的问题。在 kotlin 中使用对象异常和类异常哪个更好?
object CustomDuplicateId : RuntimeException("Invalid user with Id")
class CustomDuplicateId : RuntimeException("Invalid user with Id")
Run Code Online (Sandbox Code Playgroud)
如果我仅在一个位置使用此异常,则堆栈跟踪始终相同。所以我认为...不需要使用class. 这样对吗?
还是使用单例异常不好?
谁能让我知道编写异常的更好代码是什么?