[英]How to inject mock for only one test case with Quarkus/RestAssured
[英]Mock a JSON request parameter using Quarkus and RestAssured
使用 Quarkus,我有以下 API。
@RequestScoped
@Path("/api/v1")
public class MyApi {
@POST
@Consumes(APPLICATION_JSON)
public Response create(Entity entityToCreate) {
if (entityToCreate.isValid()) {
// create the entity in my app...
return Response.ok().build();
} else {
return Response.status(BAD_REQUEST).build();
}
}
}
我的Entity
class 看起來像這樣
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Entity {
private final String property1;
private final String property2;
// ...
@JsonCreator
public Entity(@JsonProperty("property1") String property1,
@JsonProperty("property2") String property2,
// ...
){
this.property1 = property1;
this.property2 = property2;
// ...
}
// getters for every property...
public boolean isValid() {
// check if the entity is valid, then return true or false
}
}
在我的單元測試中,我想要實現的目標如下:
import static io.restassured.RestAssured.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@QuarkusTest
public class MyApiTest {
private final Entity entityMock = mock(Entity.class);
@Test
public void shouldCreateEntityWhenValid(){
when(entityMock.isValid()).thenReturn(true);
given()
.contentType(ContentType.JSON)
.body(entityMock)
.when()
.post("/api/v1")
.then()
.statusCode(200);
}
@Test
public void shouldNotCreateEntityWhenInvalid(){
when(entityMock.isValid()).thenReturn(false);
given()
.contentType(ContentType.JSON)
.body(entityMock)
.when()
.post("/api/v1")
.then()
.statusCode(400);
}
}
目前,我遇到以下錯誤,因為 Jackson 試圖反序列化模擬。
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.invocation.mockref.MockWeakReference and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: blah blah blah... )
只需創建實體的真實實例並讓驗證發生。
Rest Assured 將在測試中發布實際的 HTTP 請求,因此它將您的模擬序列化為 JSON。 當實現處理 HTTP 請求時,請求主體隨后被反序列化為實體。 該實體是在內部實例化的,因此它與在測試中創建的實例不同。
我最終混合了幾種測試技術,以驗證 JSON 映射和方法行為。
import static io.restassured.RestAssured.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@QuarkusTest
public class MyApiTest {
private final Entity entityMock = mock(Entity.class);
private final Entity validEntity = // [...] instantiate a valid entity here
private MyApi api;
@BeforeEach
void beforeEach() {
myApi = new MyApi();
}
// this test validates both the JSON parsing and the valid entity case
@Test
public void shouldCreateEntityWhenValid(){
given()
.contentType(ContentType.JSON)
.body(validEntity)
.when()
.post("/api/v1")
.then()
.statusCode(200);
}
// this test validates the invalid entity case
@Test
public void shouldNotCreateEntityWhenInvalid(){
when(entityMock.isValid()).thenReturn(false);
Response response = myApi.create(entityMock);
assertThat(response.getStatus()).isEqualTo(400);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.