英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论