简体   繁体   English

Eclipse junit测试发现

[英]Eclipse junit test discovery

In eclipse - the junit test runner will find this test: 在eclipse中 - junit测试运行器将找到此测试:

class Blah { 
    public static class Tests {
        [Test] void testOne() {}
}}

but it won't find this test 但它不会找到这个测试

class Blah<T> { 
    public static class Tests {
        [Test] void testOne() {}
}}

Can anyone explain why, and is it possible to fix that in some way? 任何人都可以解释原因,是否有可能以某种方式解决这个问题?

I can't explain why eclipse won't accept generic classes to run as JUnit test. 我无法解释为什么eclipse不接受通用类作为JUnit测试运行。 This might be a bug, but I could not find anything on the issue tracker. 这可能是一个错误,但我在问题跟踪器上找不到任何东西。

There are now two ways to work around your problem. 现在有两种方法可以解决您的问题。 You can manually create a run-configuration for your test and start this. 您可以手动为测试创建运行配置并启动它。 The other way is to use a test suite. 另一种方法是使用测试套件。 Add a new class to your test-package containing the following 在包含以下内容的测试包中添加一个新类

package com.prodyna.packetcapture.text;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ Blah.class })
public class AllTests {

}

You can run this normally as JUnit-test and it will run all test classes that you specify in the SuiteClasses annotation. 您可以将其正常运行为JUnit-test,它将运行您在SuiteClasses批注中指定的所有测试类。

This is a bit of a strange one. 这有点奇怪。 Eclipse seems to be a bit inconsistent with this one. Eclipse似乎与这个有点不一致。

First of all, we need to say that it doesn't really make sense to have a generic test like this. 首先,我们需要说这样的通用测试没有任何意义。 JUnit (and Eclipse) instantiates this class using reflection, so it doesn't care about the generic part. JUnit(和Eclipse)使用反射来实例化这个类,因此它不关心通用部分。 It probably won't work as you expect. 它可能不会像你期望的那样工作。

To explain the Eclipse part, Eclipse has two stages for executing a JUnit test: it detects whether or not the class is a JUnit test and it then executes it. 为了解释Eclipse部分,Eclipse有两个执行JUnit测试的阶段:它检测该类是否是JUnit测试,然后执行它。 It's the first part that is preventing you from running the test. 这是阻止您运行测试的第一部分。 If you create the generic test: 如果您创建通用测试:

public class GenericTest<T> {
  @Test public void foobar() {
    System.out.println("GenericTest");
  }
}

Then the option 'Run as->JUnit test' isn't available on this class. 然后,此类上没有“Run as-> JUnit test”选项。 If you remove the <T>, it becomes available and you can run it; 如果你删除<T>,它就可用了,你可以运行它; it appears in the JUnit view. 它出现在JUnit视图中。 This is as you say. 这就像你说的那样。

The inconsistent part is when you put the back, you can now re-execute the test from the JUnit view. 不一致的部分是当您放回时,您现在可以从JUnit视图重新执行测试。

Eclipse重新运行JUnit测试

So the execution of the test actually works when the class is generic. 因此,当类是通用的时,测试的执行实际上是有效的。 However, you can't execute it normally in Eclipse. 但是,您无法在Eclipse中正常执行它。

For info, the JUnit view can't really prevent you from doing this, because it stores the class name in the run configuration: see How does Eclipse actually run Junit tests? 有关信息,JUnit视图无法阻止您执行此操作,因为它将类名存储在运行配置中:请参阅Eclipse如何实际运行Junit测试? .

I don't think you'll get very far if you raise an issue, because as I said using a generic class in this way doesn't really make sense. 如果你提出一个问题,我认为你不会走得太远,因为我说用这种方式使用泛型类并没有多大意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM