I want create tests working with the Spring Context, with mocked Repository beans. I'm using Spring Boot 1.3.2.BUILD-SNAPSHOT + JUnit + Mockito.
Here is my Test config class:
@ComponentScan(basePackages = "myapp", excludeFilters =
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = {
OfferRepository.class
}
)
)
@Configuration
public class TestEdge2EdgeConfiguration {
@Bean
public OfferRepository offerRepository() {
return mock(OfferRepository.class);
}
}
Purpose of this configuration is to exclude OfferRepository from Spring Context and mock it, thank to this I'll be able to write tests who are using Spring Context with mocked database Repository.
Here is my test class:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestEdge2EdgeConfiguration.class})
@WebAppConfiguration
public class OfferActionsControllerTest {
@Autowired
private OfferRepository offerRepository;
@Autowired
private OfferActionsController offerActionsController;
@Before
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);
}
}
Test and test configuration directory is:
src/test/java/myapp
My application configuration and packages containing OfferRepository directory is:
src/main/java/myapp/
The problem is that Spring Boot is not loading my configuration from TestEdge2EdgeConfiguration.class and mock for OfferRepository is never created.
Can any body help me with this, please?
Aucun commentaire:
Enregistrer un commentaire