dimanche 27 mars 2016

Spring: create mocks instead of real objects

I'm using Spring annotation based configuration in my Play application.

Controllers and DAOs are Spring beans. Controller and DAO layers are defined with different Spring profiles and each layer could be disabled separately.

I'd like to test controller layer in isolation from DAO layer. I've disabled DAO profile and redefined each of DAO beans as a Mockito mock. From functional point of view it works fine, the only thing I don't like is defining mocks manually like this:

@Configuration
@Import(AppContext.class)
public class TestAppContext {

    @Bean
    public DaoA getDaoA(){
        return mock(DaoA.class);
    }

    //... all dependencies are re-defined manually
}

Is there a way to define package (like with @ComponentScan annotation) and get all beans in that package as mocks instead of real objects?

Aucun commentaire:

Enregistrer un commentaire