doNothing()方法调用实际方法

时间:2018-02-07 17:40:37

标签: java unit-testing junit mockito powermockito

我在调用最终的void方法时尝试doNothing(),但是doNothing()调用正在调用实际的方法。

SwitchWorker类

UrlDecode

切换班级

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
    }    
}

我是否遗漏了doNothing()方法的任何内容?

 @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()调用实际的finalVoidMethod()而不是mocking。但是,switchMock是一个Mocked对象。

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

谢谢。

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

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

使用测试代码:

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

3 个答案:

答案 0 :(得分:0)

我运行了你的例子,它正在为我工​​作。这里要记住两件事。

  1. 当您模拟对象时,模拟对象的方法将不会被执行,并且如果它们具有任何返回类型,则默认返回null。对于无效方法,您不必明确设定期望。

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

  3. 请检查您是否兼容mockito&amp ;; 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>
    

    测试代码

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

答案 1 :(得分:0)

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

更改:

Switch switchMock = mock(Switch.class);

to(或更改导入):

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

解决了我的问题。

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

答案 2 :(得分:0)

doNothing()方法将调用实际方法,如果您不想调用,则可以使用suppress-method。它将抑制不返回方法的调用。

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

更改

suppress(method(SwitchWorker.class, "finalVoidMethod"));
相关问题