gpt4 book ai didi

java - Spring Boot/Thymeleaf 单元测试 : Model attribute does not exist

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:10:16 24 4
gpt4 key购买 nike

我制作了一个带有表单的 View ,用户可以在其中输入 inputTemp 的值,并将输入保存在 Controller 的一个属性中。

查看:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/template :: head"></head>
<head>
<title>Smart CV</title>
</head>
<body>

<nav th:replace="fragments/template :: header"></nav>

<div class="container">
<div class="hero-unit">
<h1>Invoerscherm</h1>
</div>
</div>

<form action="#" th:action="@{/invoer}" th:object="${invoerscherm}" method="post">
<td><input type="text" id="inputTemp" name="inputTemp" th:value="${inputTemp}"/></td>
<td><input name="submitKnop" type="submit" value="Input Temp"/></td>
</form>

<nav th:replace="fragments/template :: footer"></nav>
</body>
</html>

Controller :

@Controller
public class InvoerschermController {

private String inputTemp = "20";

@GetMapping("/invoer")
public String invoer(Model model) {
model.addAttribute("inputTemp", getInputTemp());
System.out.println("1: " + model.toString());
return "invoerscherm";
}

@PostMapping("/invoer")
public String addInputTemp(String inputTemp, Model model) {
setInputTemp(inputTemp);
model.addAttribute("inputTemp", getInputTemp());
System.out.println("2: " + model.toString());

try {
int newTemp = Integer.parseInt(getInputTemp());
PostgresDatabase database = new PostgresDatabase();
Connection connection = database.connectToDatabase();
database.setTemperature(connection, newTemp);
} catch (NumberFormatException nfe) {
System.err.println("Invalid number: " + nfe.getMessage());
}

return "invoerscherm";
}

public String getInputTemp() {
return inputTemp;
}

public void setInputTemp(String inputTemp) {
this.inputTemp = inputTemp;
}
}

现在,我想编写一个 UnitTest 来检查 inputTemp 是否正确保存。这些是我的单元测试:

@RunWith(SpringRunner.class)
@WebMvcTest(InvoerschermController.class)
@AutoConfigureMockMvc
public class InvoerschermTest {
@Autowired
private MockMvc mockMvc;

@Test
public void testCorrectModel() {
try {
this.mockMvc.perform(get("/invoer", "20")).andExpect(status().isOk())
.andExpect(model().attributeExists("inputTemp"));
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testPost() {
try {
this.mockMvc.perform(post("/invoer", "20")).andExpect(status().isOk())
.andExpect(view().name("invoerscherm"));
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testPostValueInModel() {
try {
this.mockMvc.perform(post("/invoer", "20")).andExpect(status().isOk())
.andExpect(model().attributeExists("inputTemp"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

现在我的两个测试失败了(testCorrectModel() 和 testPostValueInModel()),都带有以下消息:

java.lang.AssertionError: Model attribute 'inputTemp' does not exist

据我所知,该属性确实存在,所以我在某处做错了,我只是不知道我哪里出错了。编辑:我似乎没有将变量正确发送到 addInputTemp 方法。我应该怎么做?

最佳答案

在函数 testCorrectModel、testPostValueInModel 中model 属性的值为 null,因为 inputTemp 的私有(private)字段值在第一种情况下为 null,而在第二种情况下,String inputTemp 参数为 null。

因此,在这些测试函数中,您实际上是在进行此调用

model.addAttribute("inputTemp", null);

map 的大小为 1,因为您在 map 中添加了一个属性,但是 model().attributeExists 将失败,因为该属性的值为 null。

这里是spring框架中attributeExists的内部实现

public ResultMatcher attributeExists(final String... names) {
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
ModelAndView mav = getModelAndView(result);
for (String name : names) {
assertTrue("Model attribute '" + name + "' does not exist", mav.getModel().get(name) != null);
}
}
};
}

因此,正确设置inputTemp的值。例如对于 @RequestMapping("/invoer") 你如何设置 inputTemp 的值?在不同的功能?也许,您应该将其作为路径变量传递吗?

关于java - Spring Boot/Thymeleaf 单元测试 : Model attribute does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46644515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com