Mocking private method of class under test using JMockit

后端 未结 3 1016
醉话见心
醉话见心 2020-12-15 23:17

I want to mock private method of a class under test but method return false first two times when the method is called after that it should return false. Here is the code wha

3条回答
  •  执念已碎
    2020-12-16 00:15

    Here, you can over-ride a particular method of the testing class with mock behavior.

    For the below code:

    public class ClassToTest 
    {
        public void methodToTest()
        {
            Integer integerInstance = new Integer(0);
            boolean returnValue= methodToMock(integerInstance);
            if(returnValue)
            {
                System.out.println("methodToMock returned true");
            }
            else
            {
                System.out.println("methodToMock returned true");
            }
            System.out.println();
        }
        private boolean methodToMock(int value)
        {
            return true;
        }
    }
    

    Test class would be:

    public class ClassToTestTest{
    
        @Test
        public void testMethodToTest(){
    
            new Mockup(){
                @Mock
                private boolean methodToMock(int value){
                    return true;
                }
            };
    
            ....    
    
        }
    }
    

提交回复
热议问题