I have a controller class that makes use of several services. I write a test for that controller like:
@RunWith(SpringRunner.class)
@WebMvcTest(value = PurchaseController.class, secure = false)
public class PurchaseControllerTest {
@MockBean
private ShoppingService shoppingService;
@MockBean
private ShopRepository shopRepository;
@MockBean
private SomeOtherRepository someOtherRepository;
@Autowired
private MockMvc mockMvc;
// ... some tests goes here
Thing is, that there tend to be many of those mocks, thus many lines of code. I know this may be a sign of a code smell, but that's not my point now.
I noticed that there is also a @MockBeans
annotation that has @Target(ElementType.TYPE)
. So I thought I could try:
@RunWith(SpringRunner.class)
@WebMvcTest(value = PurchaseController.class, secure = false)
@MockBeans(value = {ShoppingService.class, ShopRepository.class})
public class PurchaseControllerTest {
but it doesn't event compile.
My question is: how we could use @MockBeans
annotation? Is it applicable to my case?