Spring Junit Test

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:/applicationContext.xml") //or something like "file:WebContent/WEB-INF/applicationContext.xml"
@TransactionConfiguration(defaultRollback=true, transactionManager="transactionManager")//this ensure that spring would rollback the transaction so that the test will not change the database. @Transactional
public class YourTest { 
  @Autowired
  private  MenuDao md;
     public void someTest() {     } }

  • When there are multiple test cases, make the above class an abstract one eg->BaseTest.
  • Make all your test classes extend BaseTest (or alternatively, add those annotations on all classes)

A article for Database integration testing / unit-testing with Spring, JPA, JUnit and Maven

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s