0

私のプロジェクトでは、JDBC、MySQL、サーブレット、JSP を使用しています。私のdaoをテストするために、いくつかの単体テストを行い、JUnitとUnitilsを使用しようとしています。

この例外が発生しています:

    org.unitils.core.UnitilsException: Error inserting test data from DbUnit dataset for method public void com.jean.taxi.daoImpl.ClientDaoImplTest.testListModerators()
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:190)
    at org.unitils.dbunit.DbUnitModule$DbUnitListener.beforeTestSetUp(DbUnitModule.java:791)
    at org.unitils.core.Unitils$UnitilsTestListener.beforeTestSetUp(Unitils.java:273)
    at org.unitils.UnitilsJUnit4TestClassRunner$TestListenerInvokingMethodRoadie.runBeforesThenTestThenAfters(UnitilsJUnit4TestClassRunner.java:181)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.unitils.UnitilsJUnit4TestClassRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:95)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
    at org.unitils.UnitilsJUnit4TestClassRunner.access$000(UnitilsJUnit4TestClassRunner.java:42)
    at org.unitils.UnitilsJUnit4TestClassRunner$1.run(UnitilsJUnit4TestClassRunner.java:60)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:67)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
    at java.lang.String.startsWith(String.java:1385)
    at java.lang.String.startsWith(String.java:1414)
    at org.unitils.core.dbsupport.MySqlDbSupport.toCorrectCaseIdentifier(MySqlDbSupport.java:189)
    at org.unitils.core.dbsupport.DbSupport.init(DbSupport.java:97)
    at org.unitils.core.dbsupport.DbSupportFactory.getDbSupport(DbSupportFactory.java:77)
    at org.unitils.core.dbsupport.DbSupportFactory.getDefaultDbSupport(DbSupportFactory.java:56)
    at org.unitils.dbunit.DbUnitModule.getDefaultDbSupport(DbUnitModule.java:755)
    at org.unitils.dbunit.DbUnitModule.getDataSetFactory(DbUnitModule.java:725)
    at org.unitils.dbunit.DbUnitModule.getDataSetFactory(DbUnitModule.java:713)
    at org.unitils.dbunit.DbUnitModule.getDataSet(DbUnitModule.java:441)
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:261)
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:187)
    ... 21 more

私の unitils-local.propreties:

database.userName=root
database.password=111111
database.storedIndentifierCase.mysql=mixed_case
dbUnit.datasetresolver.prefixWithPackageName=false
database.identifierQuoteString.mysql=none
database.schemaNames=TEST
database.dialect=mysql
database.driverClassName=com.mysql.jdbc.Driver

私の unitils.properties:

# Name or path of the user specific properties file. This file should contain the necessary parameters to connect to the
# developer's own unit test schema. It is recommended to override the name of this file in the project specific properties
# file, to include the name of the project. The system will try to find this file in the classpath, the user home folder
# (recommended) or the local filesystem.
unitils.configuration.localFileName=unitils-local.properties

# Properties for the PropertiesDataSourceFactory
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/test

database.schemaNames=test
# This property specifies the underlying DBMS implementation. Supported values are 'oracle', 'db2', 'mysql' and 'hsqldb'.
# The value of this property defines which vendor specific implementations of DbSupport and ConstraintsDisabler are chosen.
dbUnit.datasetresolver.prefixWithPackageName=false

私のデータセットのオプション:

    <?xml version="1.0" encoding="UTF-8"?>
<dataset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <CLIENT ID="1" CLIENT_NAME="clientName" CLIENT_LAST_NAME="clientLastName"
            EMAIL="email" ADDRESS="address" PASSWORD="password"
            PHONE="phone" SECOND_PHONE="secondPhone" THIRD_PHONE="thirdPhone" CREATE_DATE="2015-10-05 08:35:25"/>
    <CLIENT_GRANT ID="1" ID_CLIENT="1" ADMIN="0" MODERATOR="0" ACTIVE="1"/>
</dataset>

そして、私がテストしようとしている私のtest-daoクラスの一部:

@Transactional(TransactionMode.ROLLBACK)
@DataSet("ClientDaoImplTest.xml")
public class ClientDaoImplTest extends UnitilsJUnit4 {
    @TestDataSource
    DataSource dataSource;

    ClientDaoImpl clientDao = null;
    ClientGrantDaoImpl clientGrantDao = null;

    @Before
    public void init() {
        clientDao = new ClientDaoImpl(dataSource);
        clientGrantDao = new ClientGrantDaoImpl(dataSource);
        TransactionHandlerImpl.setDataSource(dataSource);
        clientDao.setCurrentLocalConnection(ConnectionHolder.getLocalConnection());
        clientGrantDao.setCurrentLocalConnection(ConnectionHolder.getLocalConnection());
    }

    @After
    public void destroy() {
        clientDao = null;
        clientGrantDao = null;
    }

    @Test
    public void testAddNew() {
        final User user = new User();
        user.setEmail("email");
        user.setAddress("address");
        user.setPassword("password");
        user.setClientName("clientName");
        user.setClientLastName("clientLastName");
        user.setPhone("phone");
        final ClientGrant clientGrant = user.getClientGrant();
        clientGrant.setClientId(user.getId());
        clientGrant.setActive(true);
        user.setClientGrant(clientGrant);
        executeTest(new Transaction<Long>() {
            @Override
            public void doTransaction() throws Exception {
                Long clientId = clientDao.addNew(user);
                user.setId(clientId);
                clientGrantDao.addNew(user.getClientGrant());
                assertTrue(assertClient(clientDao.get(clientId), user));
            }
        });
    }

データベース オプションに何か問題があることはわかっています。この問題を解決する方法を見つけるのを手伝ってもらえますか? ありがとう。

更新:私の unitils-local.properties のパラメーターを変更した後: from:

database.identifierQuoteString.mysql=none

に:

database.identifierQuoteString.mysql=auto

a には新しい例外がありました:

org.unitils.core.UnitilsException: Error inserting test data from DbUnit dataset for method public void com.jean.taxi.daoImpl.ClientDaoImplTest.testAddNew()
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:190)
    at org.unitils.dbunit.DbUnitModule$DbUnitListener.beforeTestSetUp(DbUnitModule.java:791)
    at org.unitils.core.Unitils$UnitilsTestListener.beforeTestSetUp(Unitils.java:273)
    at org.unitils.UnitilsJUnit4TestClassRunner$TestListenerInvokingMethodRoadie.runBeforesThenTestThenAfters(UnitilsJUnit4TestClassRunner.java:181)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.unitils.UnitilsJUnit4TestClassRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:95)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
    at org.unitils.UnitilsJUnit4TestClassRunner.access$000(UnitilsJUnit4TestClassRunner.java:42)
    at org.unitils.UnitilsJUnit4TestClassRunner$1.run(UnitilsJUnit4TestClassRunner.java:60)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:67)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.unitils.core.UnitilsException: Error while executing DataSetLoadStrategy
    at org.unitils.dbunit.datasetloadstrategy.impl.BaseDataSetLoadStrategy.execute(BaseDataSetLoadStrategy.java:46)
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:342)
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:268)
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:187)
    ... 21 more
Caused by: org.dbunit.dataset.NoSuchColumnException: client.ID -  (Non-uppercase input column: ID) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.
    at org.dbunit.dataset.AbstractTableMetaData.getColumnIndex(AbstractTableMetaData.java:117)
    at org.dbunit.operation.AbstractOperation.getOperationMetaData(AbstractOperation.java:89)
    at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:143)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at org.unitils.dbunit.datasetloadstrategy.impl.CleanInsertLoadStrategy.doExecute(CleanInsertLoadStrategy.java:45)
    at org.unitils.dbunit.datasetloadstrategy.impl.BaseDataSetLoadStrategy.execute(BaseDataSetLoadStrategy.java:44)
    ... 24 more

たぶん、その問題についていくつかの新しい亜種またはいくつかの考えがありますか? ありがとう。

4

3 に答える 3

3

問題はここにあると思います:

database.identifierQuoteString.mysql=none

代わりに次のように設定してみてください。

database.identifierQuoteString.mysql=auto

最終的に、クラス MySqlDbSupport にはバグがあります。プロパティ ファイルでdatabase.identifierQuoteString.mysqlプロパティが に設定されている場合、クラス DbSupport で指定されたプライベート フィールドは null に設定されます。MySqlDbSupportのメソッドはこのフィールドの値を取得しますが、に渡す前に null チェックを行うのを忘れています。したがって、NullPointerException が発生します。noneidentifierQuoteStringtoCorrectCaseIdentifierString.startsWith

Usingは、使用autoする識別子引用文字列をデータベースに要求します。これ以外のものを使用したい理由がわかりません。

于 2015-11-03T21:26:22.520 に答える
2

MySQL DB で unitils を使用すると、同じ問題が発生しました。解決策は、MySQL メタデータ ハンドラーを構成することです。

org.dbunit.database.IMetadataHandler.implClassName=org.dbunit.ext.mysql.MySqlMetadataHandler

問題が古く、unitils が維持されなくなったことは知っていますが、誰かを助けることができます。

于 2016-09-17T17:39:38.993 に答える