You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following up on the solution provided in #144, I defined an abstract class that I use as a parent for my test classes (in a Spring Boot 1.5.3 project):
public abstract class ControllerTest {
@Autowired
private ApplicationContext applicationContext;
@Autowired
MockMvc mvc;
@Autowired
private Fongo fongo;
// configure a connection to an in-memory server
@ClassRule
public static final InMemoryMongoDb inMemoryMongoDb = newInMemoryMongoDbRule().build();
@Rule
public MongoDbRule mongoDbRule = getSpringMongoDbRule();
// Custom rule to connect to the in-memory server. For more information, see
// https://github.com/lordofthejars/nosql-unit/issues/144
private SpringMongoDbRule getSpringMongoDbRule() {
MongoDbConfiguration configuration = new MongoDbConfiguration();
configuration.setDatabaseName("test");
// works fine if I use MockMongoClient.create(new Fongo("inMemoryInstance"));
Mongo mongo = MockMongoClient.create(fongo);
configuration.setMongo(mongo);
return new SpringMongoDbRule(configuration);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MainControllerTest extends ControllerTest {
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void testGETRootUrl() throws Exception {
RequestBuilder builder = MockMvcRequestBuilders.get("/")
.accept(MediaType.APPLICATION_JSON_UTF8);
mvc.perform(builder).andExpect(status().isOk());
}
}
The bean I try to inject is defined in a AbstractMongoConfiguration subclass:
@Configuration
public class MongoConfiguration extends AbstractMongoConfiguration {
@Bean
public Fongo fongo() {
return new Fongo("inMemoryInstance");
}
@Bean
@Override
public Mongo mongo() {
return fongo().getMongo();
}
@Bean
@Override
protected String getDatabaseName() {
return "test";
}
@Override
protected String getMappingBasePackage() {
return "my.base.package";
}
@Bean
@Override
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongo(), getDatabaseName());
}
}
When I try to inject the fongo Bean (or the database name) from the MongoConfiguration, a null value is injected and so all tests fail because of a NullPointerException.
This absolutely not critical, since if I use a new Fongo("inMemoryInstance") instead, everything works as expected and my tests run smoothly. But it's been bugging me for a few hours now and I'd really appreciate if someone could help me figure out what I'm doing wrong.
The text was updated successfully, but these errors were encountered:
Following up on the solution provided in #144, I defined an abstract class that I use as a parent for my test classes (in a Spring Boot 1.5.3 project):
The bean I try to inject is defined in a AbstractMongoConfiguration subclass:
When I try to inject the fongo Bean (or the database name) from the MongoConfiguration, a null value is injected and so all tests fail because of a NullPointerException.
This absolutely not critical, since if I use a
new Fongo("inMemoryInstance")
instead, everything works as expected and my tests run smoothly. But it's been bugging me for a few hours now and I'd really appreciate if someone could help me figure out what I'm doing wrong.The text was updated successfully, but these errors were encountered: