I created a Maven project and when I run all the unit tests for the project through eclipse, all of them pass as expected. But when I do a mvn install
through a terminal, I see the following error for the test testPredictThrowsException()
:
testPredictThrowsException(com.ner.core.NamedEntityRecognizerTest) Time elapsed: 0.001 sec <<< ERROR!
java.lang.NullPointerException
at com.ner.core.NamedEntityRecognizer.train(NamedEntityRecognizer.java:70)
at com.ner.core.NamedEntityRecognizerTest.setUp(NamedEntityRecognizerTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...which means that the object ner
that I am instantiating in the @Before
setUp() method is not being run resulting it in being set to null. Here are the tests and POM.xml. Thoughts?
public class NamedEntityRecognizerTest {
NamedEntityRecognizer ner;
@Before
public void setUp() throws NamedEntityRecognizerException {
ner = new NamedEntityRecognizer();
InputStream trainingStream = NamedEntityRecognizerTest.class.getClassLoader().getResourceAsStream("trainingData1.train");
ner.train(trainingStream);
}
@Test(expected=NamedEntityRecognizerException.class)
public void testPredictThrowsException() throws NamedEntityRecognizerException {
System.out.println("NERRRR: " + ner);
ner.predict(null);
}
}
POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ner</groupId>
<artifactId>AmpelloNER</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>AmpelloNER</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<!-- more dependencies go here... -->
</dependency>
</dependencies>
</project>
UPDATE:
Just figured out that the NullPointerException was being thrown when I try getting the absolute path of a file in test/resources/
directory (See code below). It is able to find that file when I run the test through eclipse, but, when I try running it through the terminal by typing mvn install
, it throws a NullPointerException
. How can I resolve this issue?
@Before
public void setUp() throws NamedEntityRecognizerException {
ner = new NamedEntityRecognizer();
String trainingFile = NamedEntityRecognizerTest.class.getClassLoader().getResourceAsStream("trainingData1.train");
ner.train(trainingFile); // <<<---- This throws the NPE
}