显而易见的一种方法是重新分解它,让您可以注入 FlexContext 之类的东西。然而,这并不总是可能的。前段时间,我所在的一个团队遇到了一种情况,我们不得不模拟一些我们无法访问的内部类的东西(比如你的上下文)。我们最终使用了一个名为jmockit的 api ,它允许您有效地模拟单个方法,包括静态调用。
使用这项技术,我们能够绕过非常混乱的服务器实现,而不必部署到实时服务器和黑盒测试,我们能够通过覆盖有效硬编码的服务器技术来进行精细级别的单元测试。
关于使用 jmockit 之类的东西,我唯一的建议是确保在您的测试代码中有清晰的文档,并且将 jomockit 与您的主要模拟框架分开(我的建议是easymock或mockito )。否则,您可能会使开发人员对难题的每个部分的各种职责感到困惑,这通常会导致测试质量差或测试效果不佳。理想情况下,正如我们最终所做的那样,将 jmockit 代码包装到您的测试装置中,这样开发人员甚至都不知道它。对于大多数人来说,处理 1 个 api 就足够了。
只是为了它,这是我们用来修复 IBM 类测试的代码。我们基本上需要做两件事,
- 能够注入自己的模拟以由方法返回。
- 杀死一个正在寻找正在运行的服务器的构造函数。
- 在没有访问源代码的情况下执行上述操作。
这是代码:
import java.util.HashMap;
import java.util.Map;
import mockit.Mock;
import mockit.MockClass;
import mockit.Mockit;
import com.ibm.ws.sca.internal.manager.impl.ServiceManagerImpl;
/**
* This class makes use of JMockit to inject it's own version of the
* locateService method into the IBM ServiceManager. It can then be used to
* return mock objects instead of the concrete implementations.
* <p>
* This is done because the IBM implementation of SCA hard codes the static
* methods which provide the component lookups and therefore there is no method
* (including reflection) that developers can use to use mocks instead.
* <p>
* Note: we also override the constructor because the default implementations
* also go after IBM setup which is not needed and will take a large amount of
* time.
*
* @see AbstractSCAUnitTest
*
* @author Derek Clarkson
* @version ${version}
*
*/
// We are going to inject code into the service manager.
@MockClass(realClass = ServiceManagerImpl.class)
public class ServiceManagerInterceptor {
/**
* How we access this interceptor's cache of objects.
*/
public static final ServiceManagerInterceptor INSTANCE = new ServiceManagerInterceptor();
/**
* Local map to store the registered services.
*/
private Map<String, Object> serviceRegistry = new HashMap<String, Object>();
/**
* Before runnin your test, make sure you call this method to start
* intercepting the calls to the service manager.
*
*/
public static void interceptServiceManagerCalls() {
Mockit.setUpMocks(INSTANCE);
}
/**
* Call to stop intercepting after your tests.
*/
public static void restoreServiceManagerCalls() {
Mockit.tearDownMocks();
}
/**
* Mock default constructor to stop extensive initialisation. Note the $init
* name which is a special JMockit name used to denote a constructor. Do not
* remove this or your tests will slow down or even crash out.
*/
@Mock
public void $init() {
// Do not remove!
}
/**
* Clears all registered mocks from the registry.
*
*/
public void clearRegistry() {
this.serviceRegistry.clear();
}
/**
* Override method which is injected into the ServiceManager class by
* JMockit. It's job is to intercept the call to the serviceManager's
* locateService() method and to return an object from our cache instead.
* <p>
* This is called from the code you are testing.
*
* @param referenceName
* the reference name of the service you are requesting.
* @return
*/
@Mock
public Object locateService(String referenceName) {
return serviceRegistry.get(referenceName);
}
/**
* Use this to store a reference to a service. usually this will be a
* reference to a mock object of some sort.
*
* @param referenceName
* the reference name you want the mocked service to be stored
* under. This should match the name used in the code being tested
* to request the service.
* @param serviceImpl
* this is the mocked implementation of the service.
*/
public void registerService(String referenceName, Object serviceImpl) {
serviceRegistry.put(referenceName, serviceImpl);
}
}
这是我们用作测试父级的抽象类。
public abstract class AbstractSCAUnitTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
ServiceManagerInterceptor.INSTANCE.clearRegistry();
ServiceManagerInterceptor.interceptServiceManagerCalls();
}
protected void tearDown() throws Exception {
ServiceManagerInterceptor.restoreServiceManagerCalls();
super.tearDown();
}
}