Java 当nRepwriterisNull()引发IOException时,需要在public void test内进行断言
我想在下面的IOException中写入断言Java 当nRepwriterisNull()引发IOException时,需要在public void test内进行断言,java,junit,sonarqube,Java,Junit,Sonarqube,我想在下面的IOException中写入断言 public void testWhenRespWriterIsNull() throws IOException { when(response.getWriter()).thenThrow(IOException.class); when(request.getParameter("version")).thenReturn(null); logoutServlet.doPost(request, response);
public void testWhenRespWriterIsNull() throws IOException {
when(response.getWriter()).thenThrow(IOException.class);
when(request.getParameter("version")).thenReturn(null);
logoutServlet.doPost(request, response);
exceptionRule.equals(IOException.class);
}
更新:SonarQube中出现以下错误
至少向该测试用例添加一个断言
因为我是JUNIT新手,所以我不知道在上面的
testWhenRepwriterisNull()
中需要编写哪个断言,如果您已经有了exceptionRule.equals(IOException.class)代码>,不需要任何其他Assert语句。此语句验证是否引发了IOException
您可能希望测试抛出的异常中的错误消息,方法是
exceptionRule.expectMessage(“用于输入字符串”)代码>
有关在Junits中断言异常的更多示例,请查看亲爱的所有人,任何人都可以在这方面帮助我。谢谢…当我在sonarqube中运行代码时,我会遇到以下断言错误“将至少一个断言添加到此测试用例”。我在代码的下面添加了一行。public void testWhenRepwriterisNull()抛出IOException{when(response.getWriter()).thenthow(IOException.class);when(request.getParameter(“version”).thenReturn(null);logoutServlet.doPost(request,response);exceptionRule.equals(IOException.class);Assert.assertNotNull(“version”);}我添加了Assert.assertNotNull(“version”);到上面以及之后,我没有收到sonar qube问题。但我不确定写Assert.assertNotNull(“version”);”或“我需要做的其他事情?这是一个解决方法。”。我认为这个声纳警报有点假阳性。也许你可以在声纳上将其标记为“不会固定”。此外,值得检查sonar源代码或文档,以找出哪些语句被认为是有效的断言语句。