测试jpa实体类 – 错误需要事务

基于原型我创建了一个java ee应用程序。 有一个包括arquillian测试运行正常。 它只是在@Stateless bean上调用一个方法来持久保存预制实体。

现在我添加了一些实体与一些关系,我为他们写了一个测试。 但在坚持任何实体我得到

Transaction is required to perform this operation (either use a transaction or extended persistence context) 

我想我需要用@Transactional标记testmethod,但似乎不是在类路径中。 在注入的EntityManager上手动调用事务会产生另一个错误。 那么如何正确设置这样的测试和依赖。

编辑 Grzesiek D.建议这里有一些细节。 这是实体(一个链接其他实体):

 @Entity public class Booking implements Serializable { /** * */ private static final long serialVersionUID = 1L; /** * internal id. */ @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", updatable = false, nullable = false) private Long id; /** * Used for optimistic locking. */ @Version @Column(name = "version") private int version; /** * A booking must have a project related. */ @ManyToOne @JoinColumn(name = "project_id") @NotNull private Project project; /** * A booking must have an owner. */ @ManyToOne @JoinColumn(name = "user_id") @NotNull private User owner; /** * A booking always has a start time. */ @Column @NotNull private Timestamp start; /** * A booking always has an end time. */ @Column @NotNull private Timestamp end; /** * * @return true if start is befor end. false otherwise (if equal or after end). */ @AssertTrue(message = "Start must before end.") public final boolean isStartBeforeEnd() { return start.compareTo(end) < 0; } /** * @return the id */ public final Long getId() { return id; } /** * @param id * the id to set */ public final void setId(final Long id) { this.id = id; } /** * @return the version */ public final int getVersion() { return version; } /** * @param version * the version to set */ public final void setVersion(final int version) { this.version = version; } /** * @return the project */ public final Project getProject() { return project; } /** * @param project * the project to set */ public final void setProject(final Project project) { this.project = project; } /** * @return the owner */ public final User getOwner() { return owner; } /** * @param owner * the owner to set */ public final void setOwner(final User owner) { this.owner = owner; } /** * @return the start */ public final Timestamp getStart() { return start; } /** * @param start * the start to set */ public final void setStart(final Timestamp start) { this.start = start; } /** * @return the end */ public final Timestamp getEnd() { return end; } /** * @param end * the end to set */ public final void setEnd(final Timestamp end) { this.end = end; } //hashCode, equals, toString omitted here } 

这是测试:

 @RunWith(Arquillian.class) public class BookingTest { @Deployment public static Archive createDeployment() { return ArquillianContainer.addClasses(Resources.class, Booking.class, Project.class, User.class); } @Inject private EntityManager em; @Test public void createBooking() { Booking booking = new Booking(); booking.setStart(new Timestamp(0)); booking.setEnd(new Timestamp(2)); User user = new User(); user.setName("Klaus"); booking.setOwner(user); Project project = new Project(); project.setName("theOne"); project.setDescription("blub"); booking.setProject(project); em.persist(booking); System.out.println("here"); } } 

这里例外:

 javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context) 

我知道如果我创建一个@Stateless bean并在那里封装持久化,但我想直接测试实体的validation,我需要一个操场来进化数据模型。

为了在Arquillian测试中获得事务支持,您需要引入支持此function的扩展 。 在你的情况下, jta依赖应该完成这项工作。

  org.jboss.arquillian.extension arquillian-transaction-jta test