博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot测试时 SpringApplicationConfiguration注解不能用
阅读量:6922 次
发布时间:2019-06-27

本文共 1381 字,大约阅读时间需要 4 分钟。

测试时,@SpringApplicationConfiguration(classes = Application.class) 报错,注解不能导入。

在学习spring boot时,按照文档学习时测试时,我也是遇到这个问题,看了好多资料,有的说是这个注解在1.4就被替换了,我用的1.5.2版本的,直接用自动生成的两个注解就可以实现测试功能。
@RunWith(SpringRunner.class) @SpringBootTest 测试代码:

package com.didispace.hello;

import static org.hamcrest.Matchers.equalTo;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(SpringRunner.class)

@SpringBootTest
public class HelloAppTests {

//模拟调用controller接口发起请求

private MockMvc mvc;
@Before
public void setUp(){
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@Test
public void hello() throws Exception{
mvc.perform(MockMvcRequestBuilders.get("/hello")
.accept(org.springframework.http.MediaType.APPLICATION_JSON)
).andExpect(content().string(equalTo("hello")))
.andExpect(status().isOk());
}
}

转载于:https://www.cnblogs.com/maohuidong/p/8036269.html

你可能感兴趣的文章
sshd_config 中文手册
查看>>
前端常用工具记录
查看>>
Spring学习总结(4)——Spring AOP教程
查看>>
scanf函数详解(上)
查看>>
SCVMM 2012 R2创建逻辑网络和VM网络
查看>>
SCVMM 2012 R2之添加hyper-v主机
查看>>
pfSense book之用户管理和认证
查看>>
在pfSense中配置Wi-Fi + Lan桥接接入点
查看>>
【译】如何通过简单的3步恢复Windows7同时删除Ubuntu
查看>>
std::call_once
查看>>
鼠标提示文字
查看>>
Oracle数据库升级路线图
查看>>
一个操作系统的实现-笔记
查看>>
JSF标签与参数使用说明
查看>>
[转载] 用心工作,做无可替代的你——致我的助理
查看>>
我的友情链接
查看>>
GIt的备份和恢复
查看>>
JavaScript中识别native方法
查看>>
RabbitMQ系列一:初识RabbitMQ
查看>>
查看zabbix的key是否被支持
查看>>