简体   繁体   中英

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

I have posted sample code which matches to my requirement in writing test case for create method using Mockito.verify() or doNothing(). I wanted to verify or doNothing () returns when create() method of ClassB without hitting DB.I have tried myself indifferent ways not worked for me and which are commented with outputs in Test Class.Any help would be appreciated

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

    }

It doesn't work for you because created instance testB is not actually assigned to field b inside ClassA . To make it work you need to set your mocked instance and add verification checks.

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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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