微服务架构

SpringBoot Junit 单元测试无法加载src/mai

2020-01-26  本文已影响0人  DongGuangqing

SpringBoot 集成Junit比较简单,首先在pom.xml 中增加Junit 的maven 依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

然后, 就可以在 src/test/java/ 目录下编写测试用例了,示例如下:

# GdnMetaApiHelperTest.groovy

package com.app..test

import com.app.gdn.helper.GdnMetaApiHelper
import groovy.transform.CompileStatic
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.test.context.junit4.SpringRunner

@CompileStatic
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class GdnMetaApiHelperTest {
    @Autowired
    GdnMetaApiHelper gdnMetaApiHelper;

    @Test
    public void testUpdateInsMember() {
        String gdnInsName = "gdntest20"
        String memberKind = "cluster"
        String memberName = "dangyitest13gdnstandby6"
        String status = "active"
        String requestId = "test_request"
        gdnMetaApiHelper.updateInsMember(gdnInsName, memberKind, memberName, null, status, requestId)
    }
}

但是在运行的过程中却一直报错

com.aliyun.apsaradb.gdnmetaapi.ApiException: java.net.UnknownHostException: http: nodename nor servname provided, or not known
    at com.aliyun.apsaradb.gdnmetaapi.ApiClient.execute(ApiClient.java:972)
    at com.aliyun.apsaradb.gdnmetaapi.api.DefaultApi.getGlobalConfigWithHttpInfo(DefaultApi.java:1496)

问题分析

解决办法

重新运行, 会发现已经将src/main/resources 目录下的文件编译到test-classes 里面去了


image.png

更多干货,欢迎关注个人博客:http://mochi.website/

上一篇 下一篇

猜你喜欢

热点阅读