DB Unit TypeCastException

时间:2014-02-06 13:58:50

标签: sql-server spring annotations dbunit hibernate-onetomany

我正在尝试为我的简单应用程序设置单元测试,但在设置测试数据时,我总是遇到TypeCast错误。

这里需要的信息:

1)数据库架构:

USE [EDIKANBAN]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

IF EXISTS
(
    SELECT  *
    FROM    INFORMATION_SCHEMA.TABLES
    WHERE   TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'CONTACTASSIGNEDROLES'
)
BEGIN
    ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] DROP CONSTRAINT FK_CONTACTDETAILS_CONTACTASSIGNEDROLES
    ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] DROP CONSTRAINT FK_CONTACTASSIGNEDROLES_CONTACTROLE
    --ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] DROP CONSTRAINT FK_CONTACTASSIGNEDROLES_CONTACTRESPONSIBILITY
    DROP TABLE [dbo].[CONTACTASSIGNEDROLES]
END
GO

IF EXISTS
(
    SELECT  *
    FROM    INFORMATION_SCHEMA.TABLES
    WHERE   TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'CONTACTDETAILS'
)
BEGIN
    DROP TABLE [dbo].[CONTACTDETAILS]
END
GO

IF EXISTS
(
    SELECT  *
    FROM    INFORMATION_SCHEMA.TABLES
    WHERE   TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'CONTACTROLE'
)
BEGIN
    DROP TABLE [dbo].[CONTACTROLE]
END
GO

IF EXISTS
(
    SELECT  *
    FROM    INFORMATION_SCHEMA.TABLES
    WHERE   TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'CONTACTRESPONSIBILITY'
)
BEGIN
    DROP TABLE [dbo].[CONTACTRESPONSIBILITY]
END
GO

IF EXISTS
(
    SELECT  *
    FROM    INFORMATION_SCHEMA.TABLES
    WHERE   TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'CONTACTROLE'
)
BEGIN
    DROP TABLE [dbo].[CONTACTROLE]
END
GO

/************************************************************************************/
/****** Object: Table [dbo].[CONTACTROLE]  Script Date: 06/02/2014 10:04:11 ******/
/************************************************************************************/
CREATE TABLE [dbo].[CONTACTROLE](
    [ID] [bigint] NOT NULL,
    [ROLE] [varchar](50) NOT NULL,
 CONSTRAINT [PK_CONTACTROLE] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

/**********************************************************************************************/
/****** Object: Table [dbo].[CONTACTRESPONSIBILITY]  Script Date: 06/02/2014 10:03:57 ******/
/**********************************************************************************************/
CREATE TABLE [dbo].[CONTACTRESPONSIBILITY](
    [ID] [bigint] NOT NULL,
    [RESPONSIBILITY] [varchar](80) NOT NULL,
 CONSTRAINT [PK_CONTACTRESPONSIBILITY] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

/***************************************************************************************/
/****** Object: Table [dbo].[CONTACTDETAILS]  Script Date: 04/02/2014 11:05:27 ******/
/***************************************************************************************/
CREATE TABLE [dbo].[CONTACTDETAILS](
    [ID] [bigint] NOT NULL,
    [SUPPLIER_CODE] [varchar](5) NOT NULL,
    [NAME] [varchar](100) NOT NULL,
    [TELEPHONE] [varchar](80) NULL,
    [MOBILE] [varchar](80) NULL,
    [FAX] [varchar](80) NULL,
    [EMAIL] [varchar](100) NULL,
    [CREATED_DATE] [datetime] NOT NULL,
    [CREATED_USER] [varchar](50) NOT NULL,
    [MODIFIED_DATE] [datetime] NOT NULL,
    [MODIFIED_USER] [varchar](50) NOT NULL,
 CONSTRAINT [PK_CONTACTDETAILS] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

/*********************************************************************************************/
/****** Object: Table [dbo].[CONTACTASSIGNEDROLES]  Script Date: 06/02/2014 10:03:34 ******/
/*********************************************************************************************/
CREATE TABLE [dbo].[CONTACTASSIGNEDROLES](
    [ID] [bigint] NOT NULL,
    [DETAILS_ID] [bigint] NOT NULL,
    [ROLE_ID] [bigint] NOT NULL,
    [RESPONSIBILITY_ID] [bigint] NOT NULL,
    [DOCK_CODE] [char](5) NULL,
    [COMMENT] [varchar](100) NULL,
    [CREATED_DATE] [datetime] NOT NULL,
    [CREATED_USER] [varchar](50) NOT NULL,
    [MODIFIED_DATE] [datetime] NOT NULL,
    [MODIFIED_USER] [varchar](50) NOT NULL,
 CONSTRAINT [PK_CONTACTASSIGNEDROLES] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] WITH CHECK ADD CONSTRAINT [FK_CONTACTDETAILS_CONTACTASSIGNEDROLES] FOREIGN KEY([DETAILS_ID])
REFERENCES [dbo].[CONTACTDETAILS] ([ID])
GO
ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] CHECK CONSTRAINT [FK_CONTACTDETAILS_CONTACTASSIGNEDROLES]
GO

ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] WITH CHECK ADD CONSTRAINT [FK_CONTACTASSIGNEDROLES_CONTACTROLE] FOREIGN KEY([ROLE_ID])
REFERENCES [dbo].[CONTACTROLE] ([ID])
GO
ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] CHECK CONSTRAINT [FK_CONTACTASSIGNEDROLES_CONTACTROLE]
GO

