jeudi 21 mars 2019

@Autowired fails under src/test

I'm trying to setup a integration test in Spring Boot 2. I created an IntegrationConfiguration.java:

@Configuration
@TestPropertySource(
    locations = "classpath:application-integrationtest.properties"
)
@ComponentScan(basePackages = { "..." })
public class IntegrationConfiguration {
    @Bean
    Sql2o sql2o(DataSource dataSource) {
        return new Sql2o(dataSource, new OracleQuirks());
    }
}

Then I created a simple testing class:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=IntegrationConfiguration.class)
public class RfxRepositoryIntegrationTest {
    @Autowired
    private RfxRepository rfxRepository;
    @Autowired
    private Rfx_models rfx_models;
    //...

    private Rfx rfx = rfx_models.getRfx();
    //...

    @Test
    public void insert() throws Exception {
        //...
    }
}

Rfx_models is this one:

@TestComponent
public class Rfx_models {
    public Rfx getRfx() {
        //...
    }
}

The problem is Rfx_models is not autowired in RfxRepositoryIntegrationTest and I get

java.lang.NullPointerException
    at ###.testSuite.integration.RfxRepositoryIntegrationTest.<init>(RfxRepositoryIntegrationTest.java:48)

It seems Spring boot does not scan under src/test. How can I force it to do, only in the testing env?

Aucun commentaire:

Enregistrer un commentaire