Alexandr
Alexandr

Reputation: 9515

JUnit exluding base class from tests in Spring context

All my unit tests have the same header with Spring annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/spring-master.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional()

I moved them to base class and all my tests now extend it:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/spring-master.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional()
public class DomainObjectBaseTest {
...
public class SomeTest extends DomainObjectBaseTest {

Now when I'm running all tests I'm getting for the DomainObjectBaseTest:

java.lang.Exception: No runnable methods

My question is how can I avoid it? I can remove @RunWith, but in this case, I'll have to apply the annotation to all other tests. Too many extra code,don't like it.

Probably, as an alternative, I can somehow in Spring group annotations, name it, and refere to the group from my tests, please tell me if it is possible. In this case I'll remove base class at all.

And probably there is a way to tell that this class should not be tested. I'm using JUnit4.

Upvotes: 4

Views: 817

Answers (1)

AlexR
AlexR

Reputation: 115418

Make your base class abstract. The problem is that I believe it is not abstract, so test runner tries to run it and fails for this (IMHO stupid) reason. But it ignores abstract classes.

Upvotes: 11

Related Questions