ALTER TABLE [dbo].[CONTACTASSIGNEDROLES]  WITH CHECK ADD  CONSTRAINT [FK_CONTACTASSIGNEDROLES_CONTACTRESPONSIBILITY] FOREIGN KEY([RESPONSIBILITY_ID])
REFERENCES [dbo].[CONTACTRESPONSIBILITY] ([ID])
GO

ALTER TABLE [dbo].[CONTACTASSIGNEDROLES] CHECK CONSTRAINT [FK_CONTACTASSIGNEDROLES_CONTACTRESPONSIBILITY]
GO

SET ANSI_PADDING OFF
GO

2)与表对应的Java对象:

@Entity
@Table(name = "CONTACTROLE")
public class ContactRole {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Column(name = "ROLE", length = 80)
    @Size(min = 0, max = 80)
    private String role;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "contactRole")
    private Set<ContactAssignedRole> contactAssignedRoles;  

}


@Entity
@Table(name = "CONTACTRESPONSIBILITY")
public class ContactResponsibility {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Column(name = "RESPONSIBILITY", length = 80)
    @Size(min = 0, max = 80)
    private String responsibility;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "contactResponsibility")
    private Set<ContactAssignedRole> contactAssignedRoles;


@Entity
@Table(name = "CONTACTDETAILS")
@Inheritance(strategy = InheritanceType.JOINED)
public class ContactDetails extends ServiceModel {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Column(name = "SUPPLIER_CODE", length = 5)
    @Size(min = 5, max = 5)
    private String supplierCode;

    @Column(name = "NAME", length = 100)
    @Size(min = 0, max = 100)
    private String name;

    @Column(name = "TELEPHONE", nullable = true, length = 80)
    @Size(max = 80)
    private String telephone;

    @Column(name = "MOBILE", nullable = true, length = 80)
    @Size(max = 80)
    private String mobile;

    @Column(name = "FAX", nullable = true, length = 80)
    @Size(max = 80)
    private String fax;

    @Column(name = "EMAIL", nullable = true, length = 100)
    @Size(max = 100)
    private String email;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "contactDetails")
    private Set<ContactAssignedRole> contactAssignedRoles;


@Entity
@Table(name = "CONTACTASSIGNEDROLES")
public class ContactAssignedRole extends ServiceModel {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DETAILS_ID")
    @NotNull
    private ContactDetails contactDetails;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ROLE_ID")
    private ContactRole contactRole;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "RESPONSIBILITY_ID")
    private ContactResponsibility contactResponsibility;

    @Column(name = "DOCK_CODE", length = 5)
    @Size(min = 0, max = 5)
    private String dockCode;

    @Column(name = "COMMENT", length = 100)
    @Size(min = 0, max = 100)
    private String comment;

