小编Den*_*din的帖子

在非模拟方法中模拟方法

我正在尝试模拟在另一个正在进行单元测试的方法中调用的方法。但是,模拟无法正常工作,UnknownHostException如果我从不首先模拟内部方法,我就会得到一个。这可能是我的 throws 声明,因为我不知道该怎么做。任何帮助表示赞赏。

到目前为止,我的测试是这样的:

@Test
public void testServiceValidHost() throws ClientProtocolException, IOException {
    HealthService service = new HealthService();
    HealthService spy = Mockito.spy(service);

    when(spy.getHTTPResponse("http://" + HOST + "/health")).thenReturn(SOMESTATUS);

    String actual = spy. executeHealthCheck(HOST);
    assertEquals(SOMESTATUS, actual);
}
Run Code Online (Sandbox Code Playgroud)

我正在测试的方法是executeHealthCheck,我想模拟getHTTPResponse

public String executeHealthCheck(String host) {
    try {
        String responseBody = getHTTPResponse("http://" + host + "/health");
        return responseBody;
    } catch (UnknownHostException e) {
        ...
        return "Invalid Host";
    } catch (Exception e) {
        ...
        return "Error";
    }
}

public String getHTTPResponse(String url) …
Run Code Online (Sandbox Code Playgroud)

java junit mockito

8
推荐指数
1
解决办法
3130
查看次数

模拟 OpenFeign 客户端以在 Spring 库中进行单元测试,而不是在 Spring Boot 应用程序中进行单元测试

我已经实现了一个基于此官方存储库调用 get API 的 feign 客户端。我有一个规则类UserValidationRule,需要调用它来获取 API 调用getUser()并验证一些内容。这按预期工作,但是当我开始测试该规则类时,模拟假客户端并不成功,它会继续调用实际的 API。我已经简化了情况,所以请忽略简单性哈哈。这是我发现这个stackoverflow问题后的一个后续问题

API 返回此模型:

@Data
public class userModel {
    private long id;
    private String name;
    private int age;

}
Run Code Online (Sandbox Code Playgroud)

与其余客户端方法的接口:

public interface UserServiceClient {

    @RequestLine("GET /users/{id}")
    UserModel getUser(@Param("id") int id);
}
Run Code Online (Sandbox Code Playgroud)

在规则类中,我构建了 feign 客户端并调用 API:

@RequiredArgsConstructor
@Component
public class UserValidationRule {

    private static final String API_PATH = "http://localhost:8080";

    private UserServiceClient userServiceClient;


    public void validate(String userId, ...) {
            // some validations 
            validateUser(userId);

    }

    private void validateUser(String userId) …
Run Code Online (Sandbox Code Playgroud)

java spring microservices spring-cloud-feign openfeign

5
推荐指数
1
解决办法
1万
查看次数

mockito 可以抛出一般异常

Mockito 可以扔将军Exception吗?

当我这样做时,测试失败并显示“org.mockito.exceptions.base.MockitoException: Checked Exception is invalid for this method”

这是我的 @Test

public void testServiceSomeError() throws ClientProtocolException, IOException {
    //Arrange    
    HealthService service = Mockito.mock(HealthService.class);

    when(service.executeX(HOST)).thenCallRealMethod();
    when(service.getHTTPResponse("http://" + HOST + "/health")).thenThrow(Exception.class);
    //Act
    String actual = service.executeX(HOST);

    //Assert
    assertEquals(ERROR, actual);
}
Run Code Online (Sandbox Code Playgroud)

java exception mockito

2
推荐指数
1
解决办法
1370
查看次数

通过 YML 文件将地图中的符号和特殊字符作为键

是否可以存储通过 application.yml 映射的: ; / , .Java 中的映射键等字符?

我试图通过 application.yml 文件中的映射来存储 URI 以及与它们关联的值,但是由于它是一个键,因此它不会保存任何特殊字符

http://localhost:8080变为 httplocalhost8080

有什么办法可以保存那些丢失的字符吗?

~~编辑~~ 我已经尝试过在键周围使用转义字符、双引号和单引号,但一旦它们位于哈希图中,它们就会被忽略。

yml 文件中的属性具有以下结构:

  uri-example-config:
    keyToValueMap:
      http://localhost:8080 : String1
      http://exmaple.com: String2
      http://moreURIs.com: String2
Run Code Online (Sandbox Code Playgroud)

然后我的配置是:

@Configuration
@ConfigurationProperties(prefix = "uri-example-config")
@EnableConfigurationProperties
@Getter
@Setter
public class URIProperties {
    private Map<String, String> keyToValueMap = new HashMap<>();
}
Run Code Online (Sandbox Code Playgroud)

~~~~编辑2~~~~

是的,我可以轻松地使用 map.put(" http://localhost:8080 "...) 并且字符串按预期保留。通过带有配置和属性的 yml 文件执行此操作会导致此问题。

java spring key hashmap special-characters

2
推荐指数
1
解决办法
1万
查看次数