Why do I get a NullPointerExeption for testManuscript when trying to run my test?
This is my Manuscript.java:
package org.lhoffjann;
public class Manuscript {
private String msID;
private String path;
public void setMSid(String msID){
this.msID = msID;
}
public String getMSid() {
return this.msID;
}
}
This is my ManuscriptTest.java:
package org.lhoffjann;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ManuscriptTest {
private static Manuscript testManuscript;
@Before
public void setUp(){
testManuscript = new Manuscript();
}
@Test
public void testGetMSid() {
testManuscript.setMSid("1234");
assertTrue("1234" == testManuscript.getMSid());
}
}
"1234" == testManuscript.getMSId()does not work - that is comparing reference identity, not value equality. It's"1234".equals(testManuscript.getMSid()), however, the better way isassertEquals("1234", testManuscript.getMSid()), as now JUnit will show you character-by-character where the differences are, if any. Also,getMSid()is not the proper style. It'd begetMsId(). Java code style dictates:.getDvdPlayer(), not.getDVDPlayer().