Java 什么-Junit中有一段时间没有出现所有2个分支的循环条件
以下while循环遗漏了哪些junit条件 CompanyService.java:Java 什么-Junit中有一段时间没有出现所有2个分支的循环条件,java,junit,code-coverage,Java,Junit,Code Coverage,以下while循环遗漏了哪些junit条件 CompanyService.java: @GET @Path("/services") @Produces(MediaType.APPLICATION_JSON) public Response getcompanyData(@QueryParam("comp") final String comp, @Context HttpServletRequest request) { Response response =
@GET
@Path("/services")
@Produces(MediaType.APPLICATION_JSON)
public Response getcompanyData(@QueryParam("comp") final String comp,
@Context HttpServletRequest request) {
Response response = null;
String company= null;
if (comp!= null && !comp.isEmpty()) {
company= comp;
} else {
company= getCompanyReqHeader(request);
}
response = responseBuilder.getcompanyData(company); // here calls getCompanyData of ResponseBuilder class
return response ;
}
public static String getCompanyReqHeader(HttpServletRequest request) {
// code to get the companyname from request header
}
CompanyServiceTest.java
private static final String SVC_URL= "http://localhost:8080/services/getcompanyData?comp=xxx"
@Test
public void testCompanyService() throws IOException, MalformedURLException {
BufferedReader br = null;
StringBuilder response = null;
Assert.assertNotNull(SVC_URL);
URL url = new URL(SVC_URL);
Assert.assertNotNull(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Assert.assertNotNull(conn);
Assert.assertSame(200, conn.getResponseCode());
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Assert.assertNotNull(br);
response = new StringBuilder();
String inputLine = null;
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
conn.disconnect();
}
上述while循环条件下显示的“所有2个分支均未命中”标记。
当循环时,是否有人可以建议上述Junit missed条件?提供的代码中的分支为
br.readLine()为null
,br.readLine()不为null
。您必须测试这两种情况,以满足您的代码覆盖率工具。这意味着你的测试覆盖了你测试的类的10-15%,你想知道其他85-90%是什么吗?你的测试正在调用你的服务。但是您的服务包含测试尚未运行的代码。我很想告诉你那是什么代码,但我把水晶球忘在家里了。添加了服务代码。。!你确定你正在运行上面的代码吗?在我看来,您应该有一个空指针异常。