0

I am trying to implement Spring integrations tests and I am thinking about two ways to go:

  1. For each test have different configuration and initialise only necessary beans for given test. This should mean the tests are isolated and the Spring context should initialise quite quickly as it doesn't require two many dependencies. I would use this to ignore unitialised beans:

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
        <property name="requiredParameterValue" value="false" />
    </bean>
    

    The disadvantage is having separate applicationContext.xml for every single test. The duplicity is especially unpleasant as 80% of the config would be the same for every test. Is there a way to define the beans to initialise directly in the test without having to have separate xmls with configuration?

  2. To initialise the full application with all the beans would simplify the process of testing as we wouldn't have to cherry-pick the beans and just run it. This would however take much longer to start and restarting the app for every class of test will take a long time. The solution would be if the app could start before running of all tests and if then it wouldn't be restarted for each test class. Is this feasible?

Can you comment on both approaches, which is more standard and more suitable?

Vojtěch
  • 9,438
  • 25
  • 81
  • 138
  • 1
    Why would you need to restart the application between tests? Find another way to (re)use the data. Spring will only only start the application once and reuse it for different tests by default (so basically it feels to me as if you are working around the framework). – M. Deinum Jan 29 '18 at 07:31
  • Is this really that clever by default? It is strange to me having classes `TestSomethingIT` and `TestSomethingElseIT` each of them having annotation `@RunWith(SpringRunner::class)` that it will recognize they are both having same `applicationContext.xml` in `@ContextConfiguration` and so it won't run the application twice, once for each test class. – Vojtěch Jan 29 '18 at 13:03
  • 1
    Yes it is that clever by default. – M. Deinum Jan 29 '18 at 19:14

1 Answers1

1

As M. Deinem pointed out...

Yes, the Spring TestContext Framework is that clever, and it has been ever since I introduced it back in 2007. ;-)

Context caching is actually one of the main features of the Spring TestContext Framework.

See the Context Caching section of the Testing chapter in the Spring Reference Manual for details.

Sam Brannen
  • 24,249
  • 2
  • 75
  • 114