I am testing a simple thing in my DB-Application. To create a product. For this I use Junit tests and here is the test that always fail, instead the expected result to turn green(passes)…
@Test
public void testCreate(){
Produkt test = new Produkt(20, "junit", "junitk", false, 900.67, true, true);
handler.createProdukt(test);
}
when I try it out simply with a main method(just creating a new Produkt and look if that works…) the create function works great and created this Produkt.
What am I doing wrong?
PS.: here is the code where I set up the tests:
@Before
public void setUp() throws SQLException, ClassNotFoundException{
try {
this.shandler = new ServiceHandler();
this.setServiceHandler(shandler);
manager.getConnection();
manager.getConnection().setAutoCommit(false);
} catch (SQLException ex) {
System.err.println("ERROR:");
ex.printStackTrace();
}
}
@After
public void tearDown() throws SQLException, ClassNotFoundException{
manager.getConnection().rollback();
manager.closeConnection();
}
UPDATE
I guess you mean the failure trace:
java.lang.NullPointerException at
tests.JUnitAbstractTests.testCreate(JUnitAbstractTests.java:35) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at
org.junit.runners.ParentRunner.run(ParentRunner.java:300) at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Assuming your code snippets are accurate, your
setUp()method instantiates ashandlerfield while yourtestCreate()test uses ahandlerfield. And the stacktrace clearly shows that you have a NullPointerException intestCreate(). So the thing that you are doing wrong is trying to call a method on an object whose value is null.