lundi 29 janvier 2018

Is there a way to not use @MockBean on Spring MVC tests?

I'm trying to test the Rest contract of my Rest Controller using @WebMvcTest, like:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
class MyControllerTest {

The method that I'm testing use only MyFirstService, but inside the class there another two services (MySecondService and MyThirdService) used by two other methods.

The problem is the use of @MockBean. The test class seems like this:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
class MyControllerTest { 

    @MockBean
    private MyFirstService s1; // internal method mocked for the test

    @MockBean
    private MySecondService s2; //never used

    @MockBean
    private MyThirdService s3; //never used

    @Test
    public void testMethod() [
        // mock s1
        // calls rest controller method
    }

}

I think that this solution is not elegant with these annotations... The declared s2 and s3 appears to not be used from who read the code. And every service that is injected in the MyController needs to be there with @MockBean.

So, I have two questions:

  1. Is there a way to not use @MockBean on Spring MVC tests?
  2. Is there a better way to do the same thing, even using @MockBean?

Aucun commentaire:

Enregistrer un commentaire