五、springboot 集成junit

 四、spring boot +mybatis(通过properties配置) 集成 博客中,我已经集成了mybatis,今天这篇博客主要是简单讲解下怎么集成junit。

我看了网上很多帖子都是说的使用@WebIntegrationTest进行接口相关的junit测试,但是我发现在springboot 1.5版本后已经没有这个注解了。

我这边查阅了一些资料,最后采用了注解TestRestTemplate

TestRestTemplate 类是sprigboot专门用作接口调用的类,再测试时无需启动容器,并且带有get、post、put、delete相关方法

我在这里只贴出junit测试类代码,工程代码已上传到码云。

https://gitee.com/yangok/springboot_junit.git

TestUserController

package springbootDemo.junit.app;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
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.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import com.yang.springbootDemo.controller.ApplicationMain;
import com.yang.springbootDemo.entity.UserEntity;

/**
 * 
 * @author liuyang
 *
 * 2018年8月3日 上午10:21:37
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationMain.class, 
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestUserController {
    
    @Autowired
    private TestRestTemplate testRestTemplate;

    
    @Test
    public void getUserTest() throws Exception {
        String name = "admin";
        Map<String,String> multiValueMap = new HashMap<>();
        //传值,但要在url上配置相应的参数
        multiValueMap.put("name",name);
        UserEntity resultUser = testRestTemplate.getForObject("/user/getUser?name={name}", UserEntity.class,multiValueMap);
        Assert.assertNotNull(resultUser);
        Assert.assertEquals(name, resultUser.getName());
        
    }
    
}

c89931a1206642aa8524f5ba3763978927f.jpg

 

 

 

转载于:https://my.oschina.net/yangok/blog/1925903

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值