Integration testing

Integration testing is also known as system testing; it tests the interactions of multiple units working together. All units should work well individually as we've already performed unit testing to confirm that, and integration testing involves testing application classes in the context of their surrounding infrastructure without running the entire project. Use the Apache DBCP connection pool instead of a container-provider pool obtained through JNDI, and use ActiveMQ to avoid expensive commercial JMS licenses.

Let's see the following diagram about integration testing:

As you can see in the preceding diagram, AccountServiceImpl is using the actual AccountRepository implementation instead of its stub implementation as we have used in the unit tests. But JpaAccountRepository will fetch data from the testing DB instead of the production DB. Spring supports integration testing using the Spring-test.jar library, and Spring can use the same application configuration for the testing environment to inject dependencies between the application components.

Spring provides a separate module (spring-test.jar) for integration testing and consists of several JUnit test-support classes. Spring has a central support class, which is SpringJUnit4ClassRunner. It caches a shared ApplicationContext across test methods.

Let's see the following class with integration testing:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes=SystemTestConfig.class) 
public final class AccountServiceImplTest { 
    
   @Autowired 
   AccountService accountService; 
 
   @Test  
   public void findAccountByAccountId() { 
   assertTrue(accountService.findAccountByAccountId(100).getBalance().intValue() == 121); 
   } 
    
   @Test  
   public void findAllByCustomerId() { 
         assertFalse(accountService.findAllByCustomerId(1000).size() == 
3); } }

The preceding test class, AccountServiceImplTest, is annotated with the @RunWith annotation by passing the SpringJUnit4ClassRunner class; it indicates this test will run with Spring support. The @ContextConfiguration annotation is used to include the testing configuration and the SystemTestConfig class points to the system test configuration file. You may notice that we didn't use the @Before annotation here because the AccountService dependency will be injected by Spring, so we don't need to use the @Before annotation.

In addition to loading the application context, SpringJUnit4ClassRunner also makes it possible to inject beans from the application context into the test itself via autowiring. Because this test is targeting an AccountService bean, it is autowired into the test. Finally, the findAccountByAccountId() method makes calls to the address service and verifies the results. As we've looked at integration testing, let's look at the benefits of testing with Spring.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.55.198