2017-11-05 2 views
0
interface MyInterface {} 

static class ImplA implements MyInterface {} 

static class ImplB implements MyInterface {} 

class OuterTests { 

    @ParameterizedTest 
    @MethodSource("myInterfaceProvider") 
    void test(MyInterface myInterface) {} 

    static Stream<MyInterface> myInterfaceProvider() { 
     return Stream.of(new ImplA(), new ImplB()); 
    } 

    @Nested 
    class InnerTests{ 

     @Test 
     void nestedTest() { 
      //how to access myInterface:MyInterface? 
     } 

    } 

} 
+0

を、なぜそれをメンバーに入れていませんか? – Mzf

+0

確かに、それはうまくいくでしょう。しかし、どのように/そのメンバーが設定されていますか? – cnmuc

+0

フィールド注入には@Parameterを使うことができます。もっとたくさんの例を見てください:https://github.com/junit-team/junit4/wiki/parameterized-testsもし完全な例を与えることができたら – Mzf

答えて

1

InnerTestsテスト・ツリーのパラメータ化テストの下ではありません。したがって

enter image description here

、引数を渡すことはできませんテストメソッドのネストされたテストクラスへの変換。

は、ここでのJUnit木星のインターフェイスのためのテストを定義する方法です:

interface MyInterfaceTests { 

    MyInterface newInstance(); 

    @Test 
    default void test() { 
     MyInterface instance = newInstance(); 
     // test something 
    } 
} 

class ImplATests implements MyInterfaceTests { 
    @Override 
    public MyInterface newInstance() { 
     return new ImplA(); 
    } 
} 

class ImplBTests implements MyInterfaceTests { 
    @Override 
    public MyInterface newInstance() { 
     return new ImplB(); 
    } 
} 

また、あなたが@TestFactoryを使用してそれを書くことができます。

@TestFactory 
Stream<DynamicNode> test() { 
    return Stream.of(new ImplA(), new ImplB()) 
     .map(instance -> dynamicContainer(instance.getClass().getSimpleName(), Stream.of(
      dynamicTest("test 1",() -> { 
       // test something 
      }), 
      dynamicTest("test 2",() -> { 
       // test something else 
      }) 
     ))); 
} 
関連する問題