How to use junit to test Jax rs in wildfly

Asked

Viewed 72 times

2

I created the following test to validate the registration but instead of returning 201 with the created resource is generating this generic error. My DAO is running on its own in a normal way (I created a listing address using it) Would you have any clue what it could be?

@Test
public void testAdicionaEspecie() {

    Especie especie = new Especie();
    especie.setDescricao("Especie Test");
    Entity<Especie> entity = Entity.entity(especie, MediaType.APPLICATION_JSON);
    System.out.println(entity);
    Response response = targetVetweb.path("prontuario/especies")
            .request()
            .post(entity);
    String locNovaEspecie = response.getHeaderString("Location");
    System.out.println(response.getStatus());
    assertTrue(response.getStatus() == 201);
    assertTrue(!locNovaEspecie.isEmpty());

}
@Path("especies")
@POST
@Consumes(value = MediaType.APPLICATION_JSON)
public Response postEspecie(Especie especie) {
    especieService.add(especie);
    return Response.created(URI.create(uriResource.toString() + "/especies/" + especie.getEspecieId())).build();
}
@Override
public void add(Especie especie) {
    animalDAO.salvarEspecie(especie);
}

I am using Wildfly 10. I discovered in debugging that it is failing with Transactionrequiredexception in the test classes. I entered the datasource in the Wildfly configuration and my ORM configuration file contains:

<jta-data-source>java:jboss/datasources/vetwebds</jta-data-source>

In other words, I imagine using jta to manage transactions, how do I make junit recognize this configuration and test my registration? I have tried using the Transactional annotation in the test but failed

1 answer

0

You need your class to be running inside the Application Server.

You can use Arquillian to upload a Container during testing or you can create a separate project to perform HTTP requests using Resteasy for example to test the desired behavior (this would be an integration test via the API exposed by your service).

When setting up a IC pipeline you can, for example, provision the environment, compile your project, deploy, and then run the tests in the second project for integration testing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.