springboot+Junit测试rest接口,报错显示url无法连接
来源:互联网 发布:excel同一列重复数据 编辑:程序博客网 时间:2024/06/08 16:05
代码很简单,因为只是测试路径嘛!!!
看代码:
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.TestRestTemplate;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.web.client.RestTemplate;import com.pcitc.domainname.servicename.Application;@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = Application.class)@WebAppConfigurationpublic class DemoApplicationTests{ private RestTemplate template = new TestRestTemplate(); /** * @author PQF */ @Test public void testMasterDataControllerQueryMasterDataByCode(){ try { String url = "http://localhost:"+8081+"/v1/masterdata/0501080060000116"; String result = template.getForObject(url, String.class); System.err.println(result); } catch (Exception e) {e.printStackTrace();} }}
于是各种上网找答案,没人告诉你是为什么。
今天我就写下这个答案。那是因为你没有启动主类,什么是主类???就是下面这个:
@RestController@SpringBootApplication@SpringBootConfiguration@EnableCachingpublic class Application{public static void main(String[] args) throws Exception {SpringApplication.run(Application.class);}}
你要先启动主类,然后再启动Junit的测试类才可以。以上就是要注意的事项。
阅读全文
1 0
- springboot+Junit测试rest接口,报错显示url无法连接
- springboot之SpringJUnit4ClassRunner测试Rest接口
- SpringBoot中的Junit测试
- SpringBoot junit测试
- junit测试@Cacheable报错
- Junit 文件URL报错解决办法
- Junit和Spring、SpringBoot测试
- junit 测试报错:invalid project specied
- Junit进行spring+jpa测试报错
- java junit测试quartz job报错
- junit测试报错:class not found
- ant + junit 接口测试
- java URL Rest风格接口
- springboot项目连接mysql数据库报错
- springboot的junit测试和多个模块cxf暴露接口问题
- Springboot 框架实现rest接口风格
- springboot下配置junit测试环境
- (3)学习SpringBoot 之 Junit测试
- 什么是:android-adt;sdk;api;adb
- Eclipse下如何打开Servers窗口
- java基础知识总结。
- iOS开发多线程介绍
- cmd命令行
- springboot+Junit测试rest接口,报错显示url无法连接
- ECharts 多个折线图动态获取json数据
- LeetCode 34. Search for a Range (Python)
- 【HDU-1878】 欧拉回路
- Android textview文字两端对齐,中间空白
- 详述undefined和null的区别
- linux c下目录文件的操作(opendir,readdir,closedir)
- 安卓简化版圆形图片控件CircleImageView
- [RK3288][Android6.0] 不同分辨率的bootanimation.zip下载