gpt4 book ai didi

java - H2 - 当 Persistable isNewObject 设置为 true 时,重复键在 Spring 启动测试中不会引发异常

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:08 25 4
gpt4 key购买 nike

我在 Java Spring Boot 应用程序中使用 H2 作为测试数据库,但是当我想在尝试插入重复的 ID/PK 时捕获“重复键”异常时,H2 不会抛出任何内容。

有了 Postman,一切都很好,只是我无法通过测试。

真正的数据库是PostgreSQL,当我与Postman集成测试时,它确实抛出异常。但在单元测试时我认为没有必要加载真实的DB所以我选择了H2。

H2 配置:

spring.datasource.url=jdbc:h2:mem:tesdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;mode=MySQL
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1

spring.jpa.datasource.show-sql=true
spring.h2.console.enabled=true # if you need console

Bean 定义:

@Entity
@Data
@JsonComponent
@Table(name="bin_info")
public class BinInfo implements Serializable, Persistable<String>{ //with Persistable we can check ID duplicate
@Id
@Size(min=6, max=8)
@Column(name="bin")
@JsonProperty("bin")
private String bin;

...

/**
* Property for identifying whether the object is new or old,
* will insert(new) or update(old)
* If is new and id/bin is duplicate, org.hibernate.exception.ConstraintViolationException will be thrown.
* If is old and id/bin is duplicate, just updates. Hibernate save() will upsert and no complain.
*/
@Transient
private boolean isNewObject;

@Override
public String getId() {
return this.bin;
}

@Override
public boolean isNew() {
return isNewObject;
}
@Override
public String getId() {
return this.bin;
}

@Override
public boolean isNew() {
return isNewObject;
}

Controller insert方法:

@RequestMapping(value="/insert", method=RequestMethod.POST)
public ResponseEntity<Object> insertBIN(@Valid @RequestBody BinInfo bin_info, HttpServletResponse response) throws JsonProcessingException {
Map<String, Object> errors = new HashMap<String, Object>();
try {
OffsetDateTime now = OffsetDateTime.now();
bin_info.setCreatedAt(now);
bin_info.setUpdatedAt(now);
bin_info.setNewObject(true); //if set to true, bin duplicate -> exception and return 200; then we avoid "select to check duplicate first"
BinInfo saved = repository.save(bin_info);
// if is new, created(201); if not, updated(status OK, 200)
return ResponseEntity.status(HttpStatus.CREATED)
.contentType(MediaType.APPLICATION_JSON_UTF8).body(saved);
} catch (DataIntegrityViolationException e0) {
log.warn("Update BIN due to duplicate", e0); // exception details in log
//if duplicate, change newObject to false and save again. And return.
bin_info.setNewObject(false);
BinInfo saved = repository.save(bin_info);
return ResponseEntity.ok() // <<<<<< here I define "save duplicate=update=200, OK"
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(saved);
} catch (Exception e) {
log.error("Cannot save BinInfo. ", e); // exception details in log
errors.put("error", "Cannot save BIN"); // don't expose to user
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(Utilities.jsonBuilder(errors));
}

测试:

@Test
public void testBinInfoControllerInsertBIN() throws Exception {
when(this.repository.save(any(BinInfo.class))).thenReturn(mockBinInfo);
String content_double_quotes = "{\"bin\":\"123456\", "
+ "\"json_full\":\"" + this.json_full + "\", "
+ "\"brand\":\"" + this.brand + "\", "
+ "\"type\":\"" + this.type + "\", "
+ "\"country\":\"" + this.country + "\", "
+ "\"issuer\":\"" + this.issuer + "\", "
+ "\"newObject\":true, "
+ "\"new\":true, "
+ "\"createdAt\":\"18/08/2018 02:00:00 +0200\", "
+ "\"updatedAt\":\"18/08/2018 02:00:00 +0200\"}";
log.info("JSON input: " + content_double_quotes);

//save the entity for the first time(newObject==true, new=true) and should return 201
this.mockMvc.perform(post("/insert")
.content(content_double_quotes) //json cannot have single quote; must be double
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
)
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(status().isCreated())
.andDo(print())
.andExpect(jsonPath("$.bin", is(this.bin)))
.andExpect(jsonPath("$.json_full", is(this.json_full)))
.andExpect(jsonPath("$.brand", is(this.brand)))
.andExpect(jsonPath("$.type", is(this.type)))
.andExpect(jsonPath("$.country", is(this.country)))
.andExpect(jsonPath("$.issuer", is(this.issuer)))
.andExpect(jsonPath("$.createdAt", is("18/08/2018 02:00:00 +0200")))
.andExpect(jsonPath("$.updatedAt", is("18/08/2018 02:00:00 +0200")));

//save the same entity, new == true, and should return 200
this.mockMvc.perform(post("/insert")
.content(content_double_quotes) //json cannot have single quote; must be double
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andDo(print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(status().isOk()); //<<<<< here I always get 201, not 200. With Postman I get 200 instead.
}

请注意, mockBinInfoisNewObject 始终设置为 true ,这意味着在第二次插入中找到重复的 PK 时它应该抛出异常,但它没有发生。这是 Persistable<ID> 接口(interface)所需要的,它会告诉 DB 当 ID 重复或不重复时是否保留。

  • 如果 isNew() 返回 true ,当 ID 重复时会抛出异常
  • 否则,将静默更新

请参阅 here 了解更多信息(搜索“Persistable”)。

编辑:

我还注意到H2似乎不支持Spring Persistable<ID>并且总是在保存的实体中返回new=false

日志详细信息:

MockHttpServletRequest:
HTTP Method = POST
Request URI = /insert
Parameters = {}
Headers = {Content-Type=[application/json;charset=UTF-8], Accept=[application/json;charset=UTF-8]}
Body = {"bin":"123456", "json_full":"{'brand':'visa', 'type':'credit', 'country':'USA', 'issuer':'BigBank'}", "brand":"visa", "type":"credit", "country":"USA", "issuer":"BigBank", "newObject":"true", "new":"true", "createdAt":"18/08/2018 02:00:00 +0200", "updatedAt":"18/08/2018 02:00:00 +0200"}
Session Attrs = {}

Handler:
Type = com.xxxxx.binlookup.controller.BinInfoController
Method = public org.springframework.http.ResponseEntity<java.lang.Object> com.xxxxx.binlookup.controller.BinInfoController.insertBIN(com.xxxxx.binlookup.model.BinInfo,javax.servlet.http.HttpServletResponse) throws com.fasterxml.jackson.core.JsonProcessingException

Async:
Async started = false
Async result = null

Resolved Exception:
Type = null

ModelAndView:
View name = null
View = null
Model = null

FlashMap:
Attributes = null

MockHttpServletResponse:
Status = 201
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {"id":"123456","newObject":false,"new":false,"bin":"123456","json_full":"{'brand':'visa', 'type':'credit', 'country':'USA', 'issuer':'BigBank'}","brand":"visa","type":"credit","country":"USA","issuer":"BigBank","createdAt":"18/08/2018 02:00:00 +0200","updatedAt":"18/08/2018 02:00:00 +0200"}
Forwarded URL = null
Redirected URL = null
Cookies = []

最佳答案

如果您对现有实体调用 save(),Hibernate 不会再次保留它,而只会更新该实体。

关于java - H2 - 当 Persistable<ID> isNewObject 设置为 true 时,重复键在 Spring 启动测试中不会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51818627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com