我看了看其他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測試,認爲同樣的異常發生時向右走,什麼都發生之前。
想法?
多一點從棧跟蹤線將有幫助 –
這就是你所有的一切p在錯誤信息中,即使在點擊「詳細信息」後也是如此。有什麼地方我應該看? – Will