Search code examples
springjpah2dbunitspring-test-dbunit

DBUnit NoSuchColumnException in parent class


So I'm getting this error:

org.dbunit.dataset.NoSuchColumnException: TOYOTA.CAR_MAKE -  (Non-uppercase input column: CRTE_DT_TM) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.

when trying to populate a H2 DB using DbUnit in the context of a Spring JUnit test. The DDL for the H2 DB is created from JPA-annotated classes. However, the column does exist, it is in an abstract class that is extended by the core class.

Let me explain. The core JPA class looks like this:

@Entity
@Table(name = "TOYOTA")
public class Toyota extends Car {

    @Id
    @Column(name = "TOYOTA_REF")
    private String toyotaRef;

    @Column(name = "TOYOTA_DEALER")
    private String toyotaDealer;
// etc.

The parent class looks like this:

public abstract class Car {

    @Column(name = "CAR_MAKE")
    private String carMake;

    @Column(name = "CAR_MODEL")
    private String carModel;

I'm initiating tests like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/test-spring-config.xml") // Load Spring Test context
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class}) // Used to load test data onto DB})

@DatabaseSetup(value = {"classpath:/integrationTestData/Toyota.xml"})
@Test
@Transactional

public void testSomething() {

and my Toyota.xml data file looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <TOYOTA TOYOTA_REF="TOY123"
           TOYOTA_DEALER="Big Dealer"
           CAR_MAKE="Toyota"
           CAR_MODEL="Corolla"
           />
</dataset>

Any thoughts?


Solution

  • Fixed this myself. I had to add this JPA annotation to the parent class:

    import javax.persistence.MappedSuperclass;
    
    @MappedSuperclass
    public abstract class Car {