2013-02-12 31 views
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 ... 

當我添加這兩個新行那麼這個測試拋出的異常:

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; 
} 

但我不能添加這個,因爲然後參數化無法運行此測試。我如何解決這個問題?

回答

2

Spring TestContext框架目前的not support Parameterized tests。你需要一個自定義規則或跑步者。有一個開放的 pull request,你可以從那裏拿走代碼。

在Spring 4.2,你可以使用

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

@Rule 
public final SpringMethodRule springMethodRule = new SpringMethodRule(); 
+0

hm thx然後我改變了我的測試的概念。我錯過了Transactional註釋,並且在註釋之後,我刪除了我在測試中更改或創建的所有內容 – hudi 2013-02-13 07:52:34

0

我CallbackParams這個JUnit的亞軍 - http://callbackparams.org

它提供了參數,但它也可以通過實際測試執行到的JUnit你選擇的行者。如果傳遞到基於SpringJUnit4ClassRunner你會得到你的「春天參數事務性測試」,以事務支持等等

@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 parameterers不能使用直。 - 以上示例使用參數類型PlayerCountData並從枚舉常量的名稱中解析int值。

運行兩試運行測試類的結果與這些名字:

  • TEST1 [playerCount = _19]
  • TEST1 [playerCount = _20]

也許這就是你正在尋找。