jeudi 25 janvier 2018

java.lang.AssertionError: No ModelAndView found

I have a test class that looks like this

@AutoConfigureMockMvc()
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest({ShoppingCartController.class, HomeController.class})
@ContextConfiguration(classes = {SecurityConfig.class})
public class ShoppingCartControllerTest {

    @Autowired
    WebApplicationContext context;

    @MockBean
    private UserDetailsServiceImpl userDetailsServiceImpl;

    @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();
    }

    @Test
    public void checkBookDetail() throws Exception {
        Book book = new Book();
        book.setId(1L);
        when(bookService.findOne(anyLong())).thenReturn(book);

        mockMvc
                .perform(get("/bookDetail")
                        .accept(MediaType.TEXT_HTML)
                        .contentType(MediaType.TEXT_HTML)
                        .param("id", "1"))
                .andExpect(model().attributeExists("book"))
                .andExpect(model().attributeExists("qty"))
                .andExpect(model().attributeExists("qtyList"))
                .andReturn();
    }

    @Test
    public void showBookShelf() throws Exception {
        List<Book> bookList = new ArrayList<>();
        when(bookService.findAll()).thenReturn(bookList);

        mockMvc.perform(get("/bookshelf")
                .accept(MediaType.TEXT_HTML)
                .contentType(MediaType.TEXT_HTML)
        )
                .andExpect(model().attributeExists("activeAll"))
                .andReturn();
    }

    @Configuration
    @Import({PropertyTestConfiguration.class, SecurityUtility.class})
    static class ContextConfiguration {
    }

}

Only the test addItemToShoppingcCart passes , the other three have the same error as shown below , It is definitely not importing everything I need but I cant figure out what exactly it is.

java.lang.AssertionError: No ModelAndView found

    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:35)
    at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:65)
    at org.springframework.test.web.servlet.result.ModelResultMatchers.getModelAndView(ModelResultMatchers.java:272)
    at org.springframework.test.web.servlet.result.ModelResultMatchers.access$000(ModelResultMatchers.java:40)
    at org.springframework.test.web.servlet.result.ModelResultMatchers$3.match(ModelResultMatchers.java:84)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
    at com.valentine.bookstore.controller.ShoppingCartControllerTest.checkBookDetail(ShoppingCartControllerTest.java:130)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

I don't understand why I t cant find models and views but when I use @SpringBootTest and EasyMock everything passes ? what can I do ?

Aucun commentaire:

Enregistrer un commentaire