使用JUnit 5发现测试不会执行LoggingListener(TestExecutionListeners的实现)

时间:2017-09-20 19:02:22

标签: java unit-testing junit5

我正在使用JUnit Jupiter 5.0.0版(发行版),我正在尝试使用测试发现功能。 Junit的文档可以在7.1.1中找到。从http://junit.org/junit5/docs/5.0.0/user-guide/#launcher-api-discovery

发现测试

我的实施是:

import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;

import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.LoggingListener;

public class MainPrueba {

    public static void main(String[] args) throws InterruptedException {

        Runnable task = () -> {
            System.out.println("Runing thread INI");

            LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(
                        selectPackage("org.package.qabootfx.test.ping")
                        //,selectClass(QabootfxApplicationTests.class)
                )
                .filters(
                    //includeClassNamePatterns(".*Test")
                        includeClassNamePatterns(".*")
                )
                .build();

            Launcher launcher = LauncherFactory.create();

            TestPlan testPlan = launcher.discover(request);

            for (TestIdentifier root : testPlan.getRoots()) {
                System.out.println("Root: " + root.toString());

                for (TestIdentifier test : testPlan.getChildren(root)) {
                    System.out.println("Found test: " + test.toString());
                }
            }

            // Register a listener of your choice
            //TestExecutionListener listener = new SummaryGeneratingListener();
            TestExecutionListener listener = LoggingListener.forJavaUtilLogging(); //new LoggingListener();
            launcher.registerTestExecutionListeners(listener);


            launcher.execute(request);

            System.out.println("Runing thread END");

        };
        new Thread(task).start();

        Thread.sleep(5000);
        System.out.println("END");
    }

}

检查LoggingListener类的实现,我们可以看到这必须打印到控制台的结果。例如:

package org.junit.platform.launcher.listeners;

@API(status = MAINTAINED, since = "1.0")
public class LoggingListener implements TestExecutionListener { 

    ....

    @Override
    public void testPlanExecutionStarted(TestPlan testPlan) {
        log("TestPlan Execution Started: %s", testPlan);
    }

    @Override
    public void testPlanExecutionFinished(TestPlan testPlan) {
        log("TestPlan Execution Finished: %s", testPlan);
    }

    ...

}

我的Test类是:

public class QabootfxApplicationTest {

    @Test
    public void testAbout() {
        System.out.println("TEST Execution....  QabootfxApplicationTests.testAbout()");

        assertEquals(4, 5, "The optional assertion message is now the last parameter.");
    }
}

我期待在控制台中看到类似于:

的内容
2017-09-20 10:53:48.041  INFO 11596 --- TestPlan Execution Started: ....
2017-09-20 10:53:48.041  INFO 11596 --- TestPlan Execution Finished: ....

但我看不到任何与“... TestPlan Execution Started ...”相似的内容。

控制台输出是:

Runing thread INI
Root: TestIdentifier [uniqueId = '[engine:junit-jupiter]', parentId = null, displayName = 'JUnit Jupiter', legacyReportingName = 'JUnit Jupiter', source = null, tags = [], type = CONTAINER]
Found test: TestIdentifier [uniqueId = '[engine:junit-jupiter]/[class:org.package.qabootfx.test.ping.QabootfxApplicationTest]', parentId = '[engine:junit-jupiter]', displayName = 'QabootfxApplicationTest', legacyReportingName = 'org.package.qabootfx.test.ping.QabootfxApplicationTest', source = ClassSource [className = 'org.package.qabootfx.test.ping.QabootfxApplicationTest', filePosition = null], tags = [], type = CONTAINER]
TEST Executon....  QabootfxApplicationTests.testAbout()
Runing thread END
END

可能是个错误?我正在实施错误?

1 个答案:

答案 0 :(得分:4)

为什么您希望LoggingListener.forJavaUtilLogging()创建的侦听器在日志级别INFO记录任何内容...当文档明确说明以下内容时?

  

创建LoggingListener,使用java.util.logging.Logger的日志级别委派给FINE

如果您希望LoggingListener在级别INFO上记录消息,则必须使用其他工厂方法创建消息,该方法接受类似此LoggingListener.forJavaUtilLogging(Level.INFO)的日志级别。