PowerMock 断言错误

PowerMock AssertionError

我对以下测试有疑问。 FileUtil class 是我要测试的class。

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 = "";
    }
  }
}

第一个测试方法(testSanitizeFilename)执行成功,但第二个(testSanitizeProcessedFilename)抛出以下错误:

Expected exception: java.net.MalformedURLException
    java.lang.AssertionError
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:88)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

我不确定我做错了什么。我尝试了 PowerMock.verifyAll() 和 PowerMock.replayAll(),但仍然出现错误。也许我把代码放在一个循环中是错误的?但我想不出另一种方法来实现这一点。任何帮助将不胜感激。

你的测试方法被注释为

@Test(expected = MalformedURLException.class)

这意味着它希望测试抛出这个异常。正如您在评论中所说 - 不会抛出此异常,因此:

Expected exception: java.net.MalformedURLException
    java.lang.AssertionError

要修复它,如果您不希望您的测试抛出异常,只需从测试注释中删除 (expected = MalformedURLException.class)