代码之家  ›  专栏  ›  技术社区  ›  WesternGun

H2-当Persistable<ID>isNewObject设置为true时,duplicate key未在Spring boot test中引发异常

  •  1
  • WesternGun  · 技术社区  · 6 年前

    我在javaspring引导应用程序中使用H2作为测试DB,但是当我想在尝试插入重复ID/PK时捕获“duplicatekey”异常时,H2不会抛出任何东西。

    邮递员一切都很好,我就是不能通过考试。

    真正的数据库是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;
        }
    

    控制器 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.
    }
    

    请注意 mockBinInfo isNewObject 始终设置为 true ,这意味着在第二次插入中发现重复的PK时应该抛出异常,但它没有发生。这是 Persistable<ID> 接口,当ID是否重复时,它将告诉DB是否持久化。

    • 如果 isNew() 退货 是的 ,将在ID重复时引发异常
    • 否则,将静默更新

    看到了吗 here 有关详细信息(搜索“持久”)。

    编辑:

    我还注意到H2似乎不支持Spring 持久性<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 = []
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   mrkernelpanic    6 年前

    如果你打电话 save() 在现有实体上,Hibernate不会再次将其持久化,只会更新实体。

        2
  •  0
  •   WesternGun    5 年前

    最后我中止了这个测试,并通过不检查异常来绕过它。它仍然是一只虫子。