Cannot run powermockrule with SpringJUnit4ClassRunner in spring boot project

前端 未结 2 1485
半阙折子戏
半阙折子戏 2020-12-11 07:53

I have a spring boot project that needs to test with spring test runner(so that I can get the real application context) and mock the static method.

@RunWith(         


        
2条回答
  •  无人及你
    2020-12-11 08:47

    I found a fix from here link to use PowerMockRunnerDelegate instead of PowerMockRule.

    The updated test class would be:

    @RunWith(PowerMockRunner.class)
    @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes= MyApplication.class)
    @PrepareForTest(StaticClass.class)
    public class StaticClassTest {
    
        @Autowired
        HelloCmd hello;
    
        @Test
        public void testGetOne() {
            mockStatic(StaticClass.class);
            when(StaticClass.getNumber()).thenReturn(2);
            System.out.println(hello.getNumber());
        }
    }
    

提交回复
热议问题