I am trying to setup Junit test suite with Spring for the first time and tried with couple of changes in my classes, but no luck and ended up with this error : "junit.framework.AssertionFailedError: No tests found in Myclass"
Briefly, I have 2 test classes both are from same base class which loads Spring context as below
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations =
{
"classpath:ApplicationContext.xml"
})
I tried adding those 2 test classes into a suite as below
@RunWith( SpringJUnit4ClassRunner.class )
@SuiteClasses({ OneTest.class, TwoTest.class })
public class MyTestSuite extends TestCase {
//nothing here
}
I am running this test suite from ant. But, this gives me an error saying "No tests found" However, If I run the individual 2 test cases from ant, they work properly. Not sure why is this behaviour, I am sure missing something here. Please advice.
As mentioned in the comments, we run the TestSuite with @RunWith(Suite.class)
and list all the test cases with @SuiteClasses({})
. In order to not repeat the @RunWith(SpringJunit4ClassRunner.class)
and @ContextConfiguration(locations = {classpath:META-INF/spring.xml})
in each test case, we create an AbstractTestCase with these annotations defined on it and extend this abstract class for all test cases. A sample can be found below:
/**
* An abstract test case with spring runner configuration, used by all test cases.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{ "classpath:META-INF/spring.xml" })
public abstract class AbstractSampleTestCase
{
}
public class SampleTestOne extends AbstractSampleTestCase
{
@Resource
private SampleInterface sampleInterface;
@Test
public void test()
{
assertNotNull(sampleInterface);
}
}
public class SampleTestTwo extends AbstractSampleTestCase
{
@Resource
private SampleInterface sampleInterface;
@Test
public void test()
{
assertNotNull(sampleInterface);
}
}
@RunWith(Suite.class)
@SuiteClasses(
{ SampleTestOne.class, SampleTestTwo.class })
public class SampleTestSuite
{
}
If you don't want to have an AbstractSampleTest
, then you need to repeat the spring runner annotations on each test case, until Spring comes up with a SpringJunitSuiteRunner
similar to how they need to add a SpringJunitParameterizedRunner
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With