Saturday, August 10, 2013

Testing the Spring Service layer through JUnit

Testing the Spring Service layer through JUnit

Steps to enable Junit test for spring service layer

This post will explains the steps required to test the spring service layer through JUnit without deploying to the server.

Right Click on the service class for which the test class needs to be created and click on New and select JUnit Test Case


Click on Next and provide the Test Class Name and the package to which the test class has to be created.


Click on Next and select the methods for which the test methods needs to be created and click on finish.


Now the test class is generated,we have to add the below configuration on the test class.

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback = false)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/config/application-context.xml" })
@WebAppConfiguration



Autowire the service class to the test class. Make sure all the required entity classes are added into persistence.xml file, if not we will receive class not found exception during execution.


Change the test methods to invoke the service class methods by providing the required input data.Now the test class can be executed.




No comments:

Post a Comment