3)数据集xml文件

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<CONTACTRESPONSIBILITY
        ID="1"
        RESPONSIBILITY="DEFAULT"
    />
    <CONTACTRESPONSIBILITY
        ID="2"
        RESPONSIBILITY="TMMF"
    />
    <CONTACTRESPONSIBILITY
        ID="3"
        RESPONSIBILITY="TMUKB"
    />
    <CONTACTRESPONSIBILITY
        ID="4"
        RESPONSIBILITY="TMUKD"
    />
    <CONTACTRESPONSIBILITY
        ID="5"
        RESPONSIBILITY="TMMP"
    />
    <CONTACTRESPONSIBILITY
        ID="6"
        RESPONSIBILITY="TMIP"
    />
    <CONTACTRESPONSIBILITY
        ID="7"
        RESPONSIBILITY="TMMT"
    />
    <CONTACTRESPONSIBILITY
        ID="8"
        RESPONSIBILITY="SERVICE"
    />
    <CONTACTRESPONSIBILITY
        ID="9"
        RESPONSIBILITY="EXPORT"
    />
    <CONTACTROLE
        ID="1"
        ROLE="DAILY CONTACT"
    />
    <CONTACTROLE
        ID="2"
        ROLE="BACK-UP CONTACT"
    />
    <CONTACTROLE
        ID="3"
        ROLE="PRENOTIFICATION CONTACT"
    />
    <CONTACTROLE
        ID="4"
        ROLE="LOGISTICS CONTACT"
    />
    <CONTACTROLE
        ID="5"
        ROLE="AF CONTACT"
    />
    <CONTACTROLE
        ID="6"
        ROLE="EMERGENCY CONTACT 1"
    />
    <CONTACTROLE
        ID="7"
        ROLE="EMERGENCY CONTACT 2"
    />
    <CONTACTROLE
        ID="8"
        ROLE="PLANT MANAGER"
    />
    <CONTACTROLE
        ID="9"
        ROLE="MANAGING DIRECTOR"
    />
    <CONTACTROLE
        ID="10"
        ROLE="SCM ORDERS EMAIL RECEIVER"
    />
    <CONTACTDETAILS 
        ID="1" 
        SUPPLIER_CODE="SUP11" 
        NAME="NAME-11111" 
        TELEPHONE="+32 2 111.11.11" 
        MOBILE="+32 499 111.111" 
        EMAIL="TEST11111@TOYOTA-EUROPE" 
        CREATED_DATE="2014-02-05 01:01:01" 
        CREATED_USER="testUser1@TARS"
        MODIFIED_DATE="2014-02-05 01:01:01" 
        MODIFIED_USER="testUser1@TARS"
    />
    <CONTACTDETAILS 
        ID="2" 
        SUPPLIER_CODE="SUP22" 
        NAME="NAME-22222" 
        TELEPHONE="+32 2 222.22.22" 
        MOBILE="+32 499 222.222" 
        EMAIL="TEST22222@TOYOTA-EUROPE" 
        CREATED_DATE="2014-02-05 02:02:02" 
        CREATED_USER="testUser2@TARS"
        MODIFIED_DATE="2014-02-05 02:02:02" 
        MODIFIED_USER="testUser2@TARS"
    />
    <CONTACTDETAILS
        ID="3" 
        SUPPLIER_CODE="SUP33" 
        NAME="NAME-33333" 
        TELEPHONE="+32 2 333.33.33" 
        MOBILE="+32 499 333.333" 
        EMAIL="TEST33333@TOYOTA-EUROPE"
        CREATED_DATE="2014-02-05 03:03:03" 
        CREATED_USER="testUser3@TARS"
        MODIFIED_DATE="2014-02-05 03:03:03" 
        MODIFIED_USER="testUser3@TARS"
     />
    <CONTACTDETAILS 
        ID="4" 
        SUPPLIER_CODE="SUP44" 
        NAME="NAME-44444" 
        TELEPHONE="+32 2 444.44.44" 
        MOBILE="+32 499 444.444" 
        EMAIL="TEST44444@TOYOTA-EUROPE"
        CREATED_DATE="2014-02-05 04:04:04" 
        CREATED_USER="testUser4@TARS"
        MODIFIED_DATE="2014-02-05 04:04:04" 
        MODIFIED_USER="testUser4@TARS" 
    />
    <CONTACTASSIGNEDROLES
        ID="1"
        DETAILS_ID="1"
        ROLE_ID="1"
        RESPONSIBILITY_ID="1"
        DOCK_CODE="11"
        COMMENT="This is comment number one."
        CREATED_DATE="2014-02-01 01:01:01"
        CREATED_USER="testUser1@TARS"
        MODIFIED_DATE="2014-02-05 01:01:01"
        MODIFIED_USER="testUser1@TARS"
    />
    <CONTACTASSIGNEDROLES
        ID="2"
        DETAILS_ID="2"
        ROLE_ID="2"
        RESPONSIBILITY_ID="2"
        DOCK_CODE="22"
        COMMENT="This is comment number two."
        CREATED_DATE="2014-02-02 02:02:02"
        CREATED_USER="testUser2@TARS"
        MODIFIED_DATE="2014-02-02 02:02:02"
        MODIFIED_USER="testUser2@TARS"
    />  
    <CONTACTASSIGNEDROLES
        ID="3"
        DETAILS_ID="3"
        ROLE_ID="3"
        RESPONSIBILITY_ID="3"
        DOCK_CODE="33"
        COMMENT="This is comment number three."
        CREATED_DATE="2014-02-03 03:03:03"
        CREATED_USER="testUser3@TARS"
        MODIFIED_DATE="2014-02-03 03:03:03"
        MODIFIED_USER="testUser3@TARS"
    />  
    <CONTACTASSIGNEDROLES
        ID="4"
        DETAILS_ID="4"
        ROLE_ID="4"
        RESPONSIBILITY_ID="4"
        DOCK_CODE="44"
        COMMENT="This is comment number four."
        CREATED_DATE="2014-02-04 04:04:04"
        CREATED_USER="testUser4@TARS"
        MODIFIED_DATE="2014-02-04 04:04:04"
        MODIFIED_USER="testUser4@TARS"
    />
    <CONTACTASSIGNEDROLES
        ID="5"
        DETAILS_ID=""
        ROLE_ID="5"
        RESPONSIBILITY_ID="5"
        DOCK_CODE="55"
        COMMENT="This is comment number five."
        CREATED_DATE="2014-02-05 05:05:05"
        CREATED_USER="testUser5@TARS"
        MODIFIED_DATE="2014-02-05 05:05:05"
        MODIFIED_USER="testUser5@TARS"
    />
