I am trying out DiffBlue on a small test project using the same software stack as the customer: Quarkus + RestEasy + JDBI.
For both the Repository and the Resource DiffBlue is not able to write a proper test.
All tests have a R013 message.
In all test cases DiffBlue tries to create a new instance of the class under test whereas with Quarkus the test should be annotated with @QuarkusTest and the class-under-test should be injected in the test class optionally mocking any dependent the class-under-test depends on.
Example of simple GreetingResource in Quarkus default application (generated by code.quarkus.io):
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello from RESTEasy Reactive";
}
}
The test generated by DiffBlue succeeds but does not do a test of the Rest Api at all:
class GreetingResourceDiffblueTest {
/**
* Method under test: {@link GreetingResource#hello()}
*/
@Test
void testHello() {
// Arrange, Act and Assert
assertEquals("Hello from RESTEasy Reactive", (new GreetingResource()).hello());
}
}
The test should have looked like:
@QuarkusTest
class GreetingResourceTest {
@Test
void testHelloEndpoint() {
given()
.when().get("/hello")
.then()
.statusCode(200)
.body(is("Hello from RESTEasy Reactive"));
}
}
For a simple UserResource which retrieves some users from a database:
@Path("/users")
public class UserResource {
@Inject
Repository repository;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers() {
return repository.findUsers();
}
}
DiffBlue is not able to create a usable test:
class UserResourceDiffblueTest {
/**
* Method under test: {@link UserResource#getUsers()}
*/
@Test
@Disabled("TODO: Complete this test")
void testGetUsers() {
// TODO: Diffblue Cover was only able to create a partial test for this method:
// Reason: No inputs found that don't throw a trivial exception.
// Diffblue Cover tried to run the arrange/act section, but the method under
// test threw
// java.lang.NullPointerException: Cannot invoke "org.acme.Repository.findUsers()" because "this.repository" is null
// at org.acme.UserResource.getUsers(UserResource.java:21)
// See https://diff.blue/R013 to resolve this issue.
// Arrange and Act
(new UserResource()).getUsers();
}
}
And the generated test cases for the Repository
class are similar to these.
Am I doing something wrong or is DiffBlue not usable for the Quarkus framework?
Which frameworks/libraries are supported by DiffBlue?
DiffBlue Cover in IntelliJ: 2024.02.02-2023.3
Quarkus: 3.6.8
Java: 21
JDBI: 3.43.0