Spring Test integrates JUnit 4 with

Source: Internet
Author: User

After two days of web development and discovering object management through spring, testing became complicated. Because all the beans need to be loaded in the Applicationcontext.xml, and then through the @resource to obtain. It is inefficient and cumbersome to test the entire business flow every time it is done. It's too tedious to write a test class alone. So I think spring has a test framework that can integrate junit for testing, so take it out and try it out.

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, 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, you use the Abstracttransactionaljunitspringcontexttests class, Please include the TransactionManager bean in the Applicationcontext.xml file:

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

If this bean is not 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:applicationContext.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

Appendix 1: Overall Test class files
/* @ (#) Userdaotest.java
*
*/
Package Com.phj.dao;

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;

Import Com.phj.entity.User;

@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations = "Classpath:applicationContext.xml")
public class Userdaotest extends Abstractjunit4springcontexttests {

@Resource
Private Userdaointerface Userdao;

@Test
public void Savetest () {

User User1 = new user ();
User1.setusername ("Tom");
User1.setpassword ("123456");
User1.setnickname ("Tom");
User1.setemail ("[email protected]");

User User2 = new user ();
User2.setusername ("admin");
User2.setpassword ("123456");
User2.setnickname ("admin");
User2.setemail ("[email protected]");

User User3 = new user ();
User3.setusername ("Feihong");
User3.setpassword ("123456");
User3.setnickname ("PhJ");
User3.setemail ("[email protected]");

Userdao.save (user1);
Userdao.save (User2);
Userdao.save (USER3);
}
}

OK, so you can use the Spring test framework.

Spring Test integrates JUnit 4 with

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.