Java 无根测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/120889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unrooted Tests
提问by Paul Croarkin
When running all my tests in Eclipse (Eclipse 3.4 'Ganymede'), one test is listed under "Unrooted Tests". I'm using Junit 3.8 and this particular test extends TestCase. I do not see any difference between this test and the other tests. I don't remember seeing this occur in Eclipse 3.3 (Europa).
在 Eclipse(Eclipse 3.4 'Ganymede')中运行我的所有测试时,一个测试列在“Unrooted Tests”下。我正在使用 Junit 3.8,这个特定的测试扩展了 TestCase。我看不出这个测试和其他测试有什么区别。我不记得在 Eclipse 3.3 (Europa) 中看到过这种情况。
Clarification:
澄清:
We haven't moved to JUnit 4.0 yet, so we are not using annotations. I also googled and it seemed like most people were having issues with JUnit 4, but I did not see any solutions. At this point the test passes both locally and in CruiseControl so I'm not overly concerned, but curious.
我们还没有迁移到 JUnit 4.0,所以我们没有使用注解。我也用谷歌搜索过,似乎大多数人都遇到了 JUnit 4 的问题,但我没有看到任何解决方案。在这一点上,测试在本地和 CruiseControl 中都通过了,所以我并不过分担心,但很好奇。
When I first saw this, though, it was on a failing test that only failed when run with other tests. This led me down the rabbit hole looking for a solution to the "Unrooted" issue that I never found. Eventually I found the culprit in another test that was not properly tearing down.
然而,当我第一次看到这个时,它是在一个失败的测试中,只有在与其他测试一起运行时才会失败。这让我陷入了困境,寻找我从未找到的“无根”问题的解决方案。最终,我在另一个没有正确拆卸的测试中找到了罪魁祸首。
I agree, it does seem like an Eclipse issue.
我同意,这似乎是 Eclipse 问题。
回答by Jim Kiley
I've never seen this -- but as far as I can tell from skimming Google for a few minutes, this appears as though it could be a bug in Eclipse rather than a problem with your test. You don't have the @Test annotation on the test, I assume? Can you blow the test away and recreate it, and if so do you get the same error?
我从来没有见过这个——但据我浏览谷歌几分钟可以看出,这似乎可能是 Eclipse 中的一个错误,而不是你的测试问题。你在测试中没有 @Test 注释,我猜?你能把测试搞砸并重新创建它,如果是这样,你会得到同样的错误吗?
回答by laz
If your class extends TestCase somewhere in its hierarchy, you have to use the JUnit 3 test runner listed in the drop down under run configurations. Using the JUnit 4 runner (the default I believe) causes that unrooted test phenomenon to occur.
如果您的类在其层次结构中的某处扩展了 TestCase,则必须使用运行配置下的下拉列表中列出的 JUnit 3 测试运行程序。使用 JUnit 4 运行程序(我认为是默认设置)会导致发生无根测试现象。
回答by ra9r
Finally I found the solution. The problem is that you are not defining your test cases using annotations but are still doing it the "old way". As soon as you convert over to using annotations you will be able to run one test at a time again.
最后我找到了解决方案。问题是您没有使用注释定义测试用例,但仍在以“旧方式”进行操作。一旦您转换为使用注释,您就可以一次再次运行一个测试。
Here is an example of what a basic test should now look like using annotations:
以下是使用注释的基本测试现在应该是什么样子的示例:
import static org.junit.Assert.*; // Notice the use of "static" here
import org.junit.Before;
import org.junit.Test;
public class MyTests { // Notice we don't extent TestCases anymore
@Before
public void setUp() { // Note: It is not required to call this setUp()
// ...
}
@Test
public void doSomeTest() { // Note: method need not be called "testXXX"
// ...
assertTrue(1 == 1);
}
}
回答by Krishna Kumar
I could the fix the issue by shifting from TestRunner ver 4.0 to 3 in run configurations for the individual test method.
我可以通过在单个测试方法的运行配置中从 TestRunner ver 4.0 转移到 3 来解决这个问题。
回答by geo
Do not extend junit.framework.TestCase in your test class with junit1.4 and this should solve the problem
不要使用 junit1.4 在您的测试类中扩展 junit.framework.TestCase ,这应该可以解决问题
回答by Jose Manuel Prieto
You are using Hamcrest? or another library to help in your test?. You are not using
您在使用 Hamcrest 吗?或其他库来帮助您的测试?你没有使用
import static org.junit.Assert.*;
Check if in your test you use:
检查您是否在测试中使用:
import static org.hamcrest.MatcherAssert.assertThat;
or other assert isn′t JUnit assert.
或其他断言不是 JUnit 断言。
回答by Bob
I was getting the "unrooted tests" error message as well and it went away magically. I believe it was due to the fact that I was using Eclipse with a Maven project. When I added a new method to my Test class and gave it the @Test annotation, it began getting the error message when I tried to run that one method using the "Run as Junit test" menu option; however, once I ran a maven build the unrooted tests message disappeared and I believe that is the solution to the problem in the future.
我也收到了“无根测试”错误消息,它神奇地消失了。我相信这是因为我将 Eclipse 与 Maven 项目一起使用。当我向我的 Test 类添加一个新方法并给它 @Test 注释时,当我尝试使用“Run as Junit test”菜单选项运行该方法时,它开始收到错误消息;但是,一旦我运行了 Maven 构建,无根测试消息就消失了,我相信这是未来问题的解决方案。
Run a maven build because it will refresh the class that JUnit is using.
运行 Maven 构建,因为它将刷新 JUnit 正在使用的类。
回答by Ben Robie
We solved the problem by making sure our test project was built. We had an issue in the build path which would not allow our test class to be compiled. Once we resolved the build path issue, the test compiled and the "new" method was able to be run. So we can assume that "Unrooted" tests also mean that they don't exist in the compiled binary.
我们通过确保我们的测试项目已经构建来解决这个问题。我们在构建路径中遇到了一个问题,它不允许我们的测试类被编译。一旦我们解决了构建路径问题,测试就会编译,“新”方法就可以运行了。所以我们可以假设“无根”测试也意味着它们不存在于编译的二进制文件中。
回答by Kenny Cason
Another scenario that causes this problem was me blindly copy/pasting a method that requires a parameter. i.e.
导致此问题的另一种情况是我盲目复制/粘贴需要参数的方法。IE
import org.junit.Test;
public class MyTest {
@Test
public void someMethod(String param) {
// stuff
}
}
You have a few simple solutions:
您有几个简单的解决方案:
define the variable in the specific test method
add it as an instance variable to the test class
create a setup method and annotate it with
@Before
在具体的测试方法中定义变量
将其作为实例变量添加到测试类中
创建一个设置方法并用
@Before
回答by Vikrant K
It turned out to be that my build path had some error...some jars were missing. I reconfigured build path and it worked!
结果是我的构建路径有一些错误......一些jar丢失了。我重新配置了构建路径并且它起作用了!