0

This is the Controller I have:

@Controller
@EnableWebMvc
@RequestMapping(value = "/spittles")
public class SpittleController {

    SpittleRepository spittleRepository;

    @RequestMapping(method = RequestMethod.GET)
    public String getSpittles(Model model) {
        model.addAttribute(spittleRepository.findSpittles(10, 10));
        return "spittles";
    }

    @Autowired
    public void setSpittleRepository(SpittleRepository spittleRepository) {
        this.spittleRepository = spittleRepository;
    }

}

and the View I have (spittles):

<c:forEach items="${spittleList}" var="spittle">
    ${spittle.message}
</c:forEach>

which works fine..

However this test fails:

    SpittleController spittleController = new SpittleController();
    spittleController.setSpittleRepository(mockRepository);

    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(spittleController)
            .setSingleView(new InternalResourceView("/WEB-INF/views/spittles.jsp")).build();

    mockMvc.perform(get("/spittles"))
            .andExpect(view().name("spittles"))
            .andExpect(model().attributeExists("spittleList"));

with the message:

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

But in the jsp, it looks like the model has the attribute spittleList , what am I missing?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

2 Answers2

0

After specify the name, is problem still occur?

model.addAttribute("spittleList", spittleRepository.findSpittles(10, 10));
kimwz.kr
  • 298
  • 1
  • 9
0

Hi You can take help of expect to mock your service, look as below:

expect(spittleRepository.findSpittles(10, 10)).andReturn(EXPECTED OUTPUT)