Unit testing using JUNIT4 Spring-test in Java SSH framework

Source: Internet
Author: User

Due to the fact that the bean is given to spring management under the framework of SSH, it is more difficult to do unit test, and JUNIT4 to introduce annotations easily.

1. Join the dependency Package

Using Spring's test framework requires adding the following dependency packages:

    • JUnit 4 (Official download: http://www.junit.org/)
    • Spring Test (Test package in spring framework)
    • Spring related other dependencies (no more, it's the context package)
2. Create test source catalogs and packages

  Here, it is recommended to create a source file directory with SRC peers, because the classes within SRC are prepared for future products, and the classes here are for testing purposes only. and the name of the package can be the same as the directory in SRC, so because in the test source directory (source file), so there is no conflict, and the name is identical, more convenient to retrieve.

3. Create a test class

  Create a test class with the recommended name "tested class name + Test".

  The test class should inherit with abstractjunit4springcontexttests or abstracttransactionaljunit4springcontexttests

  For abstractjunit4springcontexttests and Abstracttransactionaljunit4springcontexttests classes, choose from:

If you need to use transaction management in your test class (for example, to roll back the test content after the test results have been rolled out), the Abstracttransactionaljunit4springtests class can be used. The use of transaction management is the same as the normal use of spring transaction management. It is important to note that if you want to use declarative transaction management, that is, using the Abstracttransactionaljunitspringcontexttests class, in the Applicationcontext.xml file Add the TransactionManager bean:

<bean id= "TransactionManager"
class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

If no such bean is added, Nosuchbeandefinitionexception will be thrown, indicating no bean named ' TransactionManager ' is definded.
 

4. Configuring the Test class

  Add the following in front of class to configure the location of the Applicationcontext.xml file.

@RunWith (Springjunit4classrunner.  Class

@ContextConfiguration (locations={"Classpath:/config/applicationcontext-*.xml", "classpath:/config/ Applicationcontext-beans-*.xml "," Classpath:/config/applicationcontext-beans.xml "})

5. Create a test method

  To create a test method, the recommended name is "test method name + test".

  Add @Test above the test method

6. Execute via JUnit 4

  Right-click the method name and select "Run as" → "JUnit Test" to


Package com.zjpost.bank.service;


Import Javax.annotation.Resource;


Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.test.context.ContextConfiguration;
Import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath:/config/applicationcontext-*.xml", "classpath:/config/ Applicationcontext-beans-*.xml "," Classpath:/config/applicationcontext-beans.xml "})
public class Mbankdetailicbcserviceimpltest extends
abstractjunit4springcontexttests {
@Resource
Private Mbankdetailicbcserviceimpl Mbankdetailicbcservice;

@Test
public void Getbankdata () {
Mbankdetailicbcservice.getbankdata ();
}


}

@Resource can specify name or Tpye

Original: http://blog.csdn.net/chinadeng/article/details/16860511

Unit testing using JUNIT4 Spring-test in Java SSH framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.