计算测试中的行数 类

Counting number of lines in test classes

我正在开发一个工具来识别测试 类 并计算那些 类 中的行数。该工具还会计算业务代码的行数并比较两者的结果,这是我的代码:

    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++) 返回测试数量 类 而不是类 中的行数!我能做什么?

谢谢

假设您要计算包含 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());
       }