使用JUnit在Maven2中的类加载问题 [英] Classloading issues in maven2 with JUnit

查看:125
本文介绍了使用JUnit在Maven2中的类加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用maven2构建的项目,并针对该代码运行了一系列JUnit测试用例.到现在为止,这一直很好,在这里,我现在有2个测试必须按一定的顺序运行才能使事情正常工作,比如说TestA和Test(从A到B).不幸的是,maven2不了解这一点,因此我正在寻找一种说服它需要按此顺序运行测试的方法.

I have a project that builds with maven2 and runs a series of JUnit test cases against the code. This has worked fine up until this point, where I now have 2 tests that must run in a certain sequence for things to work correctly, let's say TestA and Test (A then B). Unfortunately, maven2 doesn't understand this, so I'm looking for a way of convincing it that it needs to run the tests in this order.

问题是我要在TestB中设置一些最终的静态字段,但是我是从TestA进行此操作的,TestA本身使用这些字段,并且测试的成功执行取决于将这些字段设置为新值(绝对没有办法,否则我早就走这条路了.因此,必须首先加载TestA,这当然会导致在尝试访问TestB时加载它.但是,maven2决定先运行TestB,然后运行TestA,这意味着这些最后的字段已经设置并且不能更改.

The problem is that I'm setting some final static fields in TestB, but I'm doing this from TestA, which itself uses those fields, and successful execution of the test depends on those fields being set to their new values (there is absolutely no way around this, otherwise I would have taken that road long before now). So, it is imperative that TestA loads first, and it will of course cause TestB to be loaded when tries to access it. However, maven2 has decided that it will run TestB then TestA, which means those final fields are already set and cannot be changed.

所以我正在寻找的是指定测试执行顺序的方法(每次都从A到B),或者是一种容易导致JUnit使用的类加载器重新加载TestB的方法.

So what I'm looking for is either a way to specify the order in which the tests are executed (A then B, every time), or a way to easily cause TestB to be reloaded by whichever classloader that JUnit uses.

编辑-一个其他选项可能是某些选项,例如旧的JUnit GUI工具所具有的选项,该选项将导致为每个测试重新加载所有类.我看了看又看,在maven junit插件中找不到这样的标记,如果存在这样的东西,那也行得通.

EDIT - one other option might be some option like the old JUnit GUI tool has, that causes all classes to be reloaded for each test. I have looked and looked and not found such a flag in the maven junit plugin, if such a thing exists, then that would also work.

推荐答案

JUnit中的测试顺序是故意未定义的,这不是Maven问题,很幸运的是,到目前为止您的测试还可以运行.

The test order in JUnit is intentionally undefined, this is not a Maven issue and it's probably just luck that your tests were running OK up until now.

Sal的答案直接解决了您的问题,但是在每个测试中分叉JVM以获取大量测试数量会极大地增加您的构建时间.

Sal's answer addresses your question directly, however forking the JVM on each test for large test counts can hugely increase your build time.

另一种方法是使用测试库,例如 PowerMock (目前与EasyMock和Mockito一起使用)以清除TestB的初始化中的静态字段,从而避免了任何JVM派生的需要,并确保您的测试可移植.

An alternative approach would be to use a testing library such as PowerMock (it currently works with EasyMock and Mockito) to clear the static fields in TestB's initialisation, this avoids the need for any JVM forking, and ensures your tests are portable.

在PowerMock网站上:

From the PowerMock website:

PowerMock是一个框架,它以更强大的功能扩展了其他模拟库,例如EasyMock. PowerMock使用自定义的类加载器和字节码操作来启用静态方法,构造函数,最终类和方法,私有方法,删除静态初始化程序等的模拟.通过使用自定义类加载器,无需对IDE或持续集成服务器进行任何更改,从而简化了采用过程.熟悉EasyMock的开发人员会发现PowerMock易于使用,因为对于静态方法和构造函数,整个期望API都是相同的. PowerMock使用少量方法和注释扩展了EasyMock API,以启用其他功能.从1.1版开始,PowerMock还具有对Mockito的基本支持.

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more. By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption. Developers familiar with EasyMock will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors. PowerMock extends the EasyMock API with a small number of methods and annotations to enable the extra features. From version 1.1 PowerMock also has basic support for Mockito.

这篇关于使用JUnit在Maven2中的类加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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