原因是: EasyMock的anyObject虽然是个模版方法,但是返回了一个null,这个在java自动类型转换的时候就回报异常。
public static <T> T anyObject(final Class<T> clazz) {
reportMatcher(Any.ANY);
return null;
}
关于这个问题,我已经给EasyMock报了bug了,bug链接地址:
https://jira.codehaus.org/browse/EASYMOCK-114
下面是复现代码:
import java.util.List;
public interface IService {
List<String> query(String key, int... startAndPerPages);
}
import java.util.List;
import java.util.Collections;
public class ServiceImpl implements IService{
@SuppressWarnings("unchecked")
public List<String> query(String key, int... startAndPerPages) {
return Collections.EMPTY_LIST;
}
}
import java.util.List;
public class ClientService {
private IService depenService;
public List<String> query(String key, int... startAndPerPages) {
List<String> rets = depenService.query(key, startAndPerPages);
rets.add("aaaa");
return rets;
}
public void setDepenService(IService depenService) {
this.depenService = depenService;
}
}
import java.util.ArrayList;
import java.util.List;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
public class ClientServiceTest {
@Test
public void query_page_error(){
ClientService clientService = new ClientService();
IService mockService = EasyMock.createMock(IService.class);
EasyMock.expect(mockService.query(EasyMock.anyObject(String.class),
EasyMock.anyObject(Integer.class),EasyMock.anyObject(Integer.class)));
EasyMock.replay(mockService);
clientService.setDepenService(mockService);
String key = "test";
int startPage = 1;
int recordsPerPage = 10;
clientService.query(key, startPage,recordsPerPage);
EasyMock.verify(mockService);
}
@Test
public void query_page_correct(){
ClientService clientService = new ClientService();
List<String> rets = new ArrayList<String>();
IService mockService = EasyMock.createMock(IService.class);
EasyMock.expect(mockService.query(EasyMock.anyObject(String.class),
EasyMock.anyInt(),EasyMock.anyInt()))
.andReturn(rets);
EasyMock.replay(mockService);
clientService.setDepenService(mockService);
String key = "test";
int startPage = 1;
int recordsPerPage = 10;
clientService.query(key, startPage,recordsPerPage);
EasyMock.verify(mockService);
}
}
错误现象,第一个报NullPointException,
java.lang.NullPointerException
at ClientServiceTest.query_page_error(ClientServiceTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)