Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

unit testing - Mocking beans in spring context using Spring Boot

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution of this problem

Test config class, it's important to exclude SpringBootApplicationConfigutation and add @ComponentScan:

@ComponentScan(basePackages = "com.example", excludeFilters =
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
            value = {SpringBootApplicationConfigutation.class, MyDao.class}
    )
)
@Configuration
public class TestEdge2EdgeConfiguration {

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

}

And test:

    @SpringApplicationConfiguration(classes=TestEdge2EdgeConfiguration.class)
    public class OfferActionsControllerTest extends AbstractTestNGSpringContextTests {

        @Autowired
        private OfferRepository offerRepository;

        @Autowired
        private OfferActionsController offerActionsController;

        @BeforeMethod
        public void resetMock() {
           Mockito.reset(offerRepository);
        }

        @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);
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...