0

我正在尝试使用 dbunit 和 junit。这些测试是不涉及数据库的基本 junit 测试。但是,该课程在到达那里之前就失败了。当调用onSetup()from 方法时IDatabaseTester,它会抛出 NullPointerException 而不是我所期望的:删除数据库,从dataset.xml.

为什么我得到一个空指针异常?

 29 public class TestModifyDb
 30 {
 31 
 32   private IDatabaseTester dbtester;
 33 
 34   @Before
 35   public void setUp() throws Exception
 36   {
 37     dbtester = new JdbcDatabaseTester("com.mysql.jdbc.Driver",
 38                                       "jdbc:mysql://localhost/cal",
 39                                       "cal",
 40                                       "cal");
 41 
 42     IDataSet dataSet = new FlatXmlDataSetBuilder().build(
 43         new FileInputStream("src/gps/tasks/task3663/dataset.xml"));
 44 
 45     dbtester.onSetup();
 46   }
 47 
 48   @After
 49   public void tearDown() throws Exception
 50   {
 51     dbtester.onTearDown();
 52   }
 53 
 54   @Test(expected=IndexOutOfBoundsException.class)
 55   public void outOfBounds()
 56   {
 57     new ArrayList<Object>().get(1);
 58   }
 59 
 60   @Test (expected=IndexOutOfBoundsException.class)
 61   public void elementAt()
 62   {
 63     int[] intArray = new int[10];
 64     int i = intArray[20];
 65   }
 66 
 67 }

这是堆栈跟踪:

java.lang.NullPointerException
    at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:92)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
    at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
    at gps.tasks.task3663.TestModifyDb.setUp(TestModifyDb.java:45)
    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:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    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)
4

1 回答 1

4

我怀疑你只是错过了这一行:

dbTester.setDataSet(dataSet);

(在初始化之后dataSet,但在调用之前onSetup。)

目前你dataSet在创建它之后什么都没有做......

于 2012-05-16T21:15:38.357 回答