DBUnit: NoSuchColumnException Non-uppercase input column in ColumnNameToIndexes cache map. map’s column names are NOT case sensitive

Tags: , , , ,



My java application stores your information in MySql database version 8. The user information and password are stored in this database. I am implementing an integration test to test the method that validates the user in the database using dbunit. After the test method runs, the error below occurs.

Caused by: org.dbunit.dataset.NoSuchColumnException: USER.USER_ID -  (Non-uppercase input column: USER_ID) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.

I checked through MySql Workbench that both the table and the columns were created in uppercase, so the error message displayed does not make sense. Does anyone know what can be causing this exception?

testContext.xml

unitils.properties

unitils.module.hibernate.enabled=true
unitils.module.jpa.enabled=false
unitils.module.easymock.enabled=false

database.url=jdbc:mysql://192.168.0.12:3306/db_ca?useUnicode=true&characterEncoding=UTF-8&serverTimezone=America/Sao_Paulo
database.driverClassName=com.mysql.jdbc.Driver
database.userName=root
database.password=9999
database.schemaNames=db_main, db_ca
database.dialect=mysql

unitils.module.database.className=com.myapplication.test.unitils.SingleConnectionDatabaseModule
DatabaseModule.Transactional.value.default=disabled

DbUnitModule.DataSet.loadStrategy.default=org.unitils.dbunit.datasetloadstrategy.impl.CleanInsertLoadStrategy

updateDataBaseSchema.enabled=true
dbMaintainer.script.locations=src/test/dbscripts
dbMaintainer.autoCreateExecutedScriptsTable=true
dbMaintainer.generateDataSetStructure.enabled=false
dbMaintainer.preserve.schemas=

In src/test/dbscripts exists the file 001_SCRIPT_01.00.00

SET character_set_client = utf8mb4 ;
CREATE TABLE db_ca.ROLE (
  ROLE_ID INT(11) NOT NULL AUTO_INCREMENT,
  NAME VARCHAR(200) NOT NULL,
  PRIMARY KEY (ROLE_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

SET character_set_client = utf8mb4 ;
CREATE TABLE db_ca.USER (
  USER_ID INT(11) NOT NULL AUTO_INCREMENT,
  EMAIL VARCHAR(100) NULL DEFAULT NULL,
  NAME VARCHAR(150) NOT NULL,
  PASSWORD VARCHAR(200) NOT NULL,
  PRIMARY KEY (USER_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

SET character_set_client = utf8mb4 ;
CREATE TABLE db_ca.USER_ROLE (
  ROLE_ID INT(11) NOT NULL,
  USER_ID INT(11) NOT NULL,
  PRIMARY KEY (ROLE_ID, USER_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

/datasets/UserServiceTest.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataset xmlns="db_ca">
    <USER
        USER_ID = "1"
        EMAIL = "test@test.com"
        NAME = "TEST"
        PASSWORD = "e8d95a51f3af4a3b134bf6bb680a213a"
   />    
   <ROLE
        ROLE_ID = "1"
        NAME = "ADMIN"
   />   
   <USER_ROLE
        USER_ID = "1"
        ROLE_ID = "1"
   />   
</dataset>

Entities

@Entity
@Table(name = "USER", schema="db_ca")
public class User {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="USER_ID", nullable=false)
    private Long id;

    ...
}

@Entity
@Table(name = "ROLE", schema="db_ca" )
public class Role {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ROLE_ID", nullable=false)
    private Long id;    

    ...
}   

Class Test AccessServiceIT.java

@DataSet("/datasets/UsuarioServiceTest.xml")
@RunWith(UnitilsJUnit4TestClassRunner.class)
@SpringApplicationContext("testContext.xml")
public class AccessServiceIT {

    @SpringBean("AccessServiceImpl")
    private AccessService accessService;

    @Test
    public void accessTest() {
        CredentialsBean credentialsBean = accessService.confirmLogon("test@test.com", "e8d95a51f3af4a3b134bf6bb680a213a");
        assertEquals("test@test.com", credentialsBean.getLogon());
        assertEquals(true, credentialsBean.isAuthenticated());
    }
}

Answer

I found the solution to the problem. I just put the following property in unitils.properties and everything is working perfectly fine now.

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

I found the solution here https://stackoverflow.com/a/39549867/3554659

I hope it helps who is going through the same mistake.



Source: stackoverflow