JUnit 缺少静态方法调用的行为定义

JUnit missing behavior definition for STATIC method call

这是我的测试方法:

@RunWith(Parameterized.class)
@PrepareForTest(FilenameUtils.class)
public class Test {

    //code
    private final String datum;
    private final String expectedResult;

    public Test(String datum, String expectedResult){
        this.datum = datum;
        this.expectedResult = expectedResult;
    }

    @Parameters
public static Collection<Object[]> generateData(){
    return Arrays.asList(new Object[][] {
      { ".jpg", "productimage..jpg" },
      { "jpg", "jpg.jpeg" },
      { "thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog.jpeg", "thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjump.jpeg" }
   });
 }

    @Before
    public void setUp() {

    }

    @After
    public void tearDown() {

    }

    @Test
    public void testSanitizeFilename(){
        PowerMock.mockStaticPartial(FilenameUtils.class, "getExtension" , "getBaseName");

        expect(FilenameUtils.getExtension(datum)).andReturn("jpeg").anyTimes();
        expect(FilenameUtils.getBaseName(datum)).andReturn("productimage").anyTimes();

        PowerMock.replay(FilenameUtils.class);

        String result = FileUtil.sanitizeFilename(datum, defaultName, contentType);

        PowerMock.verify(FilenameUtils.class);

        assertEquals(result, expectedResult);
    }
}

这是要测试的方法:

public class FileUtil {

     //code

     public static String sanitizeFilename(String filename, ............) {
         //code here

         if (filename.length() > 100) {
             FilenameUtils.getExtension(fileName);
             FilenameUtils.getBaseName(fileName);
         }

         return fileName;
     }
}

该代码仅适用于 FileUtil.sanitizeFilename 中的条件不成立的情况。如果为真(通过传递文件名 > 100 个字符),则会出现以下错误:

   Unexpected method call FilenameUtils.getExtension("thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog.jpeg"):
 java.lang.AssertionError
   Unexpected method call FilenameUtils.getExtension("thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog.jpeg"):
         at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
         at org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.invoke(EasyMockMethodInvocationControl.java:91)
         at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
         at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
         at org.apache.commons.io.FilenameUtils.getExtension(FilenameUtils.java)
         at com.eurodyn.ecatalogue.util.FileUtil.sanitizeFilename(FileUtil.java:235)
         at com.eurodyn.ecatalogue.ejb.session.others.AsyncFileDownloadManagerBeanTest.testSanitizeFilename(AsyncFileDownloadManagerBeanTest.java:130)
         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.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
         at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
         at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
         at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
         at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
         at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
         at org.powermock.modules.junit4.rule.PowerMockStatement.run(PowerMockRule.java:52)
         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.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
         at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
         at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
         at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
         at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
         at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
         at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
         at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
         at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
         at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
         at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
         at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
         at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
         at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:53)
         at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
         at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
         at org.junit.runners.Suite.runChild(Suite.java:127)
         at org.junit.runners.Suite.runChild(Suite.java:26)
         at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
         at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
         at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
         at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:53)
         at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
         at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
         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)

我的代码有什么不正确的地方?

编辑: 我使用参数化 class 以避免 for 循环。

我对静态方法 getBaseName 和 getExtension 使用了 PowerMock.mockStaticPartial。

它对我有用

@Test
public void testSanitizeFilename() {
    PowerMock.mockStatic(FilenameUtils.class);
    EasyMock.expect(FilenameUtils.getExtension(anyString())).andReturn("jpeg").anyTimes();
    EasyMock.expect(FilenameUtils.getBaseName(anyString())).andReturn("jpeg").anyTimes();
    PowerMock.replayAll();

    for (Map.Entry<String, String> entry : fileUrls.entrySet()) {
        System.out.println("KEY: " + entry.getKey());
        String result = FileUtil.sanitizeFilename(entry.getKey(), entry.getValue());
        assertEquals(result, entry.getValue());     
        PowerMock.verifyAll();
    }
}

请注意,我修改了一些 sanitizeFilename 以获得适合我的工作代码。我用了

@Rule
public PowerMockRule rule = new PowerMockRule();

而不是 RunWith - 但因为我之前遇到过你的所有错误,这也可能会解决你的问题。

似乎在循环内部进行模拟并不是一个好主意 - 一旦我将模拟移出循环,它就让我到了某个地方。