7

Does any one know how to tackle this.

@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(Parametrized.class)

@ContextConfiguration("/META-INF/blah-spring-test.xml")
public class BlahTest
..

so i want to have a spring nature test and at the same time want to have it parameterized to avoid code duplication ...

maryoush
  • 173
  • 1
  • 12

3 Answers3

4

You can't use two runners as it noted in the commented post. You should use the Parameterized runner as use Spring's TestContextManager to load the Spring context.

@Before 
public void before() throws Exception {
  new TestContextManager(getClass()).prepareTestInstance(this);
}
John B
  • 32,493
  • 6
  • 77
  • 98
0

As of Spring Framework 4.2, JUnit-based integration tests can now be executed with JUnit rules instead of the SpringJUnit4ClassRunner. This allows Spring-based integration tests to be run with alternative runners like JUnit’s Parameterized or third-party runners such as the MockitoJUnitRunner. See more details on spring doc.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
0

Building upon John B's answer using TestContextManager, one can also call beforeTestMethod() and afterTestMethod() on it to better simulate SpringJUnit4ClassRunner's behaviour (for instance loading database with @Sql).

These methods require a Method parameter, so one can for instance take advantage of JUnit4's TestName rule to get the current test method's name and then retrieving it by reflection.

private static TestContextManager springTestContext
  = new TestContextManager(BlahTest.class);

@Rule
public TestName testName = new TestName();

@Before
public void before() throws Exception {
  springTestContext.prepareTestInstance(this);
  springTestContext.beforeTestMethod(this,
    getClass().getMethod(testName.getMethodName()));
}

@After
public void after() throws Exception {
  springTestContext.afterTestMethod(this,
    getClass().getMethod(testName.getMethodName()), null);
}
fbastien
  • 762
  • 1
  • 9
  • 20