Java PowerMock断言错误
我对下面的测试有问题。FileUtil类是我要测试的类Java PowerMock断言错误,java,junit,powermock,Java,Junit,Powermock,我对下面的测试有问题。FileUtil类是我要测试的类 import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.apache.commons.io.FilenameUtils; import static org.easymock.EasyMock.expect; import static org.powermoc
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FilenameUtils;
import static org.easymock.EasyMock.expect;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import org.junit.After;
import org.junit.Before;
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;
import static org.junit.Assert.assertEquals;
@RunWith(PowerMockRunner.class)
@PrepareForTest({FileUtil.class, FilenameUtils.class})
public class TestBeanTest {
private Map<String, String> fileUrls = new HashMap<>();
private final String contentType = "image/jpeg";
private final String defaultName = "default";
@Before
public void setUp() {
}
@After
public void tearDown() {
fileUrls.clear();
}
@Test
public void testSanitizeFilename(){
fileUrls.put("", "");
fileUrls.put(" ", " ");
fileUrls.put("jpg", "jpg.jpeg");
mockStatic(FilenameUtils.class);
for(Map.Entry<String, String> entry : fileUrls.entrySet()){
expect(FilenameUtils.getExtension(entry.getKey())).andReturn(entry.getValue().substring(entry.getValue().lastIndexOf(". ") + 2)).anyTimes();
expect(FilenameUtils.getBaseName(entry.getKey())).andReturn(entry.getValue().substring(entry.getValue().lastIndexOf(".") + 1)).anyTimes();
PowerMock.replay();
String result = FileUtil.sanitizeFilename(entry.getKey(), defaultName, contentType);
assertEquals(result, entry.getValue());
PowerMock.verify();
}
}
@Test(expected = MalformedURLException.class)
public void testSanitizeProcessedFilename() throws MalformedURLException{
fileUrls.put("http://www.test.com", "image.jpeg");
fileUrls.put("http://www.test.com/", "image.jpeg");
mockStatic(FilenameUtils.class);
for(Map.Entry<String, String> entry : fileUrls.entrySet()){
String fileName = processUrl(entry.key());//throws MalformedURLException
expect(FilenameUtils.getExtension(entry.getKey())).andReturn(entry.getValue().substring(entry.getValue().lastIndexOf(". ") + 2)).anyTimes();
expect(FilenameUtils.getBaseName(entry.getKey())).andReturn(entry.getValue().substring(entry.getValue().lastIndexOf(".") + 1)).anyTimes();
PowerMock.replay();
String result = FileUtil.sanitizeFilename(fileName, defaultName, contentType);
assertEquals(result, entry.getValue());
PowerMock.verify();
}
}
private String processUrl(String url){
URL url = new URL(entry.getKey());
String path = url.getPath();
String fileName;
int lastIndexOfSlash = path.lastIndexOf("/");
if (lastIndexOfSlash >= 0) {
fileName = path.substring(lastIndexOfSlash + 1);
} else {
fileName = "";
}
}
}
我不确定我做错了什么。我尝试了PowerMock.verifyAll()和PowerMock.replayAll(),但仍然出现错误。也许我把代码放在循环中是不对的?但我想不出另一种方法来实现这一点。任何帮助都将不胜感激。您的测试方法带有注释
@Test(expected = MalformedURLException.class)
这意味着它期望测试抛出这个异常。正如您在评论中所说,不会引发此异常,因此:
Expected exception: java.net.MalformedURLException
java.lang.AssertionError
要修复它,如果您不希望您的测试抛出异常,只需从测试注释中删除
(expected=malformedurexception.class)
,堆栈跟踪对我来说非常清楚,您认为@test(expected…
是什么意思(doc)?还有什么是processUrl
和TestBean
?我更新了代码。TestBean在这里不起作用。我删除了相关变量。我想知道我的代码是否有什么问题。我看到了异常,但我无法弄清楚到底是什么问题。没有抛出错误的DurException,stacktrace表明它是异常的我期待了一个,但是得到了一个AssertionError。谢谢。我明白了。非常感谢!我想如果测试方法有可能引发异常,我应该添加这个注释。
Expected exception: java.net.MalformedURLException
java.lang.AssertionError