</dataset>

4)我正在尝试执行的测试:

public class ContactAssignedRoleServiceTest extends AbstractServiceTest {
    @SpringBeanByName
    private ContactAssignedRoleService contactAssignedRoleService;

    @Test
    @DataSet(value = "ContactAssignedRoleServiceTest.xml")
    public void testFindContactAssignedRoles() {

        List<ContactAssignedRole> dbContactAssignedRoles = contactAssignedRoleService.find();

        Assert.assertNotNull("Contact Details is null", dbContactAssignedRoles);
        Assert.assertEquals("Wrong number of records returned", 5, dbContactAssignedRoles.size());
        ContactAssignedRole dbContactAssignedRole = dbContactAssignedRoles.get(0);
        Assert.assertNotNull("Contact Assigned Role is null", dbContactAssignedRole);
        Assert.assertEquals("Wrong ID", 1, dbContactAssignedRole.getId().longValue());
        Assert.assertEquals("Wrong Contact Id", "1", dbContactAssignedRole.getContactDetails().getId());
        Assert.assertEquals("Wrong Role", "DAILY CONTACT", dbContactAssignedRole.getContactRole().getRole());
        Assert.assertEquals("Wrong Responsibility", "DEFAULT", dbContactAssignedRole.getContactResponsibility().getResponsibility());
        Assert.assertEquals("Wrong Dock", "11", dbContactAssignedRole.getDockCode());
        Assert.assertEquals("Wrong Comment", null, dbContactAssignedRole.getComment());
    }
}

5)我得到的错误:

org.unitils.core.UnitilsException: Error inserting test data from DbUnit dataset for method public void com.toyota.tme.edikanban.service.ContactAssignedRoleServiceTest.testFindContactAssignedRoles()
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:156)
    at org.unitils.dbunit.DbUnitModule$DbUnitListener.beforeTestSetUp(DbUnitModule.java:557)
    at org.unitils.core.Unitils$UnitilsTestListener.beforeTestSetUp(Unitils.java:273)
    at org.unitils.UnitilsJUnit4TestClassRunner$TestListenerInvokingMethodRoadie.runBeforesThenTestThenAfters(UnitilsJUnit4TestClassRunner.java:151)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.unitils.UnitilsJUnit4TestClassRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:95)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
    at org.unitils.UnitilsJUnit4TestClassRunner.access$000(UnitilsJUnit4TestClassRunner.java:44)
    at org.unitils.UnitilsJUnit4TestClassRunner$1.run(UnitilsJUnit4TestClassRunner.java:62)
    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:68)
    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)
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:230)
    at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:153)
    ... 18 more
Caused by: org.dbunit.dataset.datatype.TypeCastException: Error casting value for table 'CONTACTASSIGNEDROLES' and column 'DETAILS_ID'
    at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:208)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:78)
    at org.unitils.dbunit.datasetloadstrategy.impl.CleanInsertLoadStrategy.doExecute(CleanInsertLoadStrategy.java:45)
    at org.unitils.dbunit.datasetloadstrategy.impl.BaseDataSetLoadStrategy.execute(BaseDataSetLoadStrategy.java:44)
    ... 20 more

对我做错了什么的想法?

非常感谢!!!

0 个答案:

没有答案