Latest videos

Categories > JUnit tutorial > JUnit & Spring, integration testing

JUnit & Spring, integration testing

Annotation:

Unit tests are great, but they cover only part of your code. Another interesting category of tests is integration testing. Using integration tests you can test collaboration of objects, methods for accessing database etc.

If you use Spring, it's very easy. In Spring you have a Service or Repository like this service class. Now I want to test these methods.

First I must add Spring runner using: @RunWith(SpringJUnit4ClassRunner.class). Next I must specify where is XML configuration file on disc using @ContextConfiguration. I also strongly suggest to add @Transactional annotation to your test suite. Thanks to this annotation after each test will be automatically called rollback. You can also call rollback on your own using @Rollback. This test suite is now like a Spring bean, you can use annotation @Autowired to inject Spring beans.

comments powered by Disqus

Recommended materials

Share / Subscribe

Search

Free online web Java example projects

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:

github

Copyright

Created by , professional Java and Oracle DB trainer. All Rights Reserved.

Other projects

This web site is monitored by FOSS Site monitoring application

My GitHub account