springboot下的Junit单元测试

前言

Junit是一个Java语言的单元测试框架,被用于对应用程序的单元测试。

Junit常用注解

@Before:初始化方法 
@After:释放资源
@Test:测试方法,在这里可以测试期望异常和超时时间
@Ignore:忽略的测试方法 
@BeforeClass:针对所有测试,只执行一次,且必须为static void
@AfterClass:针对所有测试,只执行一次,且必须为static void
@RunWith:指定使用的单元测试执行类

pom.xml添加依赖

<dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
</dependency>

DEMO实例

1、业务方法如下:

public static String sendPostDataByJson(String url, JSONArray headerArr, String jsonbody, String encoding) throws ClientProtocolException, IOException {}

2、添加@Test注解,实现对sendPostDataByForm的单元测试

@Test
public void testSendPostDataByJson() throws IOException {
  String url = "https://coros-api.weloop.cn/coros//user/login";
  Map<String, String> map = new HashMap<String, String>();
  map.put("account", "/dM14gEwtXWB0PTJfT8hXYmUVn2JHeguaoi2hYph6X0=");     
  map.put("accountType", "2");
  map.put("deviceList", "[\"341F2E\",\"738C2F\"]");
  map.put("pwd", "g4jA2hZqN+QMwZcZ7s3lEDs0hpuXhaUUG23Cowi5A0Zld4hHFNeDKYEFoTSfkM1Q\\n"); map.put("clientType", "1");
  map.put("appKey", "8066728178907179");
  JSONArray headerarr =new JSONArray();
  String body = sendPostDataByJson(url,headerarr, JSON.toJSONString(map), "utf-8"); System.out.println("响应结果:" + body);

执行单元测试

鼠标右键可调出菜单选择执行


15065120-f1b19596f4f3de5e.png
执行示例

猜你喜欢

转载自blog.csdn.net/weixin_33691817/article/details/87093843