2017-09-20 81 views
0

我使用JUnit Jupiter版本5.0.0(發佈版本),我試圖使用測試發現功能。 Junit的文檔可以在7.1.1中找到。從http://junit.org/junit5/docs/5.0.0/user-guide/#launcher-api-discovery使用JUnit 5發現測試不執行LoggingListener(實現TestExecutionListeners)

我發現執行測試爲:

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); 
    } 

    ... 

} 

和我的測試類是:

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 

可能是一個錯誤?我正在執行錯誤?

回答

3

爲什麼你會期望LoggingListener.forJavaUtilLogging()創建的監聽器記錄日誌級別爲INFO ......當文檔明確指出以下內容?

創建使用FINE日誌級別LoggingListener委託給一個java.util.logging.Logger

如果你想LoggingListener記錄在INFO級別的消息,你就必須使用它接受這樣LoggingListener.forJavaUtilLogging(Level.INFO)日誌級別的其他工廠方法來創建它。

+0

謝謝,它正在工作。我嘗試了LoggingListener.forJavaUtilLogging(Level.ALL);它並沒有打印任何東西來控制。 java.util.logging.Level的Java文檔聲明:級別ALL可用於啓用所有消息的日誌記錄,但此級別不起作用。但與Level.INFO是好的。感謝Sam – Sergio

相關問題