4

私のテストでは、春の依存性注入のトランザクションとパラメーターを使用する必要があります。パラメータ化されたDIを使用する方法の例を見つけました:

@RunWith(value = Parameterized.class)
@ContextConfiguration(locations = { "classpath:applicationContextTest-business.xml" })
public class TournamentServiceTest {

@Autowired
TournamentService tournamentService;

    public TournamentServiceTest(int playerCount) {
        this.playerCount = playerCount;
    }

    @Parameters
    public static List<Object[]> data() {
        final List<Object[]> parametry = new ArrayList<Object[]>();
        parametry.add(new Object[] { 19 });
        parametry.add(new Object[] { 20 });
        return parametry;
    }

    @Before
    public void vytvorTurnaj() throws Exception {
        testContextManager = new TestContextManager(getClass());
        testContextManager.prepareTestInstance(this);
    }

@Test
public void test1() {
     Assert.assertFalse(false);
}

}

この例は機能します。次に、このクラスにトランザクションを追加する必要があります。

@RunWith(value = Parameterized.class)
@ContextConfiguration(locations = { "classpath:applicationContextTest-business.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class TournamentServiceTest ...

この2つの新しい行を追加すると、このテストは例外をスローしました:

org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.toursys.processor.service.TournamentServiceTest]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

彼は空のコンストラクターを追加したいので:

public TournamentServiceTest() {
    this.playerCount = 20;
}

しかし、パラメータ化されたカントはこのテストを実行できないため、これを追加することはできません。どうすればこの問題を解決できますか?

4

2 に答える 2

2

Spring TestContext フレームワークは現在、パラメーター化されたテストをサポートしていません。これにはカスタムルールまたはランナーが必要です。オープン プル リクエストがあり、そこからコードを取得できます。

Spring 4.2以降、使用できます

@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
于 2013-02-12T15:23:38.270 に答える
0

CallbackParams のこの JUnit ランナーがあります - http://callbackparams.org

パラメータ化を提供しますが、実際のテスト実行を選択した JUnit ランナーに渡すこともできます。SpringJUnit4ClassRunner に渡すと、トランザクションサポートなどを備えた「Spring パラメータ化されたトランザクションテスト」が得られます...

@RunWith(CallbackParamsRunner.class)
@WrappedRunner(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContextTest-business.xml"})
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class TestTournamentService {

    enum PlayerCountData {
        _19, _20;

        final int value = Integer.parseInt(name().substring(1));
    }

    @ParameterizedValue PlayerCountData playerCount;

    @Autowired TournamentService tournamentService;

    @Test
    public void test1() {
        Assert.assertNotNull(
                "TournamentService should have been autowired",
                tournamentService);
        Assert.assertTrue("Player-count value greater than 18",
                18 < playerCount.value);
        Assert.assertTrue("Player-count value less than 21",
                playerCount.value < 21);
    }
}

ご覧のとおり、パラメーター化ソリューションは少し異なります。これは、チュートリアルで説明されている理由により、CallbackParams がプリミティブ データのパラメーター化を優先しないためです: http://callbackparams.org/index.html#articles

CallbackParams を使用すると、パラメーター値はパラメーターの型から決定されるため、int パラメーターを直接使用することはできません。- 上記の例では、パラメーター タイプの PlayerCountData を使用し、列挙定数の名前から int 値を解析します。

テストクラスを実行すると、次の名前の 2 つのテストが実行されます。

  • test1[playerCount=_19]
  • test1[playerCount=_20]

多分これはあなたが探しているものです。

于 2013-05-08T13:06:48.320 に答える