依赖包的引入:pom.xml中仅依赖spring-boot-starter-test,它把相关的依赖全部引入。
在测试类上的注解
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
controller测试:
package com.cnheshun.test;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.cnheshun.common.web.Result;
import com.cnheshun.zzdsm.DsmServerApplication;
import com.cnheshun.zzdsm.modules.sys.entity.Dict;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DsmServerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyTest {
@LocalServerPort
private int port;
private URL base;
@Autowired
private RestTemplate restTemplate;
@Before
public void setup() throws Exception {
String url = String.format("http://localhost:%d/m", port);
System.out.println(String.format("port is : [%d]", port));
this.base = new URL(url);
}
@Test
public void test1() throws Exception {
Dict dict = new Dict();
dict.setType("industry");
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInVzZXJJZCI6IjEiLCJuYW1lIjoi57O757uf566h55CG5ZGYIiwiZXhwIjoxNTM2ODAzMTczfQ.P6oH0ZDBn0Q8wlJBw4PMwp-EdjOyIbYytubqbX8wWfyn80H2TbEv5GML7b2iuSJLuXF6wVdMn3DWBt442gbC3tudvpsOB0w9DPhpyFP14C6W2af1fRE8Yucdxk2zHyICeUkqcrZIIrIumdbfbzg2jVHLc1Pp-hl3V3YhXqyWqq8");
HttpEntity<String> entity = new HttpEntity<String>("",headers);
ResponseEntity<Result> response = this.restTemplate.postForEntity(
this.base.toString() + "/sys/dict/getByType",entity, Result.class, dict);
System.out.println(String.format("测试结果为:%s", JSON.toJSONString(response.getBody())));
}
}
service 测试:
package com.cnheshun.test;
import java.util.List;
import java.util.Map;
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.SpringRunner;
import com.cnheshun.zzdsm.DsmServerApplication;
import com.cnheshun.zzdsm.modules.sys.entity.Org;
import com.cnheshun.zzdsm.modules.sys.service.MenuSrv;
import com.cnheshun.zzdsm.modules.sys.service.OrgSrv;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DsmServerApplication.class)
public class ServiceTest {
@Autowired
private MenuSrv menuSrv;
@Autowired
private OrgSrv orgSrv;
@Test
public void test() throws Exception{
Map<String, Object> map = menuSrv.getMenus(1l);
for(String key : map.keySet()) {
System.out.println(key);
System.out.println(map.get(key).toString());
}
}
@Test
public void test2() throws Exception {
List<Org> list = orgSrv.findByUserId(1l);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
}
}