mercredi 27 janvier 2016

Mocking Spring Context in SpringBoot

I'm using Spring Boot 1.3.2, and I notice problem, ComponentScan in my test class is not working. And I want to mock some of Spring Beans. Is spring boot blocking ComponentScan?

Test config class:

@Configuration
@ComponentScan(value = {"myapp.offer", "myapp.image"})
public class TestEdge2EdgeConfiguration {

    @Bean
    @Primary
    public OfferRepository offerRepository() {
        return mock(OfferRepository.class);
    }

}

Test class:

@ContextConfiguration(classes=TestEdge2EdgeConfiguration.class, loader=AnnotationConfigContextLoader.class)
public class OfferActionsControllerTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private OfferRepository offerRepository;

    @Autowired
    private OfferActionsController offerActionsController;

    @BeforeMethod
    public void setUp(){

        MockitoAnnotations.initMocks(this);
    }


    @Test
    public void saveOffer() {
        //given
        BDDMockito.given(offerRepository.save(any(Offer.class))).willReturn(new Offer());
        //when
        ResponseEntity<Offer> save = offerActionsController.save(new Offer());

        //then
        org.springframework.util.Assert.notNull(save);
    }
}

Aucun commentaire:

Enregistrer un commentaire