`

EasyMock、EasyMock Class Extension和PowerMock

阅读更多

大多数同事都用JUnit做单元测试,JUnit中对方法调用打桩(Mock)是一个麻烦事。好在EasyMock可以帮我们完成。

比如有这样一个简单的User类:

 

public class User {
	private String id;
	private String name;
        //get、set方法略
}
 

相应的,有这样一个被测类UserService:

 

public class UserService {
	private UserDAO userDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

        //被测方法getUser
	public User getUser(String id) {
		return this.userDAO.getUser(id);
	}
}

我要测试的是getUser方法,那么我不想过于关心其中要调用的userDAO实现类的行为(UserDAO本身是一个接口)。

那么,我就要使用EasyMock来模拟UserDAO的返回值,并完成测试:

 

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

public class UserTest {
	private UserService userService;
	private UserDAO userDAO;

	@Before
	public void setUp() throws Exception {
		userDAO = createMock(UserDAO.class);

		userService = new UserService();
		userService.setUserDAO(userDAO);
	}

	@Test
	public void testGetUser() {
		userDAO.getUser(anyObject(String.class));
		User user1 = new User("1", "Jack");
		User user2 = new User("2", "Lucy");
		expectLastCall().andReturn(user1).andReturn(user2);

		replay(userDAO);

		assertEquals("Jack", userService.getUser("1").getName());
		assertEquals("Lucy", userService.getUser("2").getName());
		
		verify(userDAO);
	}
}

 

可以看到,EasyMock提供了createMock方法来构造对象桩,在测试方法中,对于任何传入的参数(EasyMock提供的anyObject方法),对象桩依次返回两个我设定好的User对象(两个andReturn方法)。

之后执行重放方法replay,接下去就可以进行真正的方法测试和结果校验了。

最后,verify方法可以校验对象桩是否被调用了正确的次数(这里是两次)。

 

如果是要测试抛出异常的情形,亦提供一例供大家参考:

 

	@Test(expected = NumberFormatException.class)
	public void testGetUserParamError() {
		reset(userDAO);

		userDAO.getUser(anyObject(String.class));
		expectLastCall().andThrow(new NumberFormatException());

		replay(userDAO);

		userService.getUser("abc");
	}
 

在以前的EasyMock版本中,如果对象桩不是接口,而是一个普通类,需要使用EasyMock Class Extension来完成(代码不变,只不过所有org.easymock.EasyMock的方法调用全部要变成org.easymock.classextension.EasyMock),但是新版本中,EasyMock增强了所有EasyMock Class Extension的功能,引用官方的话说:

EasyMock Class Extension 3.1 Readme 写道
EasyMock Class Extension is now considered deprecated.
Class mocking can now and should be done directly using standard EasyMock.

看起来EasyMock是够强大的,但是,如果遇到这样的需要,它是无法完成对象桩的Mock的:

 写道
Mocking static methods
Mocking final methods or classes
Mocking private methods
Mock construction of new objects
 

这个时候,可以使用PowerMock

例如,需要Mock的UserDAO变成了如下的类,本身既是final的,方法又是static的:

 

public final class UserDAO {
	public final static User getUser(String id) {
		return null;
	}
}

那么在使用PowerMock的时候,代码需要少许修改:

 

import static org.powermock.api.easymock.PowerMock.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(UserDAO.class)
public class UserTest {
	private UserService userService;

	@Before
	public void setUp() throws Exception {
		createMock(UserDAO.class);
		userService = new UserService();
	}

	@Test
	public void testGetUser() {
		
		mockStatic(UserDAO.class);
		
		UserDAO.getUser(EasyMock.anyObject(String.class));
		User user1 = new User("1", "Jack");
		User user2 = new User("2", "Lucy");
		expectLastCall().andReturn(user1).andReturn(user2);

		replay(UserDAO.class);

		assertEquals("Jack", userService.getUser("1").getName());
		assertEquals("Lucy", userService.getUser("2").getName());

		verify(UserDAO.class);
	}
}
 

相比前文,变化主要有这几个:

  • 对于org.easymock.EasyMock的方法调用变成了org.powermock.api.easymock.PowerMock的方法调用;
  • 使用了RunWith和PrepareForTest注解;
  • 由于是静态方法,需要显示调用mockStatic方法来完成mock操作。

好,依然可以顺利完成测试。恭喜。

 

如果你在使用期间遇到和我一样的如下问题,请参考下面的解决方案:

 

1、异常:

java.lang.RuntimeException: Class mocking requires to have cglib and objenesis librairies in the classpath

请下载CGLibObjenesis的jar包。

 

关于Objenesis,拓展阅读:Objenesis is a small Java library that serves one purpose:

To instantiate a new object of a particular class.

When would you want this?

Java already supports this dynamic instantiation of classes using Class.newInstance(). However, this only works if the class has an appropriate constructor. There are many times when a class cannot be instantiated this way, such as when the class contains:

Constructors that require arguments.

Constructors that have side effects.

Constructors that throw exceptions.

As a result, it is common to see restrictions in libraries stating that classes must require a default constructor. Objenesis aims to overcomes these restrictions by bypassing the constructor on object instantiation.

 

2、异常:

java.lang.NoClassDefFoundError: org/objectweb/asm/Type

请下载ASM的jar包。

 

关于ASM,拓展阅读:ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify existing classes or dynamically generate classes, directly in binary form. Provided common transformations and analysis algorithms allow to easily assemble custom complex transformations and code analysis tools.

 

3、异常:

java.lang.NoClassDefFoundError: javassist/expr/ExprEditor

请下载Javassist的jar包。

 

关于Javassist,拓展阅读:Javassist (Java programming assistant) is a load-time reflective system for Java. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file before the JVM loads it. Unlike other similar systems, Javassist provides source-level abstraction; programmers can modify a class file without detailed knowledge of the Java bytecode. They do not have to even write an inserted bytecode sequence; Javassist instead can compile a fragment of source text on line (for example, just a single statement). This ease of use is a unique feature of Javassit against other tools.

 

4、异常:

java.lang.VerifyError: class net.sf.cglib.core.DebuggingClassWriter overrides final method visit.

ASM的jar包版本过高,请使用3.31的版本。

 

文章系本人原创,转载请注明出处和作者

 

2
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics