我开始使用Spring。
当我到达WebDomainIntegrationTest时,在tutorial之后,由于内容响应为空,测试失败。
我想也许问题可能是服务器端口。应用程序在端口8000上运行,集成测试请求localhost端口80.(如果这是问题,我该如何设置不同的端口?)
CODE
SiteController:
@Controller
@RequestMapping("/")
public class SiteController {
private static final Logger LOG = LoggerFactory.getLogger(SiteController.class);
@Autowired
private MenuService menuService;
@Autowired
private Basket basket;
@RequestMapping(method = RequestMethod.GET)
public String getCurrentMenu(Model model) {
LOG.debug("Yummy MenuItemDetails to home view");
model.addAttribute("menuItems",getMenuItems(menuService.requestAllMenuItems(new RequestAllMenuItemsEvent())));
System.out.println("getCurrentMenu");
return "home";
}
private List<MenuItem> getMenuItems(AllMenuItemsEvent requestAllMenuItems) {
List<MenuItem> menuDetails = new ArrayList<MenuItem>();
for (MenuItemDetails menuItemDetails : requestAllMenuItems.getMenuItemDetails()) {
menuDetails.add(MenuItem.fromMenuDetails(menuItemDetails));
}
return menuDetails;
}
/*
Lastly, you need to put the Basket into the model for the view to be able to read from.
This method takes the auto injected Basket and annotates it so that it is automatically merged into the Model.
*/
@ModelAttribute("basket")
private Basket getBasket() {
return basket;
}
}
WebDomainIntegrationTest:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { PersistenceConfig.class, CoreConfig.class, WebConfig.class })
public class WebDomainIntegrationTest {
private static final String STANDARD = "Yummy Noodles";
private static final String CHEF_SPECIAL = "Special Yummy Noodles";
private static final String LOW_CAL = "Low cal Yummy Noodles";
private MockMvc mockMvc;
@Autowired
WebApplicationContext webApplicationContext;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void thatTextReturned() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andDo(print())
.andExpect(content().string(containsString(STANDARD)))
.andExpect(content().string(containsString(CHEF_SPECIAL)))
.andExpect(content().string(containsString(LOW_CAL)));
}
}
主页视图:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="defaultTemplate">
<tiles:putAttribute name="body">
<div class="body">
<h1>Home page !</h1>
<div class="col-md-12 text-center">
<c:forEach var="item" items="${menuItems}">
<div class="name"><c:out value="${item.name}"/></div>
</c:forEach>
</div>
</div>
</tiles:putAttribute>
</tiles:insertDefinition>
测试模型属性(成功!)
@ContextConfiguration(classes = {PersistenceConfig.class, CoreConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class CoreDomainIntegrationTest {
@Autowired
MenuService menuService;
@Autowired
OrderService orderService;
@Test
public void thatAllMenuItemsReturned() {
AllMenuItemsEvent allMenuItems = menuService.requestAllMenuItems(new RequestAllMenuItemsEvent());
assertEquals(3, allMenuItems.getMenuItemDetails().size());
}
@Test
public void addANewOrderToTheSystem() {
CreateOrderEvent ev = new CreateOrderEvent(new OrderDetails());
orderService.createOrder(ev);
AllOrdersEvent allOrders = orderService.requestAllOrders(new RequestAllOrdersEvent());
assertEquals(1, allOrders.getOrdersDetails().size());
}
}
答案 0 :(得分:5)
问题是,如果你使用jsp相关技术,你不能使用content()
,你需要使用model()
,试试这个:
@Test
public void thatTextReturned() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andDo(print())
.andExpect(model().attribute("test", hasItem(hasProperty("name", value)))));
}
我使用的Matcher来自Hamcrest。
答案 1 :(得分:0)
Spring Integration测试基于Spring Container,它并不关心您运行应用程序的端口或主机。
对于您的问题,问题出现在这部分代码中:
@RequestMapping(method = RequestMethod.GET)
public String getCurrentMenu(Model model) {
LOG.debug("Yummy MenuItemDetails to home view");
model.addAttribute("menuItems",getMenuItems(menuService.requestAllMenuItems(new RequestAllMenuItemsEvent())));
System.out.println("getCurrentMenu");
return "home";
}
该模型并未附加到您的回复中。请检查主页,它是否生成了模型属性&#39; menuItems&#39;?