繁体   English   中英

Mockito.verify 和 Mockito.doNothing 在 Junit 测试用例中不起作用

[英]Mockito.verify and Mockito.doNothing are not working in Junit Test cases

我已经发布了符合我使用 Mockito.verify() 或 doNothing() 编写创建方法测试用例的要求的示例代码。 我想验证或 doNothing () 在 ClassB 的 create() 方法没有命中 DB 时返回。我已经尝试过对我不起作用的不同方式,并且在测试 Class 中对输出进行了评论。任何帮助将不胜感激

 public class ClassA {
        ClassB b=new ClassB();

        public TestPojo create(TestPojo t) {        
            TestPojo t2= b.create(t);       
            return t2;          
        }    
    }

    public class ClassB {

    public TestPojo create(TestPojo t) {
        System.out.println("class b");
            return t;       
        }

    }

public class TestPojo {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

    public class ClassTest {


            @Test
            public void testCreate_1()
                throws Exception {
                ClassA testA=Mockito.spy(new ClassA());
                ClassB testB=Mockito.spy(new ClassB());         
                TestPojo test=Mockito.spy(new TestPojo());
                test.setId(1);
                //Mockito.verifyZeroInteractions(testB); -- testcase is passed but executing ClassB method but this not our intension
                //Mockito.verify(testB).create(test); --error output : Wanted but not invoked
                //Mockito.doNothing().when(testB).create(test); --error output : Only void methods can doNothing()!
                TestPojo act=testA.create(test);
                //System.out.println(act.getId());

            }
        @Before
        public void setUp()
            throws Exception {
        }   
        @After
        public void tearDown()
            throws Exception {
            // Add additional tear down code here
        }
        public static void main(String[] args) {
            new org.junit.runner.JUnitCore().run(ClassTest.class);
        }

    }

它对您不起作用,因为创建的实例testB实际上并未分配给ClassA中的字段b 要使其正常工作,您需要设置模拟实例并添加验证检查。

@Test
public void testCreate_1() throws Exception {
    ClassA testA = Mockito.spy(new ClassA());
    ClassB testB = Mockito.mock(ClassB.class);
    // set spied instance to testA
    testA.b = testB;
    // you don't need to spy on test, as you're not verifying anything on it
    TestPojo test = new TestPojo();
    test.setId(1);
    // execute your test method
    TestPojo act = testA.create(test);
    // verify that required create method with proper argument was called on testB 
    Mockito.verify(testB, Mockito.times(1)).create(Matchers.eq(test));
    // verify that nothing else executed on testB
    Mockito.verifyNoMoreInteractions(testB);
}

希望能帮助到你!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM