如何模拟并返回Mock中测试类方法内的方法的值?

时间:2016-12-09 07:54:37

标签: java mocking mockito easymock

测试类

class testClass{
    ClassUnderTest classUnderTest = new ClassUnderTest();
    public class testmethod1(){
        ClassNeedToMocked clasNeedToMocked=createNiceMock();
        ExpectNew(ClassNeedToMocked).andReturn(class1).anytimes();     // have to mock from two methods
        expect(classNeedToMocked.someMethod()).andReturn(returnvalue); // even return null while put .anyTimes()
        replayAll();                                                   // tried replay() and replayAll()
        Map<> reponseMap=classUnderTest.method1();
    }
}

正在测试的课程

ClassUnderTest{
    public method1(){
        ClassNeedToMocked classNeedToMocked = new ClassNeedToMocked();
        Set<String> set = method2();
    }
    public method2(){
        ClassNeedToMocked classNeedToMocked = new ClassNeedToMocked();
        returnvalue = classNeedToMocked.smoeMethod();                  // return Null here
        return returnvalue;
    }
}

上面的代码返回null,但是模拟的.expect().andReturn无效。

0 个答案:

没有答案