在遗留的junit测试套件中解决java classpath地狱 [英] Solving java classpath hell in legacy junit test suite

查看:138
本文介绍了在遗留的junit测试套件中解决java classpath地狱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个遗留的JUnit测试套件,包括以下测试:

  public class AwesomeTest {
public void testBusinessLogic(){
...
[awesome mocking library]
...
}
}

public class AmazingTest {
public void testBusinessProcess(){
...
[惊人的xml操作]
...
}
}

现在假设Awesome Mocking库依赖于Awesome BCEL字节码生成库,它包含类 org.useful.XMLClass ,此库具有XMLClass的版本1。



现在假设Amazing Xml操作依赖于包含类 org.useful.XMLClass 的Amazing Xml库,而这个库具有版本2的XML类。



还假定该类的版本2不能向后兼容版本1 - 所以哪个版本在类路径中具有更高的优先级 - 它会破坏其他版本的依赖关系。



还假设有400个测试依赖于令人敬畏的嘲笑库 - 所以重写不是一个理想的选择。



还假设一些关键的业务功能已经用惊人的xml libary构建了,而且强烈建议不要重写。



你如何解决这个类路径的情况 - 除了运行ant测试(假设你使用Ant运行它们)两次,使用两个不同的手动排序的类路径,并手动确定测试子集? (我可以使用自定义类加载器来创建一个想法 - 但是这似乎与具有蚂蚁解决方案的双重定制类路径保持一致的可维护性)

解决方案

我确实相信一个相当透明的解决方案是可以使用java代理和自定义类加载器。想法如下:


  1. 使用 Instrumentation Framework (java代理),用于在加载类时拦截类。当您检测到Awesome Mocking Library中的类时,将所有引用 org.useful.XMLClass 替换为例如 intercepted.org。有用的.XMLClass

  2. 创建一个自定义类加载器,您可以在其中检查请求的类是否为 intercepted.org.useful.XMLClass 。如果是,则加载Mocking Library使用的 XMLClass 的版本。所有其他请求可以默认处理。

使用自定义类加载器并在运行测试时附加java代理,一切都应该正确运行,就好像没有依赖冲突。


Suppose I have a legacy JUnit test suite that includes the following tests:

public class AwesomeTest {
     public void testBusinessLogic() {
     ...
     [awesome mocking library]
     ...
     }
}

public class AmazingTest {
    public void testBusinessProcess() {
    ...
    [amazing xml operation]
    ...
    }
}

Now assume that Awesome Mocking library relies on Awesome BCEL bytecode generation library which contains the class org.useful.XMLClass and this library has version 1 of XMLClass.

Now assume that Amazing Xml operation relies on Amazing Xml Library that contains the class org.useful.XMLClass and this library has version 2 of XML Class.

Also assume that version 2 of the class is not backwards compatible with version 1 - so which ever version has higher priority in the classpath - it breaks the dependencies of the other version.

Also assume that there are 400 tests that rely on awesome mocking library - so rewriting is not a desirable option.

Also assume that some critical business features have been built with amazing xml libary - and it is strongly preferred not to rewrite that.

How do you solve this classpath hell situation - apart from running the ant tests (assuming you're running them with Ant) twice with two differently manually ordered classpaths and manually determined test sub-sets? (I'm open to an idea with custom classloaders - but that seems about the same level of maintainability as the dual custom classpath with ant solution)

解决方案

I do believe a quite transparent solution is possible using a java agent and custom class loader. The idea is the following:

  1. Use the Instrumentation Framework (java agent) to intercept the classes when they are loaded. When you detect a class that is in the Awesome Mocking Library, replace all references to org.useful.XMLClass to, for example, intercepted.org.useful.XMLClass.
  2. Create a custom class loader, in which you check if the requested class is intercepted.org.useful.XMLClass. If it is, load the version of XMLClass that is used by the Mocking Library. All other requests can be handled by default.

Use the custom class loader and attach the java agent when running the tests, and everything should run correctly, as if there is no dependency conflict.

这篇关于在遗留的junit测试套件中解决java classpath地狱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