EasyMock.expect() 抛出空指针
EasyMock.expect() throwing null pointer
我在我的项目中使用 PowerMock 和 EasyMock。调用 Easymock.expect() 方法调用时出现空指针异常。
JUnit class:
import static org.easymock.EasyMock.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({InfoDao.class})
public class AdminUtilTest {
//mocking interface
@Mock
InfoDao infoDaoMock;
@Test
public void testSetUp() {
assertNotNull(infoDaoMock);
}
@Test
public void initInfoTest() throws Exception {
// getting null pointer exception in this line..
expect(infoDaoMock.getGroupInfoById(isA(Long.class))).andReturn("testString");
replay(infoDaoMock);
/*rest of the code*/
}
}
testSetUp 不为空,assertNotNull
成功。
我也试过:
InfoDao infoDaoMock = createMock(InfoDaoMockImpl.class);
这也是抛NullPointerException
。我在这里做错了什么?
您可能使用了错误的导入
你需要
import org.powermock.api.easymock.annotation.Mock;
import static org.powermock.api.easymock.PowerMock.replay;
我让代码正常工作。问题出在 Long.class。我手动给出了一些示例 long 值,并且正在加载模拟对象。
expect(infoDaoMock.getGroupInfoById(123456L)).andReturn("testString");
我在我的项目中使用 PowerMock 和 EasyMock。调用 Easymock.expect() 方法调用时出现空指针异常。
JUnit class:
import static org.easymock.EasyMock.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({InfoDao.class})
public class AdminUtilTest {
//mocking interface
@Mock
InfoDao infoDaoMock;
@Test
public void testSetUp() {
assertNotNull(infoDaoMock);
}
@Test
public void initInfoTest() throws Exception {
// getting null pointer exception in this line..
expect(infoDaoMock.getGroupInfoById(isA(Long.class))).andReturn("testString");
replay(infoDaoMock);
/*rest of the code*/
}
}
testSetUp 不为空,assertNotNull
成功。
我也试过:
InfoDao infoDaoMock = createMock(InfoDaoMockImpl.class);
这也是抛NullPointerException
。我在这里做错了什么?
您可能使用了错误的导入
你需要
import org.powermock.api.easymock.annotation.Mock;
import static org.powermock.api.easymock.PowerMock.replay;
我让代码正常工作。问题出在 Long.class。我手动给出了一些示例 long 值,并且正在加载模拟对象。
expect(infoDaoMock.getGroupInfoById(123456L)).andReturn("testString");