ray0625
V2EX  ›  Java

SpringBoot 单元测试问题

  •  
  •   ray0625 · May 31, 2018 · 2837 views
    This topic created in 2897 days ago, the information mentioned may be changed or developed.

    SpringBoot 里的单元测试,先贴个代码:

    @Test
    @Transactional
    @Rollback(true)
    public void addChildResourceTest() {
        JSONObject requestBody = new JSONObject();
        requestBody.accumulate("userId", "admin");
        requestBody.accumulate("parentId", "901BCDCA-B272-4F52-B1F0-E47603771611");
    
        JSONArray childResourceList = new JSONArray();
        JSONObject child = new JSONObject();
        child.accumulate("id", "505BCDCA-B272-5F52-B1F0-E57603771611");
        child.accumulate("name", "测试子资源");
        child.accumulate("index", 0);
        child.accumulate("oriResourceId", "6728353b-f448-11e7-8bf7-00155dbc9610");
        JSONObject metadata = new JSONObject();
        metadata.accumulate("时间", "2018-01-05 11:50:33");
        child.accumulate("metadata", metadata);
        child.accumulate("description", "描述");
        childResourceList.add(child);
        requestBody.accumulate("childReourceList", childResourceList);
    
        try {
            MvcResult updateResult = mockMvc.perform(MockMvcRequestBuilders.post("/api/resource/child").content(requestBody.toString())).andReturn();
            int updateStatus = updateResult.getResponse().getStatus();
            Assert.assertEquals("success", 200, updateStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    
    各位大佬,这种 controller 层的单元测试支持回滚吗?测试过程中感觉一会回滚一会又不回滚。。。
    
    1 replies    2018-06-01 16:12:24 +08:00
    enaxm
        1
    enaxm  
       Jun 1, 2018
    数据库操作只有一个 add?
    另外 Transcational 针对的异常是 unchecked
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1242 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:25 · PVG 01:25 · LAX 10:25 · JFK 13:25
    ♥ Do have faith in what you're doing.