明らかな1つのアプローチは、FlexContextなどを挿入できるようにリファクタリングすることです。ただし、これが常に可能であるとは限りません。しばらく前、私が参加していたチームは、アクセスできない内部クラスのもの(コンテキストなど)をモックアウトしなければならない状況に陥りました。最終的に、静的呼び出しを含む個々のメソッドを効果的にモックできるjmockitというAPIを使用することになりました。
このテクノロジーを使用して、非常に厄介なサーバー実装を回避でき、ライブサーバーにデプロイしてブラックボックステストを行うのではなく、ハードコーディングされた効果的なサーバーテクノロジーをオーバーライドすることで、細かいレベルで単体テストを行うことができました。
jmockitのようなものを使用することについて私が行う唯一の推奨事項は、テストコードに、メインのモックフレームワークからのjomockitの明確なドキュメントと分離があることを確認することです(easymockまたはmockitoが私の推奨事項です)。そうしないと、パズルの各部分のさまざまな責任について開発者を混乱させるリスクがあります。これは通常、質の低いテストやうまく機能しないテストにつながります。理想的には、最終的には、jmockitコードをテストフィクスチャにラップして、開発者がそれについてさえ知らないようにします。ほとんどの人にとって、1つのAPIを処理するだけで十分です。
これは、IBMクラスのテストを修正するために使用したコードです。基本的に2つのことをする必要があります。
- メソッドによって返される独自のモックを注入する機能があります。
- 実行中のサーバーを探しに行ったコンストラクターを強制終了します。
- ソースコードにアクセスせずに上記を実行します。
コードは次のとおりです。
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();
}
}