Vojtěch
Vojtěch

Reputation: 12416

Integration tests of Spring application

I am trying to implement integration tests for my Tomcat application, but my issue is that the application is launched separately from the tests so the tests cannot access the application context and neither the database.

My idea is running the tests "within" the running application, so I can @Autowire EntityManager and check for instance the state of the database during testing or even create database entities for testing.

My only idea of doing this is to actually run the application programmatically from the tests as ClassPathXmlApplicationContext("applicationContext.xml") and the access the Context. This would work, but it would be very hard for debugging as we wouldn't be able to use Hotswapping during the testing. Also I guess the server would be stopped as soon as the tests would end. I guess that is not the best and correct solution.

EDIT:

My question was probably unclear, so I will try to clarify.

I have a Tomcat application with Spring and Hibernate. The Spring beans and Hibernate database connection is initialised when the Tomcat application is started. The issue is how to run the tests of the active Spring beans from methods annotated with @Test in src/test/java which are started separately.

Consider this class:

@Component
class MyRepository {

    @Autowired
    EntityManager em;

    @Transactional
    public void myMethod(MyEntity entity) {
        // do some job with entity
        ...
        em.flush();
    }
}

This class will be initialised with Tomcat as a MyRepository bean.

To test it, I cannot just call new MyRepository().myMethod(...) - I need to access the bean. The issue is accessing the bean from the @Test method:

@Test
void testMyRepository() {
    Item item = ...
    // then use the repository to handle the entity 
    context.getBean(MyRepository.class).myMethod(item);

    // then assert the state of the database
    context.getBean(EntityManager.class).find(Item.class, ...) ...
}

I can probably get the context in the initialisation of the tests with

ApplicationContext context = ClassPathXmlApplicationContext("applicationContext.xml");

But it would mean launching the whole application each time the tests are started. The better solution would be if the application could run separately from the tests.

Hope my problem is more clear now.

Upvotes: 0

Views: 900

Answers (2)

Ján Halaša
Ján Halaša

Reputation: 8421

I would suggest you to use the SpringRunner to start the Spring application context and perform your tests on that running instance. You can customize the context the way it doesn't contain parts you don't want to tests and you can create mocks for components that require some external resources (REST clients and such). Take a look at the Spring docs or Spring Boot docs.

If multiple tests use the same Spring context configuration, the context is started just once and reused. So it's good to have it's configuration in a parent class of your tests. You can autowire any Spring bean into your test and test it.

You can use an in-memory database (such as H2) instead of a production one, so your tests are not dependent on an external infrastructure. To initialize the database, use tools like Flyway or Liquibase. To clear the database before each test, you can use the @Sql annotation.

You can find many examples of projects with such tests, for example my own demo.

If you want to test an external system, I would suggest something like JMeter.

Upvotes: 2

David Fischer
David Fischer

Reputation: 75

Unfortunately you cant mirror your classes and use them in your tests. Thats a big disadvantage of web services. They always depend on user / machine interaction. With a lot of effort you can extract the functionality of the essential classes or methods and construct test scenarios etc. with jUnit. The Overview of your possibilities:

  • special drivers and placeholders
  • you can use a logger with detailed log-level and file output. Then you created scenarios with the expected result and compare it with your log files.

  • Capture replay tools. They record your exection and replay them for monitoring.

  • I can also recommend using Selenium for the frontend tests.

Hope it helped.

Upvotes: 0

Related Questions