我正在开发一种识别测试类并计算这些类中行数的工具。该工具还将计算业务代码的行数并比较两个结果,这是我的代码:
for (File f : list) {
if (f.isDirectory()) {
walk(f.getAbsolutePath());
}
if (f.getName().endsWith(".java")) {
System.out.println("File:" + f.getName());
countFiles++;
Scanner testScanner = new Scanner(f);
while (testScanner.hasNextLine()) {
String test = testScanner.nextLine();
if (test.contains("org.junit") || test.contains("org.mockito")) {
System.out.println("this is a test class");
testCounter++;
break;
}
}
Scanner sc2 = new Scanner(f);
while (sc2.hasNextLine()) {
count++;
使用(count ++)计算业务代码是完美的,但计算测试类中的代码数量不起作用(testCounter ++)返回测试类的数量而不是这些类中的行数!我该怎么办?
由于
答案 0 :(得分:3)
当您遇到包字符串'org.junit'或'org.mockito'时,您只是递增testCounter。一旦找到,你就会增加并突破循环。
当然,您不希望代码中的每一行都包含这些字符串。
答案 1 :(得分:1)
假设您想要计算包含org.junit or org.mockito
然后你想做
while (testScanner.hasNextLine()) {
String test = testScanner.nextLine();
if (test.contains("org.junit") || test.contains("org.mockito"))
{
hasTestLines = true;
}
count++;
}
if (hasTestLines) {
System.out.println(String.format ("there were %d lines in file %s which also had org.junit||org.mockito",
count, f.getName());
}
else {
System.out.println(String.format ("there were %d lines in file %s which did NOT have org.junit||org.mockito",
count, f.getName());
}