jba*_*ndi 8 java testing refactoring static
我有以下遗留代码:
public class MyLegacyClass
{
private static final String jndiName = "java:comp/env/jdbc/LegacyDataSource"
public static SomeLegacyClass doSomeLegacyStuff(SomeOtherLegacyClass legacyObj)
{
// do stuff using jndiName
}
}
Run Code Online (Sandbox Code Playgroud)
这个类在J2EE-Container中工作.
现在我想测试容器外面的类.
什么是最好的策略?基本上允许重构.
允许访问LegacyDataSource(测试不必是"纯"单元测试).
编辑:不允许引入其他运行时框架.
只是为了让@ Robin建议更具体的策略模式:(请注意,原始问题的公共API保持不变.)
public class MyLegacyClass {
private static Strategy strategy = new JNDIStrategy();
public static SomeLegacyClass doSomeLegacyStuff(SomeOtherLegacyClass legacyObj) {
// legacy logic
SomeLegacyClass result = strategy.doSomeStuff(legacyObj);
// more legacy logic
return result;
}
static void setStrategy(Strategy strategy){
MyLegacyClass.strategy = strategy;
}
}
interface Strategy{
public SomeLegacyClass doSomeStuff(SomeOtherLegacyClass legacyObj);
}
class JNDIStrategy implements Strategy {
private static final String jndiName = "java:comp/env/jdbc/LegacyDataSource";
public SomeLegacyClass doSomeStuff(SomeOtherLegacyClass legacyObj) {
// do stuff using jndiName
}
}
Run Code Online (Sandbox Code Playgroud)
...和JUnit测试.我不是必须进行这种设置/拆卸维护的忠实粉丝,但这是一个基于静态方法(或Singletons)的API的不幸副作用.我做喜欢这个测试是它不使用JNDI -这是很好的,因为(a)它会跑得快,和(b)单元测试应该只无论如何测试在doSomeLegacyStuff()方法中的业务逻辑,而不是测试实际数据来源.(顺便说一句,这假设测试类与MyLegacyClass在同一个包中.)
public class MyLegacyClassTest extends TestCase {
private MockStrategy mockStrategy = new MockStrategy();
protected void setUp() throws Exception {
MyLegacyClass.setStrategy(mockStrategy);
}
protected void tearDown() throws Exception {
// TODO, reset original strategy on MyLegacyClass...
}
public void testDoSomeLegacyStuff() {
MyLegacyClass.doSomeLegacyStuff(..);
assertTrue(..);
}
static class MockStrategy implements Strategy{
public SomeLegacyClass doSomeStuff(SomeOtherLegacyClass legacyObj) {
// mock behavior however you want, record state however
// you'd like for test asserts. Good frameworks like Mockito exist
// to help create mocks
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4430 次 |
| 最近记录: |