Version 2

    The developers of Arquillian are strong proponents of integration testing, where you test your real components as they will be used. For this reason, we recommend managing the transaction from inside your EJBs or Managed Beans.

     

    However, sometimes it can be useful to manage the transaction manually. In this case, it's easy to inject the UserTransaction (an injectable resource available in a CDI environment), giving you full control.

     

    @PersistenceContext EntityManager em;
    
    @Inject UserTransaction utx;
    
    @Test
    public void testAddTask() throws Exception {
       Assert.assertEquals(agendaEm.createQuery("select count(t) from Task t")
          .getSingleResult(), 0l);
     
       utx.begin();
       em.joinTransaction();
       Task task = taskManager.getNewTask();
       Assert.assertFalse(task.isCompleted());
       task.setDescription("Task 1");
       taskManager.addTask();
    
       agendaEm.flush();
       utx.commit();
     
       Assert.assertNotNull(task.getId());
       Assert.assertNotNull(task.getDateCreated());
       Assert.assertSame(task, taskManager.getLastTaskAdded());
       Assert.assertNotSame(task, taskManager.getNewTask());
     
    }