JMockit对方法体中new出来的对象mock以及对Spring RestTemplate类的mock操作
我们知道在Spring项目中用jmockit编写单元测试。对于sevice类中@Autowired的对象用@Injectable进行mock,然后在Expection中进行相应方法的mock,类似这样:
@RunWith(JMockit.class)
public class ATest{
@Tested
A a=new A();
@Injectable
private B b;
@Test
public void c(){
new Expectations() {
{
b.methodA();
result="aaa";
}
};
...
a.c();
}
}
对于方法c中new出来的对象以及Restemplate对象如何基于jmockit进行单测的编写呢?例如:
public class A{
public String b(){
RestTemplate restTemplate = new RestTemplate();
String resStr = restTemplate.getForObject("url", String.class, token);
JSONObject jsonObject = JSONObject.parseObject(resStr);
....
}
}
jmockit单元测试如下:
@RunWith(JMockit.class)
public class ATest{
@Tested
A a=new A();
@Test
public void b(){
RestTemplate restTemplate=new RestTemplate();
new Expectations(RestTemplate.class){
{
restTemplate.getForObject(anyString,(Class<T>)any,anyString);
result="{'status':2,'data':{'phone':'18867112222'}}";
result="{'status':1,'data':{'phone':'18867112222'}}";
}
};
...
a.b();
}
}
如果报类似如下异常:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/DefaultIndenter
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.init(AbstractJackson2HttpMessageConverter.java:97)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.<init>(AbstractJackson2HttpMessageConverter.java:90)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.<init>(MappingJackson2HttpMessageConverter.java:66)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.<init>(MappingJackson2HttpMessageConverter.java:57)
at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.<init>(AllEncompassingFormHttpMessageConverter.java:61)
at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:159)
at com.zyhy.izms.service.impl.account.AccountServiceImplTest.xyrjLoginValidator(AccountServiceImplTest.java:326)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.util.DefaultIndenter
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 13 more
请在pom.xml中引入RestTemplate需要的jackson相关依赖。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>