1 package test.methodinterceptors;
2 
3 import org.testng.IMethodInstance;
4 import org.testng.IMethodInterceptor;
5 import org.testng.ITestContext;
6 import org.testng.TestNG;
7 import org.testng.annotations.Test;
8 
9 import java.util.ArrayList;
10 import java.util.List;
11 
12 import test.InvokedMethodNameListener;
13 import test.SimpleBaseTest;
14 
15 import static org.assertj.core.api.Assertions.assertThat;
16 
17 public class Issue521Test extends SimpleBaseTest {
18 
19   @Test(description = "test for https://github.com/cbeust/testng/issues/521")
BeforeClass_method_should_be_fired_when_IMethodInterceptor_removes_test_methods()20   public void BeforeClass_method_should_be_fired_when_IMethodInterceptor_removes_test_methods() {
21     TestNG tng = create(Issue521.class);
22     tng.setMethodInterceptor(new IMethodInterceptor() {
23       @Override
24       public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
25         List<IMethodInstance> instances = new ArrayList<>();
26         for (IMethodInstance instance : methods) {
27           if (!instance.getMethod().getMethodName().equals("test1")) {
28             instances.add(instance);
29           }
30         }
31         return instances;
32       }
33     });
34 
35     InvokedMethodNameListener listener = new InvokedMethodNameListener();
36     tng.addListener(listener);
37 
38     tng.run();
39 
40     assertThat(listener.getInvokedMethodNames()).containsExactly("beforeClass", "test2");
41   }
42 
43 }
44