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

流过昼夜 提交于 2019-12-10 16:47:25

问题


I have 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 refactored class like this:

@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 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 me cause of the problem
2.Please advise me elegant way to avoid following error.

I want to encapsulate logic in one file.


回答1:


I have encountered the same issue recently and I solved the issue by changing the class to public class. You might need to check if your Class Base1 is indeed a public class.




回答2:


  1. The cause of the problem is that your test class template is over engineered.
  2. The most elegant way I know of to avoid the error is to refactor your test class like this (I have omitted @RunWith annotation because I don't know what the correct one is):

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

In your implementation, having each test in it's own class is unnecessary because JUnit will create multiple instances of ProductTest anyway - so each test is isolated from all the others without having to be in it's own class.



来源:https://stackoverflow.com/questions/28475003/mockito-java-lang-exception-class-should-be-public-when-i-use-inner-classes-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!