2015-11-04 54 views
0

我想为我们公司的一些常见使用案例(例如Google Guave针对集合)创建测试套件生成器。针对TestSuites的未通过测试的测试

所以我创建了我的构建器,以及一些扩展TestSuiteTestCase的类,如果需要,它们会被添加。但是运行在Eclipse中的一切(火星4.5.1 - 不判断)显示测试为“无根试验”,并认为不能将它们标记为成功要么失败也不:

JUnit View

带来的Maven以下例外(但它是Maven,我可以刻录该桥,但我不相信它与上面有任何关系):

[错误]未能执行目标org.apache .maven.plugins:maven-surefire-plugin:2.18.1:项目org.acme.project上的测试(默认测试)目标org.apache.maven.plugins的执行默认测试:maven-surefire-plugin:2.18。 1:测试辉导致:有在叉形过程中出现错误

Other questions状态,这是由于使用JUnit 4 JUnit的3类TestCase(未弃用,因此...),在这种情况下的问题是:如何将此代码转换为JUnit 4?

@RunWith(Suite.class) 
@Suite.SuiteClasses({ MyTest.Suite.class }) 
public class MyTest { 

    public static class Suite { 

     public static TestSuite suite() { 
      final TestSuite suite = new TestSuite("My Test Suite"); 
      suite.addTest(new CompoundTester(value -> new MyTester(value), "A", "B")); 
      return suite; 
     } 
    } 

    public static class CompoundTester extends TestSuite { 

     public CompoundTester(final Function<String, TestCase> testFactory, final String... values) { 
      String name = "unknown"; 
      for (final String value : values) { 
       final TestCase testCase = testFactory.apply(value); 
       name = testCase.getName(); 
       addTest(new TestCase(value.toString()) { 

        @Override 
        public void run(final TestResult result) { 
         testCase.run(result); 
        } 
       }); 
      } 
      setName(name); 
     } 
    } 

    public static class MyTester extends TestCase { 

     private final String object; 

     public MyTester(final String object) { 
      super("testSomething"); 
      this.object = object; 
     } 

     public void testSomething() { 
      Assert.assertNotNull(this.object); 
     } 

    } 
} 

回答

0

它的工作原理,如果匿名TestCase类的run方法是这样的:

@Override 
public final void run(final TestResult result) { 
    result.startTest(this); 
    result.runProtected(this,() -> testCase.run()); 
    result.endTest(this); 
} 

不知道为什么,虽然。