Sunday, August 28, 2011

Spring: Unit Test, Reload Context After Each Test Or Test Class

Recently I came across a situation while doing some unit test using spring SpringJUnit4ClassRunner where after each test, the spring application context is no longer good for it to be reused in the next test. After some digging, I found out that there is this DirtiesContext annotation which one can use in the unit test to force spring to reload context.

It has two options:

(1) One is reload context after each test.
(2) Second is to reload context after all the tests of a class.

This is very useful and it also means that when running unit test using spring, by default the application context is loaded once and used to run all the tests.

Promote your blog