White-box testing of JUnit perfect combination of SpringTest

You can verify the validity of the unit test white-box function basic procedures to ensure the quality of the entire system, power in a moment, benefit future. Currently more than 80% of the company's background or based on java, in particular, uses a lot of background Spring framework, we used here Junit and SpringTest do white-box unit testing. Prerequisite need to understand the basics of java and maven Spring and knowledge, which is measured open interview frequently asked Spring reason.

We tested the demo directly to the test procedure

The first step: JUnit introduction and SpringTest

JUnit named knowledge Italy, is dedicated to providing a framework for unit testing for Java language. The usual development process, unit tests will be used frequently.

Spring development framework is a background, on the JUnit, unit testing white box for convenience, and made further Spring package, the test module is integrated SpringTest. Rollback comprising dirty data, so the transaction can easily support.

SpringTest use to avoid frequent repetition of the container load, this use will have a good experience. It also did not like JUnit by getBean () method to obtain the target container element, directly as in the actual development, good configuration, written label can get the desired results.

SpringTest also made a deal with the rollback of database operations, this test very useful in everyday use, avoid frequent changes made to the database in order to ensure the normal environment. And so many, SpringTest can let us in on the powerful JUnit unit testing more convenient, they need to take a look at the dependencies, open pom file, add the following code dependencies inside.

 


 

 

Step two: JUnit integration with SpringTest

Create a test class called SpringTestUser, the code is as follows

 

@RunWith (SpringJUnit4ClassRunner.class) say that we actually use this code or JUnit testing.

@ContextConfiguration ({ "classpath *: applicationContext.xml"}) Load Profile


 

applicationContext.xml is to develop profiles

@Resource是spring注入开发的类

@Test是 junit 注解

右键运行

 


 

可以看到单元测试效果

 

对测试数据测试的回滚啊,仅通过一个标签就可以实现

作  者:Testfan 极光

出  处:微信公众号:自动化软件测试平台

版权说明:欢迎转载,但必须注明出处,并在文章页面明显位置给出文章链接

Guess you like

Origin www.cnblogs.com/testfan2019/p/11573322.html