-
Disclaimer: This is a cross post with my Stackoverflow question, but also posting here because I would like to resolve this I have a pretty simple Quarkus REST application that reads/writes to Redis. When writing my endpoint tests (using the Quarkus mocking guide), I can't get the data to return from the mock. Instead, it looks like it's using a non-mock injection and returning a non-null and empty list. Here is dependency order. I want to mock
@Path("/data")
@RequestScoped
public class DataEndpointResource {
@Inject
DataService service;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllValues() {
Collection<DataRecord> values;
try {
values = service.getAllValues();
} catch (RedisException ex) {
return Response.serverError().build();
}
return Response.ok(values).build();
}
}
@RequestScoped
public class DataService {
@Inject
private RedisService redisService;
public Collection<DataRecord> getAllValues() throws RedisException {
return this.redisService.getAllValues();
}
}
@ApplicationScoped
public class RedisService {
ReactiveRedisDataSource ds;
KeyCommands<String> keyCommands;
ValueCommands<String, ObjectLockRecord> valueCommands;
public RedisService(RedisDataSource ds) {
keyCommands = ds.key();
valueCommands = ds.value(ObjectLockRecord.class);
}
public Collection<DataRecord> getAllValues() throws RedisException {
try {
// do redis stuff and return values
} catch (Exception ex) {
log("Error obtaining values", ex);
throw new RedisException(ex);
}
}
} Test:
@QuarkusIntegrationTest
public class DataEndpointResourceIT extends DataEndpointResourceTest {
} import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import static io.restassured.RestAssured.given;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import static org.mockito.Mockito.when;
@QuarkusTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@DisplayName("Data REST Resource")
public class DataEndpointResourceTest {
@InjectMock
RedisService redisServiceMock;
Map<String, DataRecord> testData;
@BeforeEach
public void setUp() throws RedisException {
testData = new HashMap<>();
DataRecord r = new DataRecord("1234", "data1");
testData.put(r.getKey(), r);
r = DataRecord r = new DataRecord("asdf", "data2");
testData.put(r.getKey(), r);
}
@Test
@Order(1)
@DisplayName("Endpoint: GET /data")
public void getData() throws RedisException // without "throws" it complains {
when(redisServiceMock.getAllValues()).thenReturn(new ArrayList<>(testData.values()));
List<DataRecord> list = given()
.accept(ContentType.JSON)
.when()
.get("/api/v1/data")
.then()
.statusCode(200)
.extract().body().as(new TypeRef<>() {
});
Assertions.assertNotNull(list);
Assertions.assertEquals(2, list.size());
}
} It always fails:
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<dependencies>
<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>quarkus-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<groups>io.quarkus.test.junit.QuarkusTest</groups>
</configuration>
</execution>
</executions>
<configuration>
<reportFormat>plain</reportFormat>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<disable>false</disable>
<version>3.0</version>
<usePhrasedFileName>false</usePhrasedFileName>
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
</statelessTestsetReporter>
<consoleOutputReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5ConsoleOutputReporter">
<disable>true</disable>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<disable>false</disable>
<usePhrasedFileName>false</usePhrasedFileName>
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
<usePhrasedClassNameInTestCaseSummary>true</usePhrasedClassNameInTestCaseSummary>
</statelessTestsetInfoReporter>
</configuration>
</plugin> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
/cc @geoand (testing) |
Beta Was this translation helpful? Give feedback.
I figured it out. It was a coding error on my part in the test (even seasoned devs like me have fat finger syndrome!).
I was doing the following:
but I should have done:
For others who are getting started with Quarkus and Redis, here is a proof of concept app I was testing with:
https://github.com/johnmanko/quarkus-redis-service-example
I hope someone can find it useful.