springboot+Junit测试rest接口,报错显示url无法连接

代码很简单,因为只是测试路径嘛!!!

看代码:

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的测试类才可以。以上就是要注意的事项。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值