mercredi 24 février 2016

How to use @UsingDataSet in Arquillian test

I have trouble using @UsingDataSet in my project. Example in manual http://ift.tt/1oAAcea doesn`t helps. I have simple entity:

@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;
}
}

And .yml file

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

My test:

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;
    }
}

When I run this test I getting 2 "strange" exeptions. Sometimes:

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.

But there are 2 columns in database! "id" and "name"

Sometimes, even more strange exception:

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

This is cross-table for another 2 entities, it exist in archive and myPersistence.xml but I dont use them in this test. I use Wildfly 10 and Postgres 9.5. Without @UsingDataSet everything fine. But I dont want programmaticaly hardcode data for tests like that:

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

Aucun commentaire:

Enregistrer un commentaire