Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

powermock - Cannot run powermockrule with SpringJUnit4ClassRunner in spring boot project

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(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes= MyApplication.class)
@PrepareForTest(StaticClass.class)
public class StaticClassTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Autowired
    HelloCmd hello;

    @Test
    public void testGetOne() {
        mockStatic(StaticClass.class);
        when(StaticClass.getNumber()).thenReturn(2);
        System.out.println(hello.getNumber());
    }
}

And I got following error message when run the test:

com.thoughtworks.xstream.converters.ConversionException: hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be : hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be
---- Debugging information ----
message             : hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be
class               : hello.hystrix.commands.StaticClassTest
required-type       : hello.hystrix.commands.StaticClassTest
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/next/target/hello
line number         : 15
class[1]            : org.junit.internal.runners.statements.InvokeMethod
class[2]            : org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks
class[3]            : org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks
class[4]            : org.powermock.modules.junit4.rule.PowerMockStatement
class[5]            : org.powermock.modules.junit4.rule.PowerMockStatement$1
version             : null

How to fix this? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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());
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...