Java

Recent site activity

Home‎ > ‎

Testing and the Spring Framework

Using the Spring annotations for JUnit 4.x testing

Configuring your JUnit 4.x tests to use Spring and transactions is easy with Spring's annotations.

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
 3         TransactionalTestExecutionListener.class})
 4 @Transactional
 5 @ContextConfiguration(locations = {"classpath:spring-context.xml", "classpath:testing-spring-context.xml"})
 6 public abstract class AbstractTransactionalSpringIntegrationTests {
 7 
 8     @Autowired
 9     protected SessionFactory sessionFactory;
10 
11     @Autowired
12     protected JdbcTemplate jdbcTemplate;
13 
14     @Autowired
15     protected IDatabaseTester databaseTester;
16 
17     protected Session session;
18 
19     @Before
20     public void doBeforeEachTestCase() throws Exception {
21         if (databaseTester.getDataSet() == null) {
22             final InputStream in = Thread.currentThread().getContextClassLoader()
23                     .getResourceAsStream("springmvc/examples/domain/database-fixtures-dataset.xml");
24             databaseTester.setDataSet(new FlatXmlDataSet(in));
25         }
26         databaseTester.onSetup();
27 
28         session = sessionFactory.getCurrentSession();
29     }
30 
31 }
32