在我的测试中,我需要使用 spring 依赖注入事务和参数。我找到了如何使用参数化和 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;
}
但我不能添加这个,因为然后参数化不能运行这个测试。我该如何解决这个问题?