2014-02-28 36 views
4

我看了看其他jUnit空點異常,並沒有一個似乎解決我遇到的問題。我使用Ubuntu 12.04運行Eclipse 4.3.1。jUnit在Eclipse中拋出空指針異常

我已經用Eclipse嚮導設置了一個jUnit類,並且認爲我遵循了所有的教程。代碼是;

import static org.junit.Assert.*; 

import java.util.concurrent.TimeUnit; 
import java.io.IOException; 

import org.openhab.action.videoanalytics.AwarenessTypes.*; 
import org.junit.Test; 

public class ThreeMinuteRun { 

String cameraId = "cam1test"; 
String videoStream = "http://xxx.xxx.xxx.xxx/mjpg/video.mjpg"; //ip obfuscated for this online question 
String format = "MJPG"; 
MotionDetectionType motionType= MotionDetectionType.basic; 
AwarenessAnalytics AA = new AwarenessAnalytics(); 
String maskFilename = "/home/will/odev/MotionDetect/src/main/resources/DrivewayMaskWhite.png"; 
MotionDetectionState motionDetectionState = MotionDetectionState.on; 
String directoryStore = "/media/PENDRIVE/alertImageTest/"; 

@Test 
public void testSetVideoSource() { 
    try { 
     AA.setVideoSource(cameraId, videoStream, format, motionType); 
    } catch (IOException e) { 
     fail("IOException in setVideoSource"); 
    } 
} 

@Test 
public void testAddListener() { 
    AA.addListener(new DetectionListener()); 
} 

@Test 
public void testSetFramesToExaminePerSecond() { 
    AA.setFramesToExaminePerSecond(cameraId, 1); 
} 

@Test 
public void testSetMotionDetectionType() { 
    AA.setMotionDetectionType(cameraId, motionType); 
} 

@Test 
public void testSetImageStoreLocation() { 
    AA.setImageStoreLocation(directoryStore);; 
} 

@Test 
public void testSetsecsUntilMotionCeasedOption() { 
    AA.setsecsUntilMotionCeasedOption(7); 
} 

@Test 
public void testSetSizeSensitivity() { 
    AA.setSizeSensitivity(cameraId, 4); 
} 

@Test 
public void testSetDebugState() { 
    AA.setDebugState(true, 10); 
} 

@Test 
public void testSetMotionDetectionState() { 
    AA.setMotionDetectionState(cameraId, motionDetectionState); 
} 

@Test 
public void testSetLightFalseAlarmAdjustment() { 
    AA.setLightFalseAlarmAdjustment(cameraId, 5); 
} 

@Test 
public void testSetMaskImage() { 
    AA.setMaskImage(cameraId, maskFilename); 
} 

@Test 
public void testSetMaskState() { 
    AA.setMaskState(cameraId, true); 
} 

@Test 
public void testGetAlertImages() { 
    fail("Not yet implemented"); 
} 

@Test 
public void testGetAlertVideo() { 
    fail("Not yet implemented"); 
} 

@Test 
public void testStart() { 
    AA.start(); 
    //then sleep for 3 minutes 
    try { 
     TimeUnit.SECONDS.sleep(360000); 
    } catch (Exception e) { 
     // Q. Should this be passed up or just ignored if it happens occasionally? 
     //throw new IOException ("Sleep problem ", e); 
     System.out.println(e); 
    } 
} 

/**@Test 
public void testRun() { 
    fail("Not yet implemented"); 
}*/ 



@Test 
public void testStop() { 
    AA. stop(); 
} 

} 

我得到的錯誤信息是比較簡潔的;

An internal error occurred during: "Launching ThreeMinuteRun (1)". 
java.lang.NullPointerException 

這個三個分鐘的運行變成了 「3小時旅遊」 ......

的思考?

編輯:

我改變了setVideoSource序言@Before(雖然它不是強制性的大多數其他調用之前運行),我改變了開始序言@After,這確實要求視頻源首先被設置。

@Before 
public void testSetVideoSource() { 
    try { 
     AA.setVideoSource(cameraId, videoStream, format, motionType); 
    } catch (IOException e) { 
     fail("IOException in setVideoSource"); 
    } 
} 

@After 
public void testStart() { 
    AA.start(); 
    //then sleep for 3 minutes 
    try { 
     TimeUnit.SECONDS.sleep(360000); 
    } catch (Exception e) { 
     //throw new IOException ("Sleep problem ", e); 
     System.out.println(e); 
    } 
} 

我仍然得到相同的異常。

剛一說明:我有一個目前正寄望非JUnit測試驅動程序,我想提高我的技能(並添加紀律)由

Analytics(分析)的認識是一個基本的主題(現在)爲每臺攝像機生成VideoAnalytics線程(此時只有一個在測試中)。該組件集目前正在使用我的非jUnit驅動程序,提供我現在需要的結果。

編輯2:

我只是重新跑我現有的非JUnit測試驅動程序,它仍然是正常的話,與AwarenessAnalytic類運作正常(少數非異常邏輯錯誤合作,通過,自然)。

EDIT 3:

改變了testSetVideoSource方法初始化,並在結果(仍然拋出空指針異常)沒有變化。

@Before 
public void init() { 
    try { 
     AA.setVideoSource(cameraId, videoStream, format, motionType); 
    } catch (IOException e) { 
     fail("IOException in setVideoSource"); 
    } 
} 

EDIT 4:

我創建了一個更基本的測試從第一測試的拷貝以上,除去所有的,除了以下的測試程序;

@Before 
public void init() { 
    try { 
     AA.setVideoSource(cameraId, videoStream, format, motionType); 
    } catch (IOException e) { 
     fail("IOException in setVideoSource"); 
    } 
} 

@Test 
public void testAddListener() { 
    AA.addListener(new DetectionListener()); 
} 

@After 
public void testStart() { 
    AA.start(); 
    //then sleep for 3 minutes 
    try { 
     TimeUnit.SECONDS.sleep(360000); 
    } catch (Exception e) { 
     //throw new IOException ("Sleep problem ", e); 
     System.out.println(e); 
    } 
} 

我現在得到的錯誤信息實際上是相同的,儘管JUnit測試類名稱後沒有「(1)」;

An internal error occurred during: "Launching ThreeMinuteRunSimple". 
java.lang.NullPointerException 

我只是想在調試運行此,也可作爲JUnit測試,認爲同樣的異常發生時向右走,什麼都發生之前。

想法?

+0

多一點從棧跟蹤線將有幫助 –

+0

這就是你所有的一切p在錯誤信息中,即使在點擊「詳細信息」後也是如此。有什麼地方我應該看? – Will

回答

2

如果您想要在執行AA.start()之前執行每個目標方法(如AA.addListener(new DetectionListener());),則應使用@Before註釋。 然後你可以在執行每個@Test目標方法之前執行AA.start()方法。如果在測試後執行該方法,則應使用@After註釋。

public class ThreeMinuteRun { 

    ... 

    @Before 
    public void setUp() throws Exception { 
     AA.start(); 
    } 

    @After 
    public void tearDown() throws Exception { 
     AA.stop(); 
    } 

    ... 

    @Test 
    public void testAddListener() { 
     AA.addListener(new DetectionListener()); 
    } 
} 

testAddListener測試調用時,它會在順序調用如下:

  1. AA.start();
  2. AA.addListener(new DetectionListener());
  3. AA.stop()
+0

好吧,我試過了(請參閱編輯更新),儘管我仍然獲得完全相同的(簡潔)異常。 – Will

+0

哪個@Test案例/方法是否特別運行? – mahesh

+0

在Eclipse中,我右鍵單擊資源管理器中的ThreeMinuteTest類,然後 - >運行方式 - > jUnit測試。這是正確的,還是我應該採取其他措施? – Will

3

一個常見的錯誤是你期望測試是彼此依次運行的。每個測試都是完全獨立的,所以不要期望來自其他運行的狀態。

嘗試將您的構造函數邏輯放在您在每次測試開始時調用的init方法中。這幫助了我。如果這不起作用直接嘗試這與你的其他常數值。

public class ThreeMinuteRun { 

    String cameraId = "cam1test"; 
    String videoStream = "http://xxx.xxx.xxx.xxx/mjpg/video.mjpg"; //ip obfuscated for this online question 
    String format = "MJPG"; 
    MotionDetectionType motionType= MotionDetectionType.basic; 
    AwarenessAnalytics AA; // <--------------- CHANGED ------------- 
    String maskFilename = "/home/will/odev/MotionDetect/src/main/resources/DrivewayMaskWhite.png"; 
    MotionDetectionState motionDetectionState = MotionDetectionState.on; 
    String directoryStore = "/media/PENDRIVE/alertImageTest/"; 

    private void init() { 
     AA = new AwarenessAnalytics(); 
    } 

    @Test 
    public void testSetVideoSource() { 
     init(); 
     try { 
      AA.setVideoSource(cameraId, videoStream, format, motionType); 
     } catch (IOException e) { 
      fail("IOException in setVideoSource"); 
     } 
    } 
+0

這是迄今爲止唯一的測試。或者你是否指測試中的@Test調用? – Will

+1

每個使用@Test註釋的方法都將在seperat實例中運行。 – rekire

+0

哦,那麼這可能是原因,讓我重組並重試。謝謝,rekire! – Will

1

您可以使用此每次測試之前執行的東西,我不知道你需要這個雖然。

@Before 
public void method() 

該方法在任何測試開始前執行一次。

@BeforeClass 
public static void method() 

這看起來也有點馬車

@Test 
public void testSetImageStoreLocation() { 
    AA.setImageStoreLocation(directoryStore);; <== 
} 

編輯:也許你應該切換到JUnit的依賴,而不是你正在使用的一個。我不確定所有其他庫是否具有相同的註釋可能是您得到空指針異常的原因,如果您正在混合不同的測試框架。請務必檢查您的構建路徑中是否具有JUnit庫,而不僅僅是一條導入語句。

+0

謝謝,雖然我仍然得到同樣的例外。 – Will

+0

我刪除了額外的分號,但仍然得到相同的異常。感謝您指出了這一點。 當你說「切換到JUnit依賴項」時,是否應該將我當前的測試驅動程序移到其他位置?我正在評論.start()調用(沒有來自Eclipse的抱怨),但是這仍然是問題的根源嗎? – Will

+0

我嘗試了一個更簡單的測試,即使在Debug(甚至不會啓動)中運行時也會得到相同的結果。思考? – Will