本文介绍了如何使用Parametrized运行JUnit SpringJUnit4ClassRunner?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 由于重复 @RunWith 注释,以下代码无效:The following code is invalid due to duplicate @RunWith annotation:@RunWith(SpringJUnit4ClassRunner.class)@RunWith(Parameterized.class)@SpringApplicationConfiguration(classes = {ApplicationConfigTest.class})public class ServiceTest {}但是如何结合使用这两个注释?But how can I use these two annotations in conjunction?推荐答案至少有两个选项可以做到:There are at least 2 options to do that: 关注 http://www.blog.project13。 pl / index.php / coding / 1077 / runwith-junit4-with-springjunit4classrunner-and-parameterized /您的测试需要看起来像这个:Your test needs to look something like this: @RunWith(Parameterized.class) @ContextConfiguration(classes = {ApplicationConfigTest.class}) public class ServiceTest { private TestContextManager testContextManager; @Before public void setUpContext() throws Exception { //this is where the magic happens, we actually do "by hand" what the spring runner would do for us, // read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though this.testContextManager = new TestContextManager(getClass()); this.testContextManager.prepareTestInstance(this); } ... } 那里是一个github项目 https://github.com/mmichaelis/spring-aware-rule ,它基于以前的版本博客,但以一般方式添加支持There is a github project https://github.com/mmichaelis/spring-aware-rule, which builds on previous blog, but adds support in a generalized way@SuppressWarnings("InstanceMethodNamingConvention")@ContextConfiguration(classes = {ServiceTest.class})public class SpringAwareTest { @ClassRule public static final SpringAware SPRING_AWARE = SpringAware.forClass(SpringAwareTest.class); @Rule public TestRule springAwareMethod = SPRING_AWARE.forInstance(this); @Rule public TestName testName = new TestName(); ...}因此,您可以拥有一个实现其中一种方法的基本类,以及从中继承的所有测试。So you can have a basic class implementing one of the approaches, and all tests inheriting from it. 这篇关于如何使用Parametrized运行JUnit SpringJUnit4ClassRunner?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!