我正在尝试使用dbunit和junit。这些测试是不涉及数据库的基本junit测试。该类在到达那里之前就失败了。当从onSetup()调用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)

最佳答案

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

dbTester.setDataSet(dataSet);

(在初始化dataSet之后,但在调用onSetup之前。)
目前,您在创建dataSet后不对其进行任何操作...

关于java - dbunit为onSetup()抛出NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10626698/

10-09 20:48