Vincent F
Vincent F

Reputation: 7341

using @MockBean for a List

I have a Spring Boot test that uses @WebMvcTest . I need to update my controller under test, so that it now accepts a list of Item as a constructor parameter. That list of elements is parsed in the constructor to configure the controller as I expect.

I am used to use @MockBean to mock a dependency in that case... But since the constructor parameter is a List, it doesn't work if I apply the annotation on the list : I don't want to mock the List, I want to mock the Item elements in the list..

What's the approach here ?

Thanks

Upvotes: 1

Views: 2619

Answers (1)

Vincent F
Vincent F

Reputation: 7341

It's actually very simple : just use @MockBean on a couple of Item, like

@MockBean
private Item  mockItem1;

@MockBean
private Item mockItem2;

These mocks will be instantiated, and injected as a list in the controller constructor

Upvotes: 1

Related Questions