mercredi 6 juillet 2016

Springboot override interface in test

I have a springboot app (v1.3.5)

My main application goes:

@SpringBootApplication
@ComponentScan({"com.akrome"})
public class InboxCoreApplication {
    public static void main(String[] args) {
        SpringApplication.run(InboxCoreApplication.class, args);
    }
}

Somewhere in the packages I have a repo defined as an implementation:

@Repository
public class CouchbaseServiceImpl implements CouchbaseService {

And somewhere else I have a class using that interface:

@Repository
public class InboxObjectRepositoryImpl {
    @Autowired
    private CouchbaseService couchbaseService;

I also have a test version of the interface:

@Repository
public class CouchbaseServiceTestImpl implements CouchbaseService {

Now. In my test, I want to simply re-point the interface mapping to the test implementation, while keeping everything else as defined by the main config component scan. But it keeps going bananas. The closest I got is:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {InboxCoreApplication.class, TestConfig.class})
public class InboxServiceTest {
    @Autowired
    CouchbaseServiceTestImpl couchBaseServiceTestImpl;

Where TestConfig is:

@Configuration
public class TestConfig {
    CouchbaseServiceTestImpl couchbaseServiceTestImpl = new CouchbaseServiceTestImpl();

    public CouchbaseService couchbaseService() {
        return couchbaseServiceTestImpl;
    }
}

But every time either it complains about duplicated bean (bc it sees both implementations), or it injects 2 different instances of the test implementation, one in the test class and one in the actual program (wat?).

Any suggestion?

Aucun commentaire:

Enregistrer un commentaire