我有一个看起来像这样的测试类 我发现使用@WebMvcTest时的测试由于某种原因无法识别网址。任何帮助都可以满足。 调试后,我得到DefaultRequestBuilder = Null,DefaultRequestMatcher size = 0
@AutoConfigureMockMvc()
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest({ShoppingCartController.class, HomeController.class})
@ContextConfiguration(classes = {SecurityConfig.class})
public class ShoppingCartControllerTest {
@Autowired
WebApplicationContext context;
@Autowired
private MockMvc mockMvc;
@MockBean
private BookService bookService;
@MockBean
private UserService userService;
@MockBean
private CartItemService cartItemService;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@Test
public void showLoginPage() throws Exception {
mockMvc.perform(get("/login")
.accept(MediaType.TEXT_HTML)
.contentType(MediaType.TEXT_HTML)
)
.andExpect(model().attributeExists("classActiveLogin"))
.andReturn();
}
@Test
@WithMockUser(username = "V", authorities = {"USER"})
public void addItemToShoppingCart() throws Exception {
CartItem cartItem = new CartItem();
String qty = "2";
Book book = new Book();
User user = new User();
book.setId(1L);
book.getId();
cartItem.setBook(book);
when(userService.findByUsername(anyString())).thenReturn(user);
when(bookService.findOne(anyLong())).thenReturn(book);
when(cartItemService.addBookToCartItem(book, user, Integer.parseInt(qty))).thenReturn(cartItem);
ObjectMapper mapper = new ObjectMapper();
String bookAsString = mapper.writeValueAsString(book);
mockMvc
.perform(get("/shoppingCart/addItem")
.accept(MediaType.TEXT_HTML)
.contentType(MediaType.TEXT_HTML)
.param("book", bookAsString)
.param("qty", qty))
.andReturn();
}
@Configuration
@Import({PropertyTestConfiguration.class, SecurityUtility.class})
static class ContextConfiguration {
}
}
只有测试addItemToShoppingcCart通过,其他三个具有如下所示的相同错误,它绝对不会导入我需要的所有内容,但我无法弄清楚究竟是什么。
java.lang.AssertionError:找不到ModelAndView
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:35)
at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:65)
I don't understand why I t cant find models and views but when I use @
SpringBootTest和EasyMock一切都通过了吗?我能做什么 ?
这些是我的控制器
@RequestMapping("/login")
public String login(Model model) {
model.addAttribute("classActiveLogin", true);
return "Myaccount";
}
@PreAuthorize("hasAuthority('USER')")
@RequestMapping("/addItem")
public String addItem(
@ModelAttribute("book") Book book,
@ModelAttribute("qty") String qty,
Model model, Principal principal
) {
User user = userService.findByUsername(principal.getName());
try {
book = bookService.findOne(book.getId());
if (Integer.parseInt(qty) > book.getInStockNumber()) {
model.addAttribute("notEnoughStock", true);
return "forward:/bookDetail?id=" + book.getId();
}
CartItem cartItem = cartItemService.addBookToCartItem(book, user, Integer.parseInt(qty));
model.addAttribute("addBookSuccess", true);
} catch (NullPointerException e) {
}
return "forward:/bookDetail?id=" + book.getId();
}
我正在测试两个控制器
@Controller
public class HomeController {
@Autowired
private JavaMailSender mailSender;
@Autowired
private MailConstructor mailConstructor;
@Autowired
private UserService userService;
@RequestMapping("/login")
public String login(Model model) {
model.addAttribute("classActiveLogin", true);
return "Myaccount";
}
第二个具有类级别映射
@Controller
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
@Autowired
private UserService userService;
// autowired services here
@PreAuthorize("hasAuthority('USER')")
@RequestMapping("/addItem")
public String addItem(
@ModelAttribute("book") Book book,
@ModelAttribute("qty") String qty,
Model model, Principal principal
) {
User user = userService.findByUsername(principal.getName());
try {
book = bookService.findOne(book.getId());
if (Integer.parseInt(qty) > book.getInStockNumber()) {
model.addAttribute("notEnoughStock", true);
return "forward:/bookDetail?id=" + book.getId();
}
CartItem cartItem = cartItemService.addBookToCartItem(book, user, Integer.parseInt(qty));
model.addAttribute("addBookSuccess", true);
} catch (NullPointerException e) {
}
return "forward:/bookDetail?id=" + book.getId();
}