Search code examples
javajunitmockitoinner-classesrunner

How to avoid ignore tests if I use inner classes for unit testing architecting?


I have following architecture of unit test:

@RunWith(Enclosed.class)
public class ProductTest {

    @RunWith(MockitoJUnitRunner.class)
    public static abstract class Base {...}

    public static class Test1 extends Base{
        @Test
        public void foo(){...}
        }
    }

    public static class Test2 extends Base{
        @Test
        public void bar(){...}
        }
    }
}

If I run unit tests I see following error message:

org.mockito.exceptions.base.MockitoException: 

No tests found in Base
Haven't you forgot @Test annotation?

Mockito tries to say me that Base is test bit have noone method annoted with @Test

I have found decision - ignore base class.

Bit it looks like hack. Is there more elegant way?


Solution

  • Move Base outside the ProductTest class (can be in same file) and move RunWith to concrete classes.