如何在Arquillian测试中使用@UsingDataSet

时间:2016-02-24 11:23:14

标签: unit-testing jboss-arquillian

我在项目中使用@UsingDataSet时遇到问题。 手册中的示例https://docs.jboss.org/author/display/ARQ/Persistence 没有帮助。 我有简单的实体:

@Entity(name = "game")
@Table(name = "game_entity", schema = "graph")
@GenericGenerator(name="uuid2", strategy = "uuid2")
public class Game implements Serializable {

private static final long serialVersionUID = -4832711740307931146L;
private UUID id;
private String name;

@Id
@GeneratedValue(generator="uuid2")
public UUID getId() {
    return id;
}

public void setId(UUID id) {
    this.id = id;
}

@NotNull
@Column(name = "name", length = 256, nullable = false)
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public boolean equals(Object value) {
    if (!(value instanceof Game)) {
        return false;
    }
    if (value == this) {
        return true;
    }
    Game obj = (Game) value;
    if (!obj.getId().equals(getId())) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    int result = 29;
    result += 29 * (getId() != null ? getId().hashCode() : 0);
    result += 29*(getName() != null ? getName().hashCode() : 0);
    return result;
}
}

和.yml文件

game_entity:
    - id         : d5c58afd-7c99-41bb-ab0a-6357bfddfe14
      name       : Call of Duty

我的测试:

public class MyTest extends Arquillian {

    @PersistenceContext(unitName = "spo")
    private EntityManager em;

    @Test
    @UsingDataSet("datasets/gameDataSet.yml")
    public void testAttribute() {
        System.out.println("Hello world !");
    }

    @Deployment
    public static WebArchive createArchive() {
        WebArchive war = ShrinkWrap
                .create(WebArchive.class, "myTest.war")
                .addAsLibraries(
                        // blah-blah-blah
                )
                .addAsResource("my-persistence.xml", "META-INF/persistence.xml")
                //.addAsResource("datasets/gameDataSet.yml", "datasets/gameDataSet.yml") // do I need to add this to archive ??
                .addAsWebInfResource("META-INF/beans.xml", "beans.xml")
                .setWebXML("web.xml")
                ;
        return war;
    }
}

当我进行这项测试时,我得到2"奇怪"产生的异常。 有时:

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

但是数据库中有2列! " ID"和"名称"

有时,更奇怪的例外:

Caused by: org.postgresql.util.PSQLException: ERROR: relation "info_resource_attributes" does not exist

这是另外两个实体的交叉表,它存在于archive和myPersistence.xml中,但我不在此测试中使用它们。 我使用Wildfly 10和Postgres 9.5。没有@UsingDataSet一切都很好。 但我不希望programmaticaly硬编码数据用于这样的测试:

Entity newEntityForTest = new Entity();
newEntityForTest.setA(...);
newEntityForTest.setB(...);
newEntityForTest.setC(...);
em.persist(newEntityForTest);
// testing ...
em.remove(newEntityForTest);

0 个答案:

没有答案