开发者

Acquiring the class object of the class which was annotated with @ContextConfiguration?

This problem isn't a showstopper, but I've been wondering if it is possible to acquire the class object of the class (the test class) which was annotated with @ContextConfiguration("mycontext.xml") from a bean's code defined in mycontext.xml?

The motive:

In my current project I keep quite开发者_如何学编程 a few test spring contexts, which have become more and more similar to eachother over the months (so instead of fine-tuning each, I've started just pulling in everything lazily). It has come to a point where they mostly differ only in the database initialization script(s) they run with (if they differ at all to begin with). So I was thinking of a neat way of getting rid of all the context xmls which contain nothing but an import and and an init-db tag.

The solution I'm primarily looking for:

Annotate the unit test classes with an annotation which somehow sets the paths to the db init scripts I'd like to run for the test cases. Injecting property-placeholder value(s) would more or less do it, but it would be nice to be able to run 1..n db scripts.

I recon that with BeanFactoryPostProcessors and BeanPostProcessors a lot can be achieved, but for starters, how do I acquire the magic annotation I put on my test class?

I hope this post makes some sense, any input is welcome :)


I you use JUnit, then there is an other Pitfall that you mayby not have considered yet. Junit create an NEW instance of the test class for EACH test case!

I have the feeling that you try do do the whole stuff in the wrong way, or lets say a to complicated one.

Why not have a small spring configuration fill for each db script that include the core spring configuration and also reference the db init script. Then you can use the @ContextConfiguration for that specific configuration.

Example

junit test case

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration("classpath:/META-INF/spring/config1.xml")
public class MyTest {...}

config1.xml

...
<import resource="classpath:/META-INF/spring/applicationContext.xml" />
<bean class="MyDbScript">
   <property name="file" value"classpath:/SCRIPTS/config1.sql" />
</bean>
...

An other solution would be not automatically setup the database on startup, but after an invocation. I am not 100% sure if its work, because I do not know if @Before annotated test methods run before or after Spring has injected the beans in the test case.

Assume you have a bean of class HandTriggeredDbScriptExecutor that has a method run which takes the script to run:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration("classpath:/META-INF/spring/applicationContext.xml")
public class MyTest {

    @Autowire
    HandTriggeredDbScriptExecutor handTriggeredDbScriptExecutor;

    @Before
    public void setUpDb() {
       this.handTriggeredDbScriptExecutor.run("classpath:/SCRIPTS/config1.sql");
    }        
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