Mockito java.lang.Exception: class should be public when I use inner classes in tests

I have the following test:

@RunWith(Enclosed.class)
public class ProductTest {

    @RunWith(MockitoJUnitRunner.class)
    @Ignore
    public static abstract class Base1 {
        @Before
        public void setUpBase()  {...}
    }

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

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

To avoid @Ignore, I reorganized the class as follows:

@RunWith(Enclosed.class)
public class ProductTest {

    @RunWith(MockitoJUnitRunner.class)
    public static class Test1 extends Base1 {
        @Test
        public void foo() {...}
    }

    @RunWith(MockitoJUnitRunner.class)
    public static class Test2 extends Base1 {
        @Test
        public void bar() {...}
    }
}

abstract class Base1 {
    @Before
    public void setUpBase()  {...}
}

But I see an error:

java.lang.Exception: Class Base1 should be public
    at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:91)
    at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:70)
    at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:133)
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:165)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:104)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.<init>(JUnit45AndHigherRunnerImpl.java:23)


1. Please clarify the cause of the problem 2. Please advise me on an elegant way to avoid the following error.

I want to encapsulate the logic in a single file.

+4
source share
1 answer
  • The cause of the problem is that the test class template has been redesigned.
  • , , , - ( @RunWith, , ):

    public class ProductTest {
        @Before
        public void setUpBase()  {...}
    
        @Test
        public void foo(){...}
    
        @Test
        public void bar(){...}
    }
    

, JUnit ProductTest - .

-1

All Articles