网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何中SpringBoot中添加一个单元测试

这篇文章将为大家详细讲解有关如何中SpringBoot中添加一个单元测试,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

成都创新互联是一家集网站建设,安宁企业网站建设,安宁品牌网站建设,网站定制,安宁网站建设报价,网络营销,网络优化,安宁网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

首先我们来添加单元测试所需要的jar

 
  org.springframework.boot 
  spring-boot-starter-test 

接着我们写了一个单元测试的demo

package com.zkn.learnspringboot.service.test; 
 
import com.zkn.learnspringboot.FirstExample; 
import com.zkn.learnspringboot.service.PersonService; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
 
/** 
 * Created by wb-zhangkenan on 2016/11/18. 
 */ 
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes = FirstExample.class) 
public class TestService extends BaseTestService{ 
  @Autowired 
  private PersonService personService; 
  @Test 
  public void testSys() { 
    System.out.println(personService.getPersonDomain().toString()); 
  } 
 
}

然后我们run一下,一个单元测试就搞定了。

另外:@RunWith和@SprintBootTest这两个注解上都有@Inherited这个注解,所以我们可以定义一个单元测的父类,然后所有的单元测试类继承这个父类就行了。如下所示:

package com.zkn.learnspringboot.service.test; 
 
import com.zkn.learnspringboot.FirstExample; 
import org.junit.runner.RunWith; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
 
/** 
 * Created by zkn on 2016/11/20. 
 */ 
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes = FirstExample.class) 
public class BaseTestService { 
 
}
package com.zkn.learnspringboot.service.test; 
 
import com.zkn.learnspringboot.service.PersonService; 
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
 
/** 
 * Created by wb-zhangkenan on 2016/11/18. 
 */ 
 
public class TestService extends BaseTestService{ 
  @Autowired 
  private PersonService personService; 
  @Test 
  public void testSys() { 
    System.out.println(personService.getPersonDomain().toString()); 
  } 
 
}

如果你用的SpringBoot是1.4.0之前的话,所用的注解稍有不同。你需要把@SpringBootTest注解换成@SpringApplicationConfiguration和@WebAppConfiguration。

关于如何中SpringBoot中添加一个单元测试就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站题目:如何中SpringBoot中添加一个单元测试
标题路径:http://bjjierui.cn/article/pgscce.html

其他资讯