Mockito + PowerMock LinkageError while mocking system class

后端 未结 6 1817
难免孤独
难免孤独 2020-12-12 10:28

I\'ve got such a code snippet:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Thread.class})
public class AllMeasuresDataTest {

@Before
public void setUp(         


        
6条回答
  •  眼角桃花
    2020-12-12 11:17

    In order to mock system classes, prepare the class that is the target of the test, not Thread.class. There's no way PowerMock will be able to instrument Thread.class because it is required during JVM startup - well before PowerMock can instrument.

    The way instrumentation works, once a class is loaded, it can no longer be intstrumented.

    See the PowerMock wiki.

提交回复
热议问题