我剛寫了抽樣檢測情況,在這裏我想嘲笑一個類型實例方法。我很驚訝,我的測試用例傳遞時沒有調用expectLastCall方法。我想知道,在調用實例void方法時,expectLastCall不需要調用嗎?嘲諷實例無效方法的工作,而無需調用「expectLastCall」的方法
StringUtil.java
package com.sample.util;
import com.sample.model.MethodNotImplementedException;
public class StringUtil {
public String toUpperAndRepeatStringTwice(String str) {
String upperCase = str.toUpperCase();
sendStringToLogger(upperCase);
return upperCase + upperCase;
}
public void sendStringToLogger(String str){
throw new MethodNotImplementedException();
}
}
StringUtilTest.java
package com.sample.util;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ StringUtil.class })
public class StringUtilTest {
@Test
public void toUpperAndRepeatStringTwice() {
StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, "sendStringToLogger");
String str = "HELLO PTR";
stringUtil.sendStringToLogger(str);
//PowerMock.expectLastCall().times(1);
PowerMock.replayAll();
String result = stringUtil.toUpperAndRepeatStringTwice("hello ptr");
assertEquals(result, "HELLO PTRHELLO PTR");
}
}
我想你會找到答案[這裏](http://stackoverflow.com/questions/22831523/easymock-void-method) –
只是好奇:你在這裏使用PowerMock? Mockito應該能夠嘲弄或窺探你的'StringUtil',而不訴諸PowerMock ......我在這裏丟失了一個用例嗎? –
我正在使用PowerMock和EasyMock。只是爲了試驗 –