spring boot test

依赖包的引入: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());
		}
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值