1

I am working on an established application that has no Unit testing. I want to start writing test cases for this application. Bean Mocking is non-existent and it will take me considerable time to set it up. So for the sake of getting started quickly and since we do not have any test cases at all, I am thinking of setting up Integration testing and once I am comfortable with all the test coverage, I will slowly move on to converting it to true Unit testing(by mocking). Since the application is big and loading up the spring container takes considerable amount of time, I want some suggestions on increasing the turn around time. I can think of few ways of doing it.

  • Have some lightweight spring container running all the time and run all the unit test cases against this lightweight container.(or have access to its applicationContext)

  • Run the test cases against the actual server.(Run Junit remotely from your IDE)

  • Utilize Spring Junit configuration and somehow prevent reloading the context for each individual test cases.

I am sure that this use case would have come up before, any insight is highly appreciated.

2 Answers 2

0

Context caching is a built-in feature of Spring, so if your test cases use the same configuration file (or set of files) Spring won't repeatedly reload the context. Review the Context management and caching section of the reference docs:

By default, once loaded, the configured ApplicationContext is reused for each test. Thus the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term test suite means all tests run in the same JVM.

2
  • But if we want to individually run each of your unit test cases, the application context is lost. The only way it works is when you run a whole suite of Junit test cases. You lose the ability to unit test and this methodology is completely useless when it comes to TDD.
    – billygoat
    Jun 6, 2013 at 20:56
  • @doc_180 It sounds like your problem isn't just the unit tests. It's the time it takes to start Spring. You must face this problem for running your app locally for development too. You should investigate finding ways to reduce the speed of starting Spring. Add logging and find what takes so much time. This will be the easiest solution.
    – jasop
    Jun 7, 2013 at 2:12
0

When running a test in Spring you can point the test to what application context configuration you want the test to run with. So you don't have to use your production application context, you can make special configs for testing. I personally have an "integration test application context" and an "unit test application context". But you could break this down even further.

Easiest way to set this up is to set the application context on super classes, and have each test extend from one of them.

eg. setup:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:BaseSpringIntegrationTestContext.xml")
public abstract class BaseSpringIntegrationTest {

and

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:BaseSpringUnitTestContext.xml")
public abstract class BaseSpringUnitTest {

then for testing

public class BlahTest extends BaseSpringUnitTest  {

The next step would be to work out how you can speed up the starting of your spring context. Certain large components may not need to be loaded at all for some of your tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.