mercredi 21 mars 2018

Testing Spring Boot console app

I have a console app written on Spring Boot. I have write tests;

I have some class, which connect to external service:

public class CoreDao {

    private final Pool sessionPool;

    @Autowired
    public CoreDao(Pool pool) {
        this.sessionPool = pool;
    }

I want inject mock of Pool. I write some test:

@SpringBootTest(classes = ApplicationConfigurationTest.class)
@RunWith(SpringRunner.class)
public class ExecutorTest {
  // some methods
}

I created test configuration class:

@TestConfiguration
public class ApplicationConfigurationTest {
    @Bean
    @Primary
    public Pool sessionPool() throws Exception {
        return mock(Pool.class);
}

But it does not work at all. Spring does not go into my test configuration at all (checked in the debugger). Instead of mock bean, it uses the real one, which is registered in the applicationContext.hml in the application itself. How can I get a Spring boot to inject my mock so that it does not load the real mock? Any idea.

Aucun commentaire:

Enregistrer un commentaire