jeudi 8 octobre 2015

Replace bean at runtime

The codebase is typical spring based enterprise codebase with about 1.5m lines of code. We have quite a few spring context files. The test infra is a problem.

For the test cases, I created another set of test-spring files (mostly it imports relevant project spring contexts) and for few beans contains mocked beans for external services. All Test classes use the same set of context configuration files and things are well 90% of the times.

But in some cases, there would be a bean which I would like to mock. But I do not wish to edit the spring-text.xml (as it will disturb all classes), nor do I wish to have separate set of xml's for each test class. One very simple say of doing it would be:

@Autowired
@Qualifier("fundManager")
FundManager fundManager;

@Test
public void testSomething(){
    TransactionManager tx = mock(TransactionManager.class);
    fundManager.setTransactionManager(tx);
    //now all is well.
}

This works in some cases. But sometimes, it is desired that this new temporary bean tx should be set where ever TransactionManager bean was being used all across the code base.

Proxy class IMHO is not a great solution, because I would then have to wrap all the beans with a wrapper. This is what I am ideally looking for:

@Test
public void testSomething(){
    TransactionManager tx = mock(TransactionManager.class);
    replace("transactionManagerBean",tx);
    //For bean with id:transactionManagerBean should be replace with `tx`
}

BeanPostProcessor looks like an alternate suggestion but I have faced a few hiccups with it.

Aucun commentaire:

Enregistrer un commentaire