简体   繁体   English

测试方法中的Easymock多种方法调用

[英]Easymock multiple method calls inside the test method

So I am using easymock to write unit test fpr a method inside my class. 因此,我正在使用easymock在类内部编写方法的单元测试。 I have mocked an object inside this method and it works fine. 我已经在此方法中模拟了一个对象,并且效果很好。 The problem is inside this test method I have several calls to other methods inside the same class. 问题出在这个测试方法内部,我在同一个类中多次调用其他方法。

For example, 例如,

public class testClass { 
    public void testMethod() { 
        mockedObject.method(); --> I define a return type for this and it works fine.
        this.otherMethod() {
        --> Inside this method I have a call to the mocked object with different methods..
    }
}

So my question is that, when the method calls another method by using this.otherMethod() should I define all the return values for the mockedObject calls inside otherMethod? 所以我的问题是,当该方法通过使用this.otherMethod()调用另一个方法时,我是否应该在otherMethod内定义mockedObject调用的所有返回值? Isn't there a way to specify a return value for otherMethod so that it won't even go inside the other method? 是否没有办法为otherMethod指定返回值,以便它甚至不会进入其他方法? Plz note that here this is not the mockedObject and that is why I cannot use eaymock return value for that. 请注意,这里不是模拟对象,这就是为什么我不能使用eaymock返回值的原因。 I was thinking maybe there might be a way around this. 我当时在想也许有办法解决这个问题。

Thanks. 谢谢。

Update: I found this for partial mocking of methods: http://www.easymock.org/EasyMock2_2_2_ClassExtension_Documentation.html Which helps to only mock specific methods. 更新:我发现这是为了部分模拟方法: http : //www.easymock.org/EasyMock2_2_2_ClassExtension_Documentation.html仅有助于模拟特定方法。 But when I try to use it, it crosses a line on createMock method.. Sth like wrong syntax! 但是,当我尝试使用它时,它在createMock方法上越过一行。

Thanks everyone, 感谢大家,

I found this link: http://fczaja.blogspot.com/2011/11/easymock-create-partial-mocks.html 我找到了此链接: http : //fczaja.blogspot.com/2011/11/easymock-create-partial-mocks.html

The answer is to partially mock those methods called within this method to isolate those method calls. 答案是部分模拟在此方法内调用的那些方法,以隔离这些方法调用。

Thanks again. 再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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