Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为Webtarget编写模拟测试用例,并为rest客户端编写响应?_Java_Unit Testing_Mockito_Junit4_Powermock - Fatal编程技术网

Java 如何为Webtarget编写模拟测试用例,并为rest客户端编写响应?

Java 如何为Webtarget编写模拟测试用例,并为rest客户端编写响应?,java,unit-testing,mockito,junit4,powermock,Java,Unit Testing,Mockito,Junit4,Powermock,当模拟Response对象时,授权头的builder对象返回null 如何模拟头的第二部分,使其不返回空值?eq(anyString())中存在问题 Mockito.when(mockWebTarget.request(MediaType.APPLICATION_JSON)).thenReturn(mockBuilder); Mockito.when(mockBuilder.header("Content-type", MediaType.APPLICATION_JSON)) .

当模拟
Response
对象时,授权头的
builder
对象返回null

如何模拟头的第二部分,使其不返回空值?

eq(anyString())
中存在问题

Mockito.when(mockWebTarget.request(MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);
Mockito.when(mockBuilder.header("Content-type", MediaType.APPLICATION_JSON))
        .thenReturn(mockBuilder);
Mockito.when(mockBuilder.header("Authorization",eq(anyString())))
        .thenReturn(mockBuilder);
Mockito.when(mockBuilder.post(Entity.entity(anyString(), MediaType.TEXT_PLAIN), eq(Response.class)))
        .thenReturn(mockResponse);
应该是

Mockito.when(mockBuilder.header("Authorization",eq(anyString())))
    .thenReturn(mockBuilder);
参数matcher
eq
用于文本匹配

此外,如果使用参数匹配器,所有参数都必须由匹配器提供

第一个有效,因为所有参数都是文本值

这也意味着

Mockito.when(mockBuilder.header(eq("Authorization"), anyString()))
    .thenReturn(mockBuilder);
需要换成

Mockito.when(mockBuilder.post(Entity.entity(anyString(), MediaType.TEXT_PLAIN), eq(Response.class)))
    .thenReturn(mockResponse);
Mockito.when(mockBuilder.post(Entity.entity(anyString(), MediaType.TEXT_PLAIN), eq(Response.class)))
    .thenReturn(mockResponse);
Mockito.when(mockBuilder.post(any(Entity.class), eq(Response.class)))
    .thenReturn(mockResponse);