我有一个测试检查我使用 H2 进行测试的最后插入的 id
@Entity
@Data
public class Guy implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
protected Long id;
@Column(nullable = false)
protected String name;
}
这是测试
@Test
public void getTest() {
String jsonResponse = "{\"name\":\"andrew\"}";
Response response = given().body(jsonResponse).header("Content-Type",
"application/json").header("Client", 123).post("/thin/guy");
assertEquals(HttpServletResponse.SC_CREATED,response.getStatusCode());
//here the record was created
JSONObject jsonObject = new JSONObject(response.getBody().print());
Response resGet = given().header("Client",123).get("/thin/guy/"+String.valueOf(jsonObject.get("id")));
assertEquals(HttpServletResponse.SC_OK, resGet.getStatusCode());
//this is the response "{\"id\":1,\"name\":\"andrew\"}"
JSONObject getGuy = new JSONObject(resGet.getBody().print());
assertEquals(5000L,Long.valueOf(getGuy.get("id").toString()));
}
我如何才能使仅在测试范围内运行的 H2database 返回插入的 id,其 de 值例如为 5000。有可能在测试范围内为实体 Guy 设置星值?谢谢!
一只斗牛犬
相关分类