簡體   English   中英

jUnit測試中的調用方法

[英]Calling methods from jUnit test

以下是我的jUnit測試,試圖從中afterPropertiesSet在OSGiFramework類中調用afterPropertiesSet 但是以某種方式在該流程中不會調用getBundlesInformation方法。

當我調試我的jUnit測試時,調用afterPropertiesSet方法,然后轉到initializeModelFramework方法,然后再轉到getBundlesInformation方法。

@Test
public void testOSGiFramework() {

    Method method;
    try {
        method = OSGiFramework.class.getDeclaredMethod("afterPropertiesSet", null);
        Object o = method.invoke(new OSGiFramework(), null);

        Assert.assertEquals(false, o instanceof Void);
    }
}

以下是OSGiFramework類中的方法-

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

@Override
public void afterPropertiesSet() throws Exception {

    try {
        initializeModelFramework();
    }
}

private void initializeModelFramework() {

    final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(
            new Runnable() {
                public void run() {
                    try {
                        getBundlesInformation();
                    }catch(Exception ex) {
                        LOG.log(Level.SEVERE, "Exception in OSGiFramework::initializeModelFramework " +ex);
                        ex.printStackTrace();
                    }
                }
            }, 0, 30, TimeUnit.MINUTES);
}

protected static void getBundlesInformation() throws BundleException, Exception {
    System.out.println("Hello");
}

有誰知道可能是什么問題?

我認為,由於您沒有啟動線程(您定義的Runnable對象),因此run方法將永遠不會執行。 您必須啟動線程才能執行run方法。

好吧,getBundlesInformation()方法正在不同的線程上運行,那么誰能說它在assert方法或測試之后不能運行呢? 無論哪種方式,似乎問題都在於嘗試對Junit測試線程應用程序。

您可能需要研究使用不同線程進行的Junit測試。 我個人從來沒有做過,但是經過Google的快速搜索后,看來這不是一件很簡單的任務,而且可能更容易避免/將測試分解,因此他們不必處理不同的線程。

抱歉,如果此答案不再有用-如果更有經驗的人看到此答案,請嘗試給出更好的答案!

一些快速的搜索將我帶到了:

單元測試多線程應用程序?

JUnit中的線程行為異常

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM