EasyMock、EasyMock Class Extension 和 PowerMock

EasyMock大多数同事都用 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 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 的版本。

文章未经特殊标明皆为本人原创,未经许可不得用于任何商业用途,转载请保持完整性并注明来源链接 《四火的唠叨》

11,173 次阅读

发表评论

电子邮件地址不会被公开。

back to top