6.2 Single bundle integration testing

One of the most common forms of integration testing is ensuring that the object relational mapping in an application is working properly. This kind of testing typically uses a data access object to retrieve data from a live database. In this step a test case for the JpaDirectory class is created.

Open the greenpages.jpa.JpaDirectorySpringContextTests class in the src/test/java source folder of the greenpages.jpa project. This class contains a method that uses JUnit to test that a search completes correctly. Rather than instantiate this class directly in the test, the Spring Test Framework is used to instantiate and inject a JpaDirectory based on the META-INF/spring/module-context.xml file.

Add Spring Test Framework declarations to the test class. These declarations run the test with the SpringJunit4ClassRunner and configure the test with the classpath:/META-INF/spring/module-context.xml file:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring/module-context.xml")
@TestExecutionListeners(value = DependencyInjectionTestExecutionListener.class)
public class JpaDirectorySpringContextTests {
…

Use Eclipse to suggest the necessary imports until there are no errors.

When this configuration is complete, click on the Run drop-down menu and select Run Configurations…. In the the dialog that opens select JUnitJpaDirectorySpringContextTests and press Run.

This test run will fail because there is no DataSource bean to be injected; it is typically sourced from the OSGi service registry at runtime:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No bean named 'dataSource' is defined

The next step will correct this error.