Java Eclipse中的jUnit正在引发空指针异常

Java Eclipse中的jUnit正在引发空指针异常,java,eclipse,exception,junit,Java,Eclipse,Exception,Junit,我查看了其他jUnit空点异常,它们似乎都没有解决我遇到的问题。我正在用Ubuntu 12.04运行Eclipse4.3.1 我已经用Eclipse向导建立了一个jUnit类,并认为我已经完成了所有教程。代码是 import static org.junit.Assert.*; import java.util.concurrent.TimeUnit; import java.io.IOException; import org.openhab.action.videoanalytics.A

我查看了其他jUnit空点异常,它们似乎都没有解决我遇到的问题。我正在用Ubuntu 12.04运行Eclipse4.3.1

我已经用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
这三分钟的跑步已经变成了“三小时之旅”

想法

编辑:

我将setVideoSource前导更改为@Before(尽管在大多数其他调用之前不强制运行),并将Start前导更改为@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);
    }
}
我仍然得到同样的例外

@Before
public void init() {
    try {
        AA.setVideoSource(cameraId, videoStream, format, motionType);
    } catch (IOException e) {
        fail("IOException in setVideoSource");
    }
}
请注意:我有一个非jUnit测试驱动程序,它目前工作得非常出色,我正试图通过

分析感知(Analytics Aware)是一个基本线程(目前),为每个摄像头生成一个视频分析线程(目前只有一个在测试中)。该组件集目前正在使用我的非jUnit驱动程序,提供我现在需要的结果

编辑2:

我刚刚重新运行了我现有的非jUnit测试驱动程序,它仍然正常运行,Awarenessanalysis类的行为正常(当然还有一些非异常逻辑错误需要解决)

编辑3:

将testSetVideoSource方法更改为init,结果没有更改(仍然引发空指针异常)

编辑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
我只是试着在Debug中运行它,也作为一个JUnit测试,认为在其他任何事情发生之前,同样的异常会立即发生


想法?

一个常见的错误是,您希望测试在彼此之后运行。每个测试都是完全独立的,所以不要期望其他运行的状态

尝试将构造函数逻辑放入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");
        }
    }
public类三分钟运行{
字符串cameraId=“cam1test”;
字符串videoStream=”http://xxx.xxx.xxx.xxx/mjpg/video.mjpg“;//此联机问题的ip已混淆
字符串格式=“MJPG”;
MotionDetectionType motionType=MotionDetectionType.basic;

AwarenessAnalytics AA;//您可以在每次测试之前使用它执行某些操作,但我不确定您是否需要它

@Before 
public void method()
此方法在任何测试开始之前执行一次

@BeforeClass 
public static void method()
这看起来也有点麻烦

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

AA.setImageStoreLocation(directoryStore);如果要在执行每个目标方法之前执行
AA.start()
,如
AA.addListener(new DetectionListener());
,则应在
注释之前使用
@。
然后可以在执行每个
@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
test时,将按如下顺序调用它:

  • AA.start();
  • AA.addListener(新的DetectionListener());
  • AA.stop()

  • stacktrace中的更多行将非常有用这是错误消息中出现的所有内容,即使在单击“详细信息”之后。还有其他地方我应该找吗?这是我到目前为止唯一的测试。或者你是说测试中的@test调用?每个带有@test注释的方法都将在一个单独的实例中运行。哦,那可能就是原因,让我重新构造并重试。谢谢,rekire!即使在mods之前和之后,我仍然会收到相同的错误。请参阅更新的说明。我以前的
    >
    之后的
    @都不适用。请尝试将构造函数逻辑放入每次测试开始时调用的init方法中。这对我很有帮助。好的,我尝试过(请参阅编辑更新),但我仍然得到了完全相同的结果(简写)exception.What@Test case/method您特别运行了吗?在Eclipse中,我右键单击Explorer中的三分钟测试类,然后单击->run As->jUnit Test。这是否正确,或者我应该采取其他步骤?我尝试了一个更简单的测试,结果相同,即使在Debug中运行(甚至不会启动)时也是如此.thinks?我的意思是,不要试图作为单个测试用例一次运行整个类。首先单独运行每个@testcase,例如,您可以选择“testSetVideoSource”然后右键单击并选择“运行方式”并查看它的工作方式谢谢,尽管我仍然收到相同的异常。我删除了额外的分号,尽管仍然收到相同的异常。感谢您指出这一点。当您说“切换到JUnit依赖项”时,您的意思是我应该将当前的测试驱动程序删除到其他位置吗?我目前正在对.start()调用进行注释(Eclipse对此没有任何抱怨),但这可能仍然是问题的根源吗?我尝试了一个更简单的测试,结果相同,即使在调试中运行(甚至不会启动).Thinks?请确保检查您的构建路径中是否有JUnit库,而不仅仅是作为导入语句。它位于库的下面,有一个JUnit 4标题。有趣的是,在那下面是;-JUnit.jar(在我的Yoxos目录下作为插件ing-org.hamcrest.org.jar,也在我的Yoxos目录下作为插件。我相信这是一个dJUnit插件,在我运行常规JUnit测试时也会出现。这是否有干扰?我也尝试运行dJUnit选项,但它给出了相同的结果。