I am trying to test a mongo document persister using Junit along with Fongo. I am able to inject MongoTemplate to the class to be tested. But the call mongotemplate.getCollection("testCollection") returns null. I am not sure how to load the collection also for the unit testing.
Class to be tested :-
@Component
@Doc
public class MongoPersisterBean {
@Autowired
protected MongoTemplate template;
@Override
public boolean save(String collectionName, String json) {
template.getCollection(collectionName).save(json);
return true;
}
}
Junit class,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MongoPersisterBeanTest {
@InjectMocks
private DocumentPersisterBean bean = new DocumentPersisterBean();
@Mock
@Autowired
private MongoTemplate mongoTemplate;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSave() {
System.out.println("Template "+mongoTemplate);
System.out.println("Collection ===> "+mongoTemplate.getCollection("testCollection"));
}
@Configuration
static class MongoTestConfiguration {
@Bean
public Mongo mongo() {
// Configure a Fongo instance
return new Fongo("mongo-test").getMongo();
}
@Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongo(), "testDB");
}
}
Aucun commentaire:
Enregistrer un commentaire