jeudi 6 février 2020

Spring testing with MockMvc on specific repositories

I'm trying to do do test with my spring-boot application using MockMvc, I'm new to all of this so I'm not even sure our tests are laid out correctly. We have n repositories, and a controller for each repository. Our tests are laid out as so:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = Repo1Controller.class)
public class Repo1ControllerTest
{
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private Repo1Repository repository;

    //.... Tests and such
}

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = Repo2Controller.class)
public class Repo2ControllerTest
{
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private Repo2Repository repository;

    //.... Tests and such
}

My application has multiple repositories:

public interface Repo1 extends JpaRepository<Type1, Long> {
    //Don't need anything in here
}

public interface Repo2 extends JpaRepository<Type2, Long> {
    //Don't need anything here either
}

Each test class uses the same repositories as the controller that it's testing for. However. I get a could not get applicationContext error, with the first test class complaining that no bean is available for repo2repository, and the second test class complaining the no bean is available for repo1repository.

I'm wondering if there's a way to make MockMvc ignore the existence of the repositories it's not using? I tried using the @EnableAutoConfiguration(exclude={RepoNRepository.class}), but that did not fix the issue.

Aucun commentaire:

Enregistrer un commentaire