Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 226
Alexandr Avatar asked Dec 20 '22 05:12

Alexandr


1 Answers

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.

like image 118
AlexR Avatar answered Jan 07 '23 06:01

AlexR