View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.coverage.testRedundancy;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   
9   import java.lang.reflect.Method;
10  
11  import org.junit.runner.Description;
12  import org.junit.runner.notification.RunListener;
13  
14  public final class JUnitListener extends RunListener {
15      @NonNull
16      private final TestCoverage testCoverage;
17  
18      public JUnitListener(@NonNull TestCoverage testCoverage) {
19          this.testCoverage = testCoverage;
20      }
21  
22      @Override
23      public void testStarted(@NonNull Description description) {
24          if (description.isTest()) {
25              Class<?> testClass = description.getTestClass();
26              String testMethodName = description.getMethodName();
27  
28              for (Method testMethod : testClass.getDeclaredMethods()) {
29                  if (testMethod.getName().equals(testMethodName)) {
30                      testCoverage.setCurrentTestMethod(testMethod);
31                      return;
32                  }
33              }
34          }
35      }
36  
37      @Override
38      public void testFinished(@NonNull Description description) {
39          if (description.isTest()) {
40              testCoverage.setCurrentTestMethod(null);
41          }
42      }
43  }