2017-10-10 63 views
1

我使用Hibernate和JPA與Spring Boot。春季DTO的單元測試,在Hibernate/Spring Boot中有一個字段集合

我有一個DTO這會從聯繫人,其中,設想來從客戶端的一種形式,將可能有多個手機發送信息收集:

public class ContactDTO { 

    private BigInteger userId; 
    private String contactName; 
    private Map<String, BigInteger> phones; 

    // getters and setters 
} 

我想象的數據將在一個JSON對象被髮送的格式如下:

{phones:[{"mobile":"2325552932"}, {"landline":"2235553329"}, ...]

我有一個POST方法設計爲處理控制器:

@PostMapping(path = "/newContact") 
    public String createNewContact(@ModelAttribute ContactDTO newContact) { 
    if (newContact.getPhones() !=null) { 
     // method that persists phone data 
    } 
    // .. use a CRUDRepository object to persist the data to MySQL DB 
    return "savedContact"; 
    } 

我想我的問題是雙重的:

  1. 將我的控制器能夠自動映射該格式的JSON對象?
  2. 我該如何測試?

我使用了Spring啓動測試,他們是這個樣子:

@RunWith(SpringRunner.class) 
@SpringBootTest 
@AutoConfigureMockMvc 
public class AdditiveControllerShould { 

    @Autowired 
    private AdditiveController additiveController; 

    @Autowired 
    private MockMvc mockMvc; 

@Test 
    public void saveAnEntryWhenPOSTOnlyUserIdAndContactName() throws Exception { 
    mockMvc.perform(post("/newContact") 
      .param("userId", "12345") 
      .param("contactName", "John Smith")) 
      // how to run test for the Map<String, BigInteger> ??? 
      .andExpect(status().isOk()) 
      .andExpect(content().string(containsString("savedContact"))); 
    } 
} 

回答

3

控制器

@PostMapping(value= "/newContact") 
    public String createNewContact(@RequestBody ContactDTO newContact) { 
    if (newContact.getPhones() !=null) { 
     // method that persists phone data 
    } 
    // .. use a CRUDRepository object to persist the data to MySQL DB 
    return "savedContact"; 
    } 

測試類

@RunWith(SpringRunner.class) 
    @SpringBootTest 
    @AutoConfigureMockMvc 
    public class AdditiveControllerShould { 

    @Autowired 
     private MockMvc mockMvc; 

    private static final ObjectMapper mapper=new ObjectMapper(); 

    @Test 
     public void saveAnEntryWhenPOSTOnlyUserIdAndContactName() throws Exception { 
     Map<String,String> phones=new HashMap<>(); 
     phones.put("phone1", "12345"); 
     Map<String,Object> input=new HashMap<>(); 
     input.put("userId", "123456"); 
     input.put("contactName", "TEST"); 
     input.put("phones", phones); 
     mockMvc.perform(post("/newContact") 
       .content(mapper.writeValueAsString(input)) 
       .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))   
       .andExpect(status().isOk()); 

     } 

} 

輸出:

CONTACT ContactDTO [userId=123456, contactName=TEST, phones={phone1=12345}] 
+0

我認爲這是接近,但它給我一個錯誤:'org.springframework.web.util.NestedServletException:請求處理失敗;嵌套異常爲javax.validation.ConstraintViolationException:對於組[javax.validation.groups.Default,]在持續時間期間類[app.models.relationentities.Contact]的驗證失敗約束違規列表:[ \t ConstraintViolationImpl {interpolatedMessage = '可能不爲空',propertyPath = name,rootBeanClass = class app.models.relationentities.Contact,messageTemplate ='{javax.validation.constraints.NotNull.message}'}' – NateH06

+0

我想也許這是一個名稱問題的變量,所以我改變了'input.put(「contactName」,「TEST」);'input.put(「name」,「TEST」);'但它仍然給我相同的錯誤 – NateH06

+0

它看起來像驗證問題。確保你獲得contactDTO實例,並從那裏你可以把它打開。我能夠獲得控制器內的實例。 – Barath