【问题标题】:Mockito.verify and Mockito.doNothing are not working in Junit Test casesMockito.verify 和 Mockito.doNothing 在 Junit 测试用例中不起作用
【发布时间】:2020-07-25 19:02:42
【问题描述】:

我已经发布了符合我在使用 Mockito.verify() 或 doNothing() 为 create 方法编写测试用例的要求的示例代码。我想验证或在 ClassB 的 create() 方法不打 DB 时返回 doNothing()

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

    }

【问题讨论】:

    标签: java spring junit mockito junit5


    【解决方案1】:

    它对您不起作用,因为创建的实例 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);
    }
    

    希望对你有帮助!

    【讨论】:

    • 应用您的快速响应,我已经用上述更正测试了相同的测试用例,但在执行测试类时它仍然可以在控制台上从 ClassB 的 create() 打印 syso,即它仍然在执行 classB 而没有模拟。任何解决它的建议。
    • 这是我们想要的……请说明您想要达到的结果?
    • ---不要从 ClassB 执行任何方法,因为我们已经为它编写了验证方法,但是在测试上面的测试代码时 ClassB 方法正在执行。您可以看到 classB 中有一个 print 语句,它在执行不需要的测试用例时在控制台上打印。不要执行任何 ClassB 方法,只需验证它将为该请求调用多少次
    • 好的,知道了,我已经更新了答案。关键是你需要模拟它,而不是监视testB 实例。如果没有任何额外的存根,它将返回 null,但由于您不关心结果,因此可以满足您的需求。
    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2012-05-29
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多