我有一個(gè)測試檢查我使用 H2 進(jìn)行測試的最后插入的 id@Entity@Datapublic class Guy implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(nullable = false) protected Long id; @Column(nullable = false) protected String name;}這是測試@Testpublic 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()));}我如何才能使僅在測試范圍內(nèi)運(yùn)行的 H2database 返回插入的 id,其 de 值例如為 5000。有可能在測試范圍內(nèi)為實(shí)體 Guy 設(shè)置星值?謝謝!
在 H2 JPA 測試中更新 @Id(使用身份策略)
慕無忌1623718
2022-06-30 17:54:49