How to make Jersey to use SLF4J instead of JUL?

后端 未结 6 709
野的像风
野的像风 2020-12-25 12:22

I\'ve found a useful article that explains how to make Jersey to use SLF4J instead of JUL. Now my unit test looks like (and it works perfectly):

public class         


        
6条回答
  •  囚心锁ツ
    2020-12-25 13:01

    What it sounds like is you'd want the JUL/SLF4J configuration handle before JUnit starts testing so it could be covered for all tests? Here's a way you could do that.

    Output

    MySuite.init()
    MySuite()
    getSuiteTests()
    MyTest.init()
    MyTest()
    test()
    

    Code

    @RunWith(AbstractTestSuite.TestSuiteRunner.class)
    public abstract class AbstractTestSuite {
       public static class TestSuiteRunner extends Suite {
          public TestSuiteRunner(Class klass) throws Exception {
             super(klass, ((Class) klass).newInstance().getSuiteClasses());
          }
       }
    
       public Class[] getSuiteClasses() {
          List> all = new ArrayList>();
          for (Class testClass : getSuiteTests()) {
             all.add(testClass);
          }
          return all.toArray(new Class[0]);
       }
    
       protected abstract Iterable> getSuiteTests();
    }
    
    public class MySuite extends AbstractTestSuite {
       public static class MyTest {
          static {
             System.out.println("MyTest.init()");
          }
    
          public MyTest() {
             System.out.println("MyTest()");
          }
    
          @Test
          public void test() {
             System.out.println("test()");
             assertTrue(true);
          }
       }
    
       static {
          System.out.println("MySuite.init()");
       }
    
       public MySuite() {
          System.out.println("MySuite()");
       }
    
       @Override
       protected Iterable> getSuiteTests() {
          System.out.println("getSuiteTests()");
          return Arrays.asList(new Class[] {MyTest.class});
       }
    }
    

提交回复
热议问题