我有一个非常简单的 Quarkus REST 应用程序,可以读取/写入 Redis。在编写端点测试时(使用 Quarkus 模拟指南),但我无法从模拟返回数据。相反,它看起来像是使用非模拟注入并返回非空和空列表。
这是依赖顺序。我想嘲笑
RedisService
:
DataEndpointResource
-> DataService
-> RedisService
DataEndpointResource
:
@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();
}
}
DataService
:
@RequestScoped
public class DataService {
@Inject
private RedisService redisService;
public Collection<DataRecord> getAllValues() throws RedisException {
return this.redisService.getAllValues();
}
}
RedisService
:
@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);
}
}
}
测试:
@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> locks;
@BeforeEach
public void setUp() throws RedisException {
locks = new HashMap<>();
DataRecord r = new DataRecord("1234", "data1");
locks.put(r.getKey(), r);
r = DataRecord r = new DataRecord("asdf", "data2");
locks.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<>(locks.keySet()));
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());
}
}
总是失败:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
|
+-- Data REST Resource
| +-- [XX] Endpoint: GET /data - 0.728 ss
Results:
Failures:
DataEndpointResourceTest.getData:79 expected: <2> but was: <0>
如果您只运行 Quarkus 测试
DataEndpointResourceTest
,模拟应该可以工作。您不能将 Bean 注入 @QuarkusIntegrationTest
中。不会有任何影响。
Quarkus 文档描述了如何编写 Quarkus 集成测试:https://quarkus.io/guides/getting-started-testing#quarkus-integration-test
另请参阅此答案,了解普通 Quarkus 测试和集成测试之间的差异:Quarkus @QuarkusTest 与 @QuarkusIntegrationTest