简体   繁体   English

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

[英]JUnit missing behavior definition for STATIC method call

This is my test method: 这是我的测试方法:

@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);
    }
}

And this is the method to be tested: 这是要测试的方法:

public class FileUtil {

     //code

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

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

         return fileName;
     }
}

The code works only for the cases that the condition in FileUtil.sanitizeFilename is not true. 该代码仅适用于FileUtil.sanitizeFilename中的条件不为真的情况。 If it is true (by passing a filename > 100 characters), then the following error occurs: 如果为真(通过传递文件名> 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$1.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$1.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$3.run(ParentRunner.java:238)
         at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
         at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
         at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
         at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:238)
         at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
         at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
         at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
         at org.junit.runners.ParentRunner$2.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)

What is incorrect in my code? 我的代码有什么不正确?

EDIT: I used Parameterized class in order to avoid the for loop. 编辑:为了避免for循环,我使用了Parameterized类。

I used PowerMock.mockStaticPartial for the static methods getBaseName and getExtension. 我将PowerMock.mockStaticPartial用于静态方法getBaseName和getExtension。

How it worked for me 它如何为我工作

@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();
    }
}

Note that I modified sanitizeFilename some to get working code for me. 请注意,我修改了sanitizeFilename以获得我的工作代码。 And I used 我用

@Rule
public PowerMockRule rule = new PowerMockRule();

instead of RunWith - but as I had all your errors before this might fix your stuff as well. 而不是RunWith但由于我在解决所有问题之前也遇到了所有错误。

Seems like mocking inside the loop was no good idea to begin with - once I moved the mocks out of the loop it got me somewhere. 似乎在循环中进行模拟并不是一个好主意-一旦我将模拟从循环中移出,它就会带我到某个地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IllegalStateException:在同一方法中有两次调用时缺少行为定义 - IllegalStateException: missing behavior definition when having a double call in same method 即使定义了行为,“ IllegalStateException:在方法调用之前缺少行为定义” - 'IllegalStateException: missing behavior definition for preceeding method call' even though behavior is defined 上述方法调用缺少行为定义:用法是:expect(a.foo())。和XXXX() - missing behavior definition for the preceding method call:Usage is: expect(a.foo()).andXXX() java.lang.IllegalStateException:缺少前面方法调用 getMessage(“title”) 的行为定义 - java.lang.IllegalStateException: missing behavior definition for the preceding method call getMessage(“title”) java.lang.IllegalStateException:前面的方法调用getLast(...)的行为定义缺失 - java.lang.IllegalStateException: missing behavior definition for the preceding method call getLast(…) JUnit assertTrue()方法调用,是否缺少对象? - JUnit assertTrue() method call, is it missing an object? 方法的jUnit更改行为 - jUnit change behavior of a method 来自JUnit测试的静态方法调用,无需指定类 - Static method call from JUnit test without specifying class junit4:如何在junit4 @before或@Test中调用其他类的静态方法? - junit4: How to call the other class static method in junit4 @before or @Test? JUnit:忽略方法调用 - JUnit: Ignore method call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM