1

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

    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++) 计算测试类中的代码数量不起作用返回的是测试类的数量,而不是这些类中的行数!我能做些什么 ?

谢谢

4

1 回答 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());
       }
于 2016-02-29T04:39:49.483 回答