springboot+Junit测试rest接口,报错显示url无法连接
2017-08-02 16:52
1036 查看
代码很简单,因为只是测试路径嘛!!!
看代码:
运行DemoApplicationTests后,打断点,发现url报错,说是无法连接?????
于是各种上网找答案,没人告诉你是为什么。
今天我就写下这个答案。那是因为你没有启动主类,什么是主类???就是下面这个:
你要先启动主类,然后再启动Junit的测试类才可以。以上就是要注意的事项。
看代码:
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) @WebAppConfiguration public 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(); } } }
运行DemoApplicationTests后,打断点,发现url报错,说是无法连接?????
于是各种上网找答案,没人告诉你是为什么。
今天我就写下这个答案。那是因为你没有启动主类,什么是主类???就是下面这个:
@RestController @SpringBootApplication @SpringBootConfiguration @EnableCaching public class Application{ public static void main(String[] args) throws Exception { SpringApplication.run(Application.class); } }
你要先启动主类,然后再启动Junit的测试类才可以。以上就是要注意的事项。
相关文章推荐
- springboot的junit测试和多个模块cxf暴露接口问题
- Spring boot 中的事务管理,并基于junit进行测试
- iOS企业版(Enterprise) App发布,Safari打开URL显示无法连接"xxx"解决办法
- Spring Boot Junit 测试Controller
- 解决spring boot中rest接口404,500等错误返回统一的json格式
- Spring Boot REST API 自动化测试
- spring boot rest接口自动生成文档(包含swagger)
- spring boot(3)-Rest风格接口
- Spring Boot使用AOP实现REST接口简易灵活的安全认证
- spring3.0.2 junit 接口测试问题
- springboot下配置junit测试环境
- SpringBoot建立父pom和JUnit代码测试
- springboot + mybatis +junit 测试用例编写
- 运用RestExpress+Springboot实现web接口
- Spring Boot通过Mybatis,使用mapper接口和xml配置sql,连接数据库
- rest-assured : Restful API 测试利器 - 真正的黑盒单元测试(跟Spring-Boot更配哦,更新至spring-boot1.4.1)
- spring boot 中访问 REST 接口
- Spring Boot实战之Rest接口开发及数据库基本操作
- 使用JUnit 对 Spring Boot REST API 执行单元测试
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)