简体   繁体   English

EasyMock:如何测试此方法

[英]EasyMock: How to test this method

I am new to development environment in Java and want to understand how to write a UT for this kind of method in Java using EasyMock. 我是Java开发环境的新手,并且想了解如何使用EasyMock在Java中为这种方法编写UT。

public class MyClass{
   public ClassB classBObj;

   public int myMethod(SomeClass someClassObj){

     ClassA objA = new ClassA();
     objA.addParam(classBObj);
     ClassC classCObj = objA.getClassCObj(classBObj);

     return someClassObj.getResult(classCObj);

  }

}

I can create mocks of SomeClass, ClassB but how to mock behavior of ClassA and ClassC ? 我可以创建SomeClass,ClassB的模拟,但是如何模拟ClassA和ClassC的行为? Basically I want to define behaviour of ClassA ie, "addParam" and " getClassCObj" . 基本上我想定义ClassA的行为,即“ addParam”和“ getClassCObj”。 How can I do this ? 我怎样才能做到这一点 ?

I need to test "myMethod" of this "MyClass" Thanks. 我需要测试这个“ MyClass”的“ myMethod”谢谢。

Because the ClassA object is instantiated within the method, you're not going to be able to mock it with EasyMock. 由于ClassA对象是在方法中实例化的,因此您将无法使用EasyMock模拟该对象。

If you're happy to not mock the ClassA object, then you can add any expectations needed for the mocked instance of ClassB and then use a capture to check that the ClassC object has been built as expected. 如果您不愿意模拟ClassA对象,则可以添加模拟的ClassB实例所需的所有期望值,然后使用capture功能检查是否已按预期构建了ClassC对象。

So your test would look something like this: 因此,您的测试将如下所示:

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.easymock.Capture;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

public class MyClassTest {
    private MyClass myClass;
    private SomeClass mockSomeClassObj;
    private ClassB mockClassBObj;

    @Before
    public void setUp() throws Exception {
        this.mockSomeClassObj = EasyMock.createMock(SomeClass.class);
        this.mockClassBObj = EasyMock.createMock(ClassB.class);

        this.myClass = new MyClass();
        this.myClass.classBObj = this.mockClassBObj;
    }

    @Test
    public void thatMethodDoesExpectedThings() {
        //Add some expectations for how the mockClassBObj is used within the addParam and getClassCObj methods

        final Capture<ClassC> classCCapture = new Capture<ClassC>();
        EasyMock.expect(this.mockSomeClassObj.getResult( EasyMock.capture(classCCapture) ) ).andReturn(9);

        EasyMock.replay(this.mockClassBObj, this.mockSomeClassObj);

        final int result = this.myClass.myMethod(this.mockSomeClassObj);
        assertThat(result,  is(9));

        EasyMock.verify(this.mockClassBObj, this.mockSomeClassObj);

        final ClassC classCobject = classCCapture.getValue();
        //Some assertions about the classC object
    }
}

Having said all of that, it is possible to use PowerMock to mock the constructor for the ClassA class (assuming you're allowed to use PowerMock) 综上所述 ,可以使用PowerMock模拟ClassA类的构造函数(假设您被允许使用PowerMock)

See the root documentation for powermock here and the specific docs for the Constructor mocking here 请参阅此处的powermock文档以及此处的有关Constructor 模拟的特定文档。

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

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