2016-06-25 7 views
0

Ich möchte Unit Test für meine RESTful API auf der Grundlage von Spring Framework verwenden, ich verwendete mysql, um meine Daten zu speichern und PagingAndSortingRepository mit meiner RESTful API implementiert, und das ist mein Testcode:test Spring RESTful Put-Methode fehlgeschlagen mit MockMVC

@RunWith(SpringJUnit4ClassRunner.class) 
    @SpringApplicationConfiguration(classes = SpringMvcApplication.class) 
    @WebAppConfiguration 
    public class CustomerRepositoryTests { 


     private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), 
     SUBTYPE); 

     private static final String SUBTYPE = "hal+json"; 

     private MockMvc mockMvc; 
     @Autowired 
     private WebApplicationContext webApplicationContext; 

     @Autowired 
     private CustomerRepository customerRepository; 

     private long setupId; 

     @Before 
     public void setup() { 
      this.mockMvc = webAppContextSetup(webApplicationContext) 
      .apply(springSecurity()) 
      .build(); 
      customerRepository.deleteAll(); 
      Customer customer = customerRepository.save(new Customer("userId", "my mobile", "my address", "my contactName")); 
      setupId = customer.getId(); 
     } 


     @Test 
     //// FIXME: 6/26/16 Status Code is always 204, not 200! 
     public void changeCustomer() throws Exception { 
      mockMvc.perform(put("/api" + "/customers/{id}", setupId) 
      .content(TestUtil.objToJson(new Customer("my new userId", "my new mobile", "my new address", "my new contactName"))) 
      .contentType(contentType)) 
      .andExpect(status().isOk()) 
      .andExpect(jsonPath("$.userId", is("my new userId"))) 
      .andExpect(jsonPath("$.contactName", is("my new contactName"))) 
      .andExpect(jsonPath("$.mobile", is("my new mobile"))) 
      .andExpect(jsonPath("$.address", is("my new address"))); 
     } 
} 

mein Test immer gescheitert und sagt:

java.lang.AssertionError: Status 
    Expected :200 
    Actual :204 

aber wenn ich meine Anwendung ausführen und einen Befehl wie:

 curl -X PUT -H "Content-Type:application/hal+json" -d '{ "userId": "Bilbo", "mobile": "Baggins", "contactName":"my new contact", "address":"new address" }' http://localhost:8080/api/customers/1 

mein Server gibt Statuscode 200 zurück und aktualisiert die Daten erfolgreich. Ich suchte eine Weile durch, habe aber keine Ahnung davon.

bearbeitet: hier ist mein CustomerRepository:

public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> { 
    } 

Kunde:

@Data 
@Entity 
@Table(name = "customer") 
public class Customer { 
    @Id 
    @GeneratedValue() 
    private Long id; 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "created", nullable = false) 
    private Date created; 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "updated", nullable = false) 
    private Date updated; 

    @Version 
    @JsonIgnore 
    private Long version; 

    @NotNull 
    private String userId; 

    //TODO: Limit type to 1, 2, 3 or 4 
    private int type; 

    private String companyName; 

    private String phone; 

    @NotNull 
    private String mobile; 

    @NotNull 
    private String address; 

    private String zip; 

    @NotNull 
    private String contactName; 


    private String email; 


    public Customer(String userId, String mobile, String address, String contactName) { 
     this(userId, 1, null, null, mobile, address, null, contactName, null); 
    } 

    public Customer() {} 


    public Customer(Long id, String userId, String mobile, String address, String contactName) { 
     this(id, userId, 1, null, null, mobile, address, null, contactName, null); 
    } 

    public Customer(Long id, String userId, int type, String companyName, String phone, String mobile, String address, String zip, String contactName, String email) { 
     this.id = id; 
     this.userId = userId; 
     this.type = type; 
     this.companyName = companyName; 
     this.phone = phone; 
     this.mobile = mobile; 
     this.address = address; 
     this.zip = zip; 
     this.contactName = contactName; 
     this.email = email; 
    } 


    public Customer(String userId, int type, String companyName, String phone, String mobile, String address, String zip, String contactName, String email) { 
     this.userId = userId; 
     this.type = type; 
     this.companyName = companyName; 
     this.phone = phone; 
     this.mobile = mobile; 
     this.address = address; 
     this.zip = zip; 
     this.contactName = contactName; 
     this.email = email; 
    } 

    public Long getId() { 
     return id; 
    } 


    public String getUserId() { 
     return userId; 
    } 

    public void setUserId(String userId) { 
     this.userId = userId; 
    } 

    public int getType() { 
     return type; 
    } 

    public void setType(int type) { 
     this.type = type; 
    } 

    public String getCompanyName() { 
     return companyName; 
    } 

    public void setCompanyName(String companyName) { 
     this.companyName = companyName; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    public String getMobile() { 
     return mobile; 
    } 

    public void setMobile(String mobile) { 
     this.mobile = mobile; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getZip() { 
     return zip; 
    } 

    public void setZip(String zip) { 
     this.zip = zip; 
    } 

    public String getContactName() { 
     return contactName; 
    } 

    public void setContactName(String contactName) { 
     this.contactName = contactName; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 



    @PrePersist 
    protected void onCreate() { 
     updated = created = new Date(); 
    } 

    @PreUpdate 
    protected void onUpdate() { 
     updated = new Date(); 
    } 



    @Override 
    public String toString() { 
     return "Customer{" + 
      "id=" + id + 
      ", creation time=" + created + 
      ", userId=" + userId + 
      ", type=" + type + 
      ", companyName='" + companyName + '\'' + 
      ", phone='" + phone + '\'' + 
      ", mobile='" + mobile + '\'' + 
      ", address='" + address + '\'' + 
      ", zip='" + zip + '\'' + 
      ", contactName='" + contactName + '\'' + 
      ", email='" + email + '\'' + 
      '}'; 
    } 
} 
+0

Können Sie Ihre Restendpunkt-Implementierung hinzufügen? – niekname

+0

@niekname Ich habe von diesem [link] (http://spring.io/guides/tutorials/react-and-spring-data-rest/) verwiesen und ich denke, ich habe etwas mit meiner Ruheendpunktimplementierung nicht getan. Vielleicht verwende ich seine Standardimplementierung? –

Antwort

2

Es ist wie der Test sucht Standardkonfiguration verwendet, die nach kehrt 204 No Content zu Spring Data REST - Reference Documentation

5.1.1. Default status codes

Für die freiliegenden Ressourcen verwenden wir eine Reihe von Statuscodes default:

  • 200 OK - für Normal GET-Anfragen.

  • 201 Erstellt - für POST- und PUT-Anforderungen, die neue Ressourcen erstellen.

  • 204 Kein Inhalt - für PUT, PATCH und DELETE-Anforderungen, wenn die Konfiguration gesetzt Antwortstellen für Ressourcen Updates (RepositoryRestConfiguration.returnBodyOnUpdate) nicht zurück. Wenn der Konfigurationswert so eingestellt ist, dass Antworten für PUT enthalten sind, wird 200 OK für Aktualisierungen zurückgegeben. 201 Created wird für Ressource zurückgegeben, die über PUT erstellt wurde.

Wenn die Konfiguration (RepositoryRestConfiguration.returnBodyOnUpdate und RepositoryRestConfiguration.returnBodyCreate) Werte sind explizit auf null, die Anwesenheit des HTTP-Header akzeptiert der Antwortcode zu bestimmen, verwendet werden.