mardi 13 avril 2021

Spring - how to @Autowired jpa repository if mock is working fine

I'm writing tests for my Controller class. I have configuration class which looks like this :

@Configuration
@Import(RoomRepository.class)
public class TestConfig {
@MockBean
RoomRepository roomRepository;

@Bean
ReservationService reservationService() {
    return new ReservationService(roomRepository);
}

}

In controller test class I'm injecting config class :

@WebMvcTest(ReservationController.class)
@Import({TestConfig.class})
class ReservationControllerTest { ... }

Everything works well except one thing. I need "real" room repository - not mock. When I try to @Autowired that repository I'm receiving an error that it's interface, not class (which is quite obvious). How can I initialize working instance of RoomRepository ?

Aucun commentaire:

Enregistrer un commentaire