我需要使用彈簧依賴注入事務和參數。我發現如何使用參數化和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;
}
但我不能添加這個,因爲然後參數化無法運行此測試。我如何解決這個問題?
hm thx然後我改變了我的測試的概念。我錯過了Transactional註釋,並且在註釋之後,我刪除了我在測試中更改或創建的所有內容 – hudi 2013-02-13 07:52:34