初次学习适用IDEA 配置Testng
这是一遍我从网上度过的一篇内容为了方便自己学习,于是将它转来我的博客中
1.IDEA安装TestNG

若IDEA已经安装TestNG的插件,显示如下

若没有,则搜索TestNG,进行下载

2.创建单元测试方法
1.打开需要进行单元测试的方法,选择类名,点击Alt+Enter键,选择Create Test

第一次创建单元测试方法,可能会提示“TestNG library not found In the module”,是因为没有在pom.xml文件中,写入TestNG插件的依赖,点击下图的“Fix”按钮,即可自动添加



setUp/@Before:创建@Before方法
tearDown/@After:创建@After方法
Show inherited methods:为父类的方法创建单元测试
2.规范单元测试的存放路径

output directory:${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}


因为Springboot框架的mapper文件放在了scm-bms-starter下面,将Controller层的单元测试方法放在scm-bms-web下,加载mapper文件失败,所以,将test方法转移到scm-bms-starter

package com.gw.scm.bms.web.controller;
import com.gw.scm.bms.BmsApplication;
import com.gw.scm.bms.commons.entity.BmsAccurateBomUpdateParamsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
@SpringBootTest(classes = {BmsApplication.class}, properties = {"classpath:*.yml", "classpath:*.xml"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BmsAccurateBomControllerTest extends AbstractTestNGSpringContextTests {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testCreate() {
BmsAccurateBomUpdateParamsEntity model = new BmsAccurateBomUpdateParamsEntity();
model.setVehicleMaterialId(12L);
model.setMaterialId(112L);
ResponseEntity<String> entity = this.restTemplate.postForEntity("/BmsAccurateBom/create", model, String.class);
System.out.println(entity.getBody());
}
}
————————————————
版权声明:本文为CSDN博主「月月吃喝」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43840640/article/details/89083030
最后我再推荐一个微信公众账号:小海购FL(shenghuohui2021)实时更新淘宝优惠券,天猫优惠券,拼多多,京东优惠券直接领取平台,先领劵在购物不仅自购省钱,还能分享赚钱。