JUnit in Eclipse throws null pointer exception

I looked at other jUnit null point exceptions and none of them are suitable for what I am experiencing. I am running Eclipse 4.3.1 with Ubuntu 12.04.

I created a jUnit class with the Eclipse wizard and thought I was following the whole tutorial. The code:

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

}

The msg error I get is pretty concise;

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

This three-minute tour has turned into a 3-hour tour ...

Thoughts?

EDIT:

I changed the setVideoSource preamble to @Before (although it is not required to run before most other calls), and I changed the Start preamble to @After, which requires you to first set the video source.

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

I still get the same exception.

Just a note: I have a non-jUnit test driver that currently works great, I'm trying to improve my skills (and add discipline) to

Awareness - ( ), VideoAnalytics ( ). , jUnit, , .

2:

jUnit, , AwarenessAnalytic ( , ).

3:

testSetVideoSource init, (- ).

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

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, , , - .

?

+4
3

AA.start() , , AA.addListener(new DetectionListener());, @Before. AA.start() @Test. , @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 , :

  • AA.start();
  • AA.addListener(new DetectionListener());
  • AA.stop()
+2

, , . , .

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");
        }
    }
+3

- , , .

@Before 
public void method()

.

@BeforeClass 
public static void method()

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

EDIT: Perhaps you should switch to a JUnit dependency instead of the one you are using. I'm not sure that all the other libraries with the same annotations may be the reason that you get a null pointer exception if you mix different test environments. Make sure you have the JUnit library in the build path, and not just as an import statement.

+1
source

Source: https://habr.com/ru/post/1529401/


All Articles