Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito.verify和Mockito.doNothing在Junit测试用例中不起作用_Java_Spring_Junit_Mockito_Junit5 - Fatal编程技术网

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

Java Mockito.verify和Mockito.doNothing在Junit测试用例中不起作用,java,spring,junit,mockito,junit5,Java,Spring,Junit,Mockito,Junit5,在使用Mockito.verify()或doNothing()为create方法编写测试用例时,我发布了符合我要求的示例代码。我想在不点击DB的情况下验证或执行ClassB的create()方法时()返回的结果。我尝试了一些对我不起作用的方法,这些方法在测试类中用输出进行了注释。任何帮助都将不胜感激 public class ClassA { ClassB b=new ClassB(); public TestPojo create(TestPojo t) {

在使用Mockito.verify()或doNothing()为create方法编写测试用例时,我发布了符合我要求的示例代码。我想在不点击DB的情况下验证或执行ClassB的create()方法时()返回的结果。我尝试了一些对我不起作用的方法,这些方法在测试类中用输出进行了注释。任何帮助都将不胜感激

 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
实际上没有分配给
b
字段
ClassA
内部。要使其工作,您需要设置模拟实例并添加验证检查

@测试
public void testCreate_1()引发异常{
ClassA testA=Mockito.spy(新ClassA());
ClassB testB=Mockito.mock(ClassB.class);
//将间谍实例设置为testA
testA.b=testB;
//您不需要监视测试,因为您不需要验证测试中的任何内容
TestPojo test=新的TestPojo();
test.setId(1);
//执行您的测试方法
TestPojo act=testA.create(测试);
//验证是否在testB上调用了具有正确参数的必需create方法
验证(testB,Mockito.times(1)).create(Matchers.eq(test));
//验证testB上没有执行任何其他操作
Mockito.verifyNoMoreInteractions(testB);
}

希望有帮助

感谢您的快速响应,我已经用上述更正测试了相同的测试用例,但在执行测试类时,它仍然可以在控制台上打印ClassB的create()中的syso,即它仍然在执行ClassB而不进行模拟。任何解决它的建议。这是需要的。。。请澄清您想要实现哪一个结果?---不要执行ClassB中的任何方法,因为我们已经为它编写了验证方法,但在测试上述testcode时,ClassB方法正在执行。您可以看到classB中有一个print语句,它在控制台上启动,同时执行不需要的测试用例。不要执行任何classB方法,只需验证该请求将被调用多少次就可以了OK,明白了,我已经更新了答案。关键是,您需要模拟它,而不是监视
testB
实例。没有任何额外的存根,它将返回
null
,但因为您不关心结果,所以它可以满足您的需要。