EasyMock注释– JUnit 4

时间:2020-02-23 14:41:17  来源:igfitidea点击:

EasyMock批注可用于创建Mock对象。
我们还可以告诉EasyMock框架将这些模拟对象注入到另一个具体的类中。

EasyMock注释

我们应该注意两个重要的EasyMock注释:

  • @Mock:用于指定EasyMock模拟的字段。

  • @TestSubject:用于指定一个对象,我们希望EasyMock注入使用@Mock注释创建的模拟对象。

EasyMock注释示例

当我们使用EasyMock批注时,我们必须通过以下方法之一显式初始化它们。

  • @RunWith(EasyMockRunner.class):如果我们使用的是JUnit 4,可以使用它,请注意,JUnit 5仍然不支持此功能。
    如果我们将其与JUnit 5一起使用,则它将回退以使用JUnit 4运行器类。

  • org.easymock.EasyMockRule:这利用了JUnit 4规则,因此同样不能与JUnit 5一起使用。

  • EasyMockSupport.injectMocks(this):我们可以在@Before方法中使用它来告诉EasyMock注入模拟对象。
    这是JUnit 5和TestNG框架的首选方法。

让我们看一下使用EasyMock使用上述所有方法创建模拟对象的示例。

首先,我们将创建一些要模拟的类。
我将在示例中使用JUnit 4,以便展示通过EasyMock注释创建模拟对象的所有三种方式。

package com.theitroad.utils;

public interface IntegerUtils {

	int add(int x, int y);
}
package com.theitroad.utils;

public interface StringUtils {

	String reverse(String input);
	
	String convert(int i);
}

我们将创建上述接口的模拟对象,并将其注入以下具体类中。

package com.theitroad.utils;

public class MyUtils {

	private StringUtils su;
	private IntegerUtils iu;
	
	public MyUtils(StringUtils su, IntegerUtils iu) {
		this.su = su;
		this.iu = iu;
	}
	
	public int add(int i, int j) {
		return iu.add(i, j);
	}
	
	public String reverse(String s) {
		return su.reverse(s);
	}
	
	public String convert(int i) {
		return su.convert(i);
	}
}

EasyMockRunner

这是使用带有EasyMock批注的@RunWith(EasyMockRunner.class)的测试类。

package com.theitroad.easymock;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import org.easymock.EasyMockRunner;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.theitroad.utils.IntegerUtils;
import com.theitroad.utils.MyUtils;
import com.theitroad.utils.StringUtils;

@RunWith(EasyMockRunner.class)
public class EasyMockAnnotationsRunWithExample {

	@Mock StringUtils mockSU;
	@Mock IntegerUtils mockIU;

	@TestSubject MyUtils mu = new MyUtils(mockSU, mockIU);

	@Test
	public void test() {
		expect(mockIU.add(10, 10)).andReturn(20);
		expect(mockSU.convert(10)).andReturn("10");
		expect(mockSU.reverse("CAT")).andReturn("TAC");

		replay(mockSU, mockIU);

		assertEquals(20, mu.add(10, 10));
		assertEquals("10", mu.convert(10));
		assertEquals("TAC", mu.reverse("CAT"));
	}

}

请注意,行为模拟是在模拟对象上完成的。
当我们在TestSubject对象上调用方法时,将在内部调用它们。

EasyMockRule

这是使用EasyMockRule的代码片段。
我已从测试方法中删除了代码,因为它们与早期的测试方法相同。

package com.theitroad.easymock;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import org.easymock.EasyMockRule;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Rule;
import org.junit.Test;

import com.theitroad.utils.IntegerUtils;
import com.theitroad.utils.MyUtils;
import com.theitroad.utils.StringUtils;

public class EasyMockAnnotationsEasyMockRuleExample {

	@Mock StringUtils su;
	@Mock IntegerUtils iu;

	@TestSubject MyUtils mu = new MyUtils(su, iu);

	@Rule
	public EasyMockRule easyMockRule = new EasyMockRule(this);

	@Test
	public void test() {
	}

}

EasyMockSupport.injectMocks()

package com.theitroad.easymock;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import org.easymock.EasyMockSupport;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Before;
import org.junit.Test;

import com.theitroad.utils.IntegerUtils;
import com.theitroad.utils.MyUtils;
import com.theitroad.utils.StringUtils;

public class EasyMockAnnotationsInjectExample {

	@Mock
	StringUtils su;
	@Mock
	IntegerUtils iu;

	@TestSubject
	MyUtils mu = new MyUtils(su, iu);

	@Before
	public void setup() {
		EasyMockSupport.injectMocks(this);
	}
	
	@Test
	public void test() {
	}

}

这种创建模拟的方式将与JUnit 5以及TestNG测试框架一起使用。