JWordPress前台项目实战:如何编写单元测试

发表于:2021-3-05 09:28

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:mb5ffd6fed5661e    来源:51CTO博客

分享:
  写在前面
  如何在springboot应用中编写单元测试代码呢?带着这个疑问我们走进今天要讲解的主题,在springboot应用中编写junit单元测试代码实现单元测试,下面让我们开始吧!
  代码
  引入pom.xml
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  </dependency>
  测试的通用父类
  /**
  * MIT License
  * Copyright (c) 2018 haihua.liu
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the “Software”), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
  * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
  package cn.liuhaihua.web;
  import org.junit.runner.RunWith;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringRunner;
  /**
  * @ClassName: BaseTest
  * @Description: 测试的基础类
  * @author Liuhaihua
  * @date 2018年7月2日
  *
  */
  @RunWith(SpringRunner.class)
  @SpringBootTest(classes = JWordpressWebApplication.class)
  public class BaseTest {
  }
  测试代码
  /**
  * MIT License
  * Copyright (c) 2018 haihua.liu
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the “Software”), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
  * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
  package cn.liuhaihua.web;
  import org.junit.Assert;
  import org.junit.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import cn.liuhaihua.web.model.WpUsers;
  import cn.liuhaihua.web.service.WpUsersService;
  /**
  * @ClassName: WpUsersTest
  * @Description: 用户测试类
  * @author Liuhaihua
  * @date 2018年6月29日
  *
  */
  public class WpUsersTest extends BaseTest{
  @Autowired
  private WpUsersService wpUsersService;
  /**
  * @Title: getUserDetail
  * @Description: 测试获取用户信息方法
  * @param 参数
  * @return void 返回类型
  * @throws
  */
  @Test
  public void getUserDetail() {
  WpUsers user =wpUsersService.getUserDetail(1l);
  Assert.assertEquals(“Harries”, user.getDisplayName());
  }
  }
  测试
  运行单元测试代码:

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号