如何为一个抛出IOException并且接受ResourceRequest作为参数的方法编写JUnit测试。

huangapple go评论79阅读模式
英文:

How to write JUnit for a method that throws IOException and also takes in ResourceRequest as an argument

问题

以下是一个以ResourceRequest作为参数的方法的代码:

JSONObject updateDetail(ResourceRequest request) throws IOException {
    BufferedReader resourceReqBr = new BufferedReader(
            new InputStreamReader(request.getPortletInputStream()));
    String resourceReqStr = resourceReqBr.readLine();
    JSONObject updateCaDetailJsn = JSONObject.parse(resourceReqStr);
}

这里的request.getPortletInputStream()resourceReqBr.readLine()JSONObject.parse(resourceReqStr)是可能引发IOException的代码行。我该如何设置request对象以使其引发IOException。

我的测试代码:

@Mock
private ResourceRequest request;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testUpdatDetailIOException() throws Exception {
    expectedException.expect(IOException.class);
    foo.updateDetail(request);
}

这导致了空指针错误(这是显而易见的)。

英文:

Following is the code for a method that takes ResourceRequest as the argument

JSONObject updateDetail(ResourceRequest request) throws IOException {
	
 	BufferedReader resourceReqBr = new BufferedReader(
			new InputStreamReader(request.getPortletInputStream()));
	String resourceReqStr = resourceReqBr.readLine();
	JSONObject updateCaDetailJsn = JSONObject.parse(resourceReqStr);
	
}

Here request.getPortletInputStream(), resourceReqBr.readLine(), and JSONObject.parse(resourceReqStr) are the lines of code that throw IOException. How do i setup request object such that it throws IOException.

My test code:

@Mock
	private ResourceRequest request;

@Rule
    public ExpectedException expectedException = ExpectedException.none();

@Test
    public void testUpdatDetailIOException() throws Exception
    {
        expectedException.expect(IOException.class);
        foo.updateDetail(request);
    }

This is causing nullpointer error (which is obvious).

答案1

得分: 2

对于 ResourceRequest 的方法调用进行模拟只是为了抛出 IOException在我看来并不是唯一的测试案例

我还会尝试让 JSONParser 抛出 IOException例如期望在 JSON 字符串有问题时抛出 IOException

@Test
public void if_IOException_is_thrown_when_json_is_faulty() throws Exception {

  expectedException.expect(IOException.class);
  ResourceRequest mockResourceRequest = Mockito.mock(ResourceRequest.class);
  Mockito.when(mockResourceRequest.getPortletInputStream()).thenReturn(
      new ByteArrayInputStream("{[broken".getBytes(UTF_8)));
  foo.updateDetail(mockResourceRequest);
}
英文:

Mocking a method call of ResourceRequest just to throw a IOException is in my opinion a not the only test case.

I would also try to make the JSONParser throw the IOException. For example expect an IOException when json string is faulty.

@Test
public void if_IOException_is_thrown_when_json_is_faulty() throws Exception {

  expectedException.expect(IOException.class);
  ResourceRequest mockResourceRequest = Mockito.mock(ResourceRequest.class);
  Mockito.when(mockResourceRequest.getPortletInputStream()).thenReturn(
      new ByteArrayInputStream("{[broken".getBytes(UTF_8)));
  foo.updateDetail(mockResourceRequest);
}

答案2

得分: 0

请参考下面在您的测试代码中的模拟示例:

// 导入所需的包
import org.mockito.Mockito;
import java.io.IOException;
import javax.portlet.ResourceRequest;

// 在测试中使用模拟对象
ResourceRequest mockResourceRequest = Mockito.mock(ResourceRequest.class);
Mockito.when(mockResourceRequest.getPortletInputStream()).thenThrow(new IOException());
foo.updateDetail(mockResourceRequest);

请注意,上述代码中的变量名(例如foo)可能需要根据您的实际情况进行替换。

英文:

try below mocking in ur test code

ResourceRequest mockResourceRequest = Mockito.mock(ResourceRequest.class);
Mockito.when(mockResourceRequest.getPortletInputStream()).thenThrow(new IOException);
foo.updateDetail(mockResourceRequest);

huangapple
  • 本文由 发表于 2020年8月18日 15:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63463581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定