gpt4 book ai didi

java - Spring MVC View / Controller 对已知良好图 block 设置的测试失败并出现断言错误

转载 作者:行者123 更新时间:2023-12-01 13:15:14 25 4
gpt4 key购买 nike

我有这个 MVC Controller :

@RequestMapping(produces = "text/html")
public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) {
if (page != null || size != null) {
int sizeNo = size == null ? 10 : size.intValue();
final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
uiModel.addAttribute("smaker", Smak.findSmakEntries(firstResult, sizeNo, sortFieldName, sortOrder));
float nrOfPages = (float) Smak.countSmaker() / sizeNo;
uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
} else {
uiModel.addAttribute("smaker", Smak.findAllSmaker(sortFieldName, sortOrder));
}
return "smaker/list";
}

}

我使用 Tiles 2.2.2,当 Web 应用程序在 Jetty 中运行时,我会呈现此 View ,但我想评估 spring-test,所以我从这个简单的测试开始:

@RunWith(SpringJUnit4ClassRunner.class)
public class ViewsControllerTests extends AbstractContextControllerTests {

private MockMvc mockMvc;

@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
}//wac is in the superclass which loads it from file

@Test
public void getSmakerView() throws Exception {
this.mockMvc.perform(get("/smaker"))
.andExpect(view().name(containsString("smaker/list")));
}

}

确实,我有渲染 View [org.springframework.web.servlet.view.tiles2.TilesView name 'smaker/list';当我在 Jetty 中运行该项目时,在日志中。

然而,当我运行测试时,上面的测试失败了

java.lang.AssertionError: Expected: a string containing "smaker/list but: was "uncaughtException"

这是我的 SimpleMappingExceptionResolver 的默认 ErrorView。所以我的测试或我对其作用的假设似乎有问题。你知道我的错误可能在这里吗?

最佳答案

问题是由调用引起的

uiModel.addAttribute("smaker", Smak.findAllSmaker(sortFieldName, sortOrder));

尤其是这部分:Smak.findAllSmaker如果没有任何应用程序上下文以及由此产生的依赖项注入(inject)或模拟此行将产生您面临的问题。

关于java - Spring MVC View / Controller 对已知良好图 block 设置的测试失败并出现断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22524516/

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