首页 文章

doNothing()方法调用实际方法

提问于
浏览
0

我在调用final void方法时尝试doNothing(),但doNothing()调用正在调用实际方法 .

SwitchWorker类

public class SwitchWorker {

    public boolean switchMethod(String id){
        this.methodA(id);
        return true;
    }

    public void methodA(String id){
        final Switch switchObj = Switch.getSwitchInstance(); 
        switchObj.finalVoidMethod(id);
        // XYZ......
    }
}

开关类

public final class Switch {
    private static final Switch INSTANCE = new Switch();
    private Switch() { //Private Constructor
        super();
    }
    public static Switch getSwitchInstance() {
        return INSTANCE;
    }
    public final synchronized void finalVoidMethod(String param) {
        // Do Something
    }    
}

测试代码:

@RunWith(PowerMockRunner.class)
 @SuppressStaticInitializationFor("com.application.switching.Switch")
 public class SwitchWorkerTest {

        @Test
        public void test() throws Exception {
            SwitchWorker swObj = new SwitchWorker();

            final String channelId = "0001";

            Switch switchMock = mock(Switch.class);
            PowerMockito.mockStatic(Switch.class);
            when(Switch.getSwitchInstance()).thenReturn(switchMock);

            doNothing().when(switchMock).finalVoidMethod(channelId);

            boolean result = swObj.switchMethod(channelId);
            assertTrue(result);                            
        }

我错过了doNothing()方法的任何内容吗?

"doNothing().when(switchMock).finalVoidMethod(channelId);"

doNothing()调用实际的finalVoidMethod()而不是mocking . 但是,switchMock是一个Mocked对象 .

我已经查看了关于PowerMockito的堆栈溢出,模拟静态方法和doNothing方法的几乎所有问题,我不确定我是否遗漏了任何内容,因为我已经按照大多数示例来构建代码 . 我也添加了@PrepareForTest但后来删除了它 .

谢谢 .

编辑:我剪断了代码更抽象,稍微改变以适应@kswaughs评论

public boolean mockedMethod(final String input) {
    System.out.println("0----------------------");
    final Switch switchObj = Switch.getSwitchInstance();
    System.out.println("1:switchObj: "+ switchObj);
    switches.refresh("input");
    System.out.println("2----------------------");
    return true;
}

有了测试代码:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Switch.class, SwitchWorker.class})
public class SwitchWorkerTest {
    @Test
    public void test() throws Exception {
        SwitchWorker swObj = new SwitchWorker();

        Switch switchMock = mock(Switch.class);
        PowerMockito.mockStatic(Switch.class);
        when(Switch.getSwitchInstance()).thenReturn(switchMock);
        boolean result = swObj.mockedMethod("0001");
        assertTrue(result);

回报 .

0----------------------
1:switchObj: Mock for Switch, hashCode: 1659309731

java.lang.NullPointerException
at com.application.switching.Switch.refresh(Switch.java:238)
at com.application.switching.SwitchWorker$1.run(SwitchWorker.java:51)

2 回答

  • 0

    我跑了你的例子,它正在为我工作 . 这里要记住两件事 .

    • 模拟对象时,模拟对象的方法不会被执行,默认情况下,如果它们具有任何返回类型,则返回null . 对于void方法,您不必明确设置期望 .

    • 要模拟最终类,必须在prepareForTest中声明它 .

    请检查您是否兼容mockito和powermockito版本 .

    我按如下方式运行了你的例子 .

    Maven

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>1.6.6</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito</artifactId>
      <version>1.6.6</version>
      <scope>test</scope>
    </dependency>
    

    Test code

    package com.kswaughs;
    
    import static org.powermock.api.mockito.PowerMockito.mock;
    import static org.powermock.api.mockito.PowerMockito.when;
    
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(Switch.class)
    public class SwitchWorkerTest {
    
       @Test
       public void test() throws Exception {
           SwitchWorker swObj = new SwitchWorker();
    
           final String channelId = "0001";
    
           Switch switchMock = mock(Switch.class);
           PowerMockito.mockStatic(Switch.class);
           when(Switch.getSwitchInstance()).thenReturn(switchMock);
    
           boolean result = swObj.switchMethod(channelId);
           Assert. assertTrue(result);                            
       }
    }
    
  • 0

    在更改了使用PowerMockito的所有方法后,我意识到模拟对象本身是使用Mockito初始化的 .

    更改:

    Switch switchMock = mock(Switch.class);
    

    到(或改变进口):

    Switch switchMock = PowerMockito.mock(Switch.class);
    

    解决了我的问题 .

    感谢贡献的人(像这样的小事是我的祸根)

相关问题