Powermock mockstatic Cannot subclass final class

前端 未结 3 2043
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-18 23:12

I am trying to mock a final class

PowerMockito.mockStatic(TestFinalClass.class);

It is working from my eclipse when I run a single junit and ad

3条回答
  •  长情又很酷
    2021-02-19 00:12

    I got the code coverage using JaCoCo and need to make some changes related to it in the test class code. Those are as below:

    1. I removed @RunWith annotation

    2. Added @Rule and PowerMockRule class

    3. Mentioned the Final and Static class in @PrepareForTest

      @PrepareForTest(FinalClass.class, StaticClass.class)

      public class Tests {

      @Rule
      public PowerMockRule rule = new PowerMockRule();
      
      @Test
      public void test() {
          PowerMockito.mockStatic(FinalClass.class);
          PowerMockito.mockStatic(StaticClass.class);
      }
      

      }

    Also added the argline in surefire to overcome the final class problem while mocking.

    
                    -javaagent:{path}/powermock-module-javaagent-1.6.4.jar
                
    

提交回复
热议问题