在我的测试中,我需要使用
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;
}
但我不能添加这个,因为然后参数化不能运行此测试.我怎么能解决这个问题?
最佳答案 Spring TestContext Framework目前执行
not support Parameterized tests.您需要一个自定义规则或运行器.有空位
pull request,您可以从那里获取代码.
从0700春季开始,您可以使用
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();