Junit 无法使用PowerMock模拟构造函数
在下面的代码中,我无法使用PowerMock模拟构造函数。 我想嘲弄下面的陈述Junit 无法使用PowerMock模拟构造函数,junit,powermock,easymock,Junit,Powermock,Easymock,在下面的代码中,我无法使用PowerMock模拟构造函数。 我想嘲弄下面的陈述 APSPPortletRequest wrappedRequest = new APSPPortletRequest(request); @PrepareForTest({APSPPortletRequest.class}) @RunWith(PowerMockRunner.class) public class ReminderPortletControllerTest { private Portlet
APSPPortletRequest wrappedRequest = new APSPPortletRequest(request);
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
下面是我的嘲弄步骤
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
请就此向我提出建议。因为你想模仿这句台词
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
APSPPortletRequest wrappedRequest = new APSPPortletRequest(request);
此对象创建调用只接受一个参数,但在测试方法中模拟时,您将两个值传递给expectNew
方法
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
事实上你应该这样做
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
PowerMock.expectNew(APSPPortletRequest.class, EasyMock.anyObject(requestClass.class)).andReturn(apspPortletRequestMock).anyTimes();
通过这样做,您告诉编译器,只要在类APSPPortletRequest上调用“new”操作符并将request类的任何对象作为参数,就会返回一个模拟实例apspPortletRequestMock
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
您还缺少一点,即您需要重播所有Easymock对象。。i、 e.EasyMock.replay(…)代码>也需要存在
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
希望这有帮助
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
祝你好运 如果要模拟以下方法:
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
EncryptionHelper EncryptionHelper=新的EncryptionHelper(“cep”,true)
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
您可以通过这种方式使用powerMock来实现
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
一,。导入类
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
导入静态org.powermock.api.support.membermodification.MemberMatcher.method
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
导入静态org.powermock.api.support.membermodification.MemberModifier.stub
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
二,。在junit测试计算器上方添加注释@RunWith和@PrepareForTest
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
@PrepareForTest({EncryptionHelper.class})
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
3.嘲笑它
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
EncryptionHelper encryptionHelperMock=PowerMock.createMock(EncryptionHelper.class)
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
expectNew(EncryptionHelper.class,isA(String.class),EasyMock.anyBoolean()).andReturn(encryptionHelperMock)
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
4.回复
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
PowerMock.replayAll(encryptionHelperMock)
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}
我按照上述步骤操作,工作正常。您能提供错误信息,或者提供一些关于具体问题的提示吗
@PrepareForTest({APSPPortletRequest.class})
@RunWith(PowerMockRunner.class)
public class ReminderPortletControllerTest {
private PortletRequest requestMock;
private APSPPortletRequest apspPortletRequestMock;
public void setUp() throws Exception {
requestMock = EasyMock.createNiceMock(PortletRequest.class);
apspPortletRequestMock = EasyMock.createNiceMock(APSPPortletRequest.class);
}
@Test
public void testExecuteMethod() throws Exception {
PowerMock.expectNew(APSPPortletRequest.class, requestMock).andReturn(apspPortletRequestMock).anyTimes();
EasyMock.replay(apspPortletRequestMock, requestMock);
PowerMock.replayAll();
}
}