Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null autowired fields in test classes #172

Open
choda opened this issue May 30, 2017 · 0 comments
Open

Null autowired fields in test classes #172

choda opened this issue May 30, 2017 · 0 comments

Comments

@choda
Copy link

choda commented May 30, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant