使用弹簧休息api

时间:2014-05-31 05:52:28

标签: spring rest

这是我的测试资源类

       package com.oasisdigital.rental.client;

import static javax.ws.rs.core.Response.Status.CREATED;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import javax.ws.rs.core.Response;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

 import com.oasisdigital.rental.test.AbstractIT;

  @Test
  public class ClientResourceIT extends AbstractIT {

  private ClientApi clientApi;

    @Override
   @BeforeMethod
   public void setUp() {
    super.setUp();

    this.clientApi = new ClientApi(api);
   }



  @Test
  public void shouldReturnClientAfterCreation() {
    Response resp = clientApi.postClient("Jimmy");
    assertThat(resp.getStatus(), is(CREATED.getStatusCode()));

    ClientDto client = resp.readEntity(ClientDto.class);
    assertThat(client.getId(), is(notNullValue()));
    assertThat(client.getName(), is("Jimmy"));

    assertThat(clientApi.getClients(), contains(client));
  }

}

这是我的Api课程

 public class ClientApi {
private WebTarget api;

public ClientApi(WebTarget apiRoot) {
    this.api = apiRoot.path("clients");
}

public List<ClientDto> getClients() {
    return api.request().get(new GenericType<List<ClientDto>>() {
    });
}

public Response postClient(String name) {
    return api.request().post(json(new ClientDto(name)));
}

public ClientDto createClient(String name) {
    return postClient(name).readEntity(ClientDto.class);
}

}

这是我的客户实体

 public class ClientDto {
  private Integer clientId;

@NotBlank
private String name;

public ClientDto() {
}

public ClientDto(Integer id, String name) {
    this.clientId = id;
    this.name = name;
}

public ClientDto(String name) {
    this.name = name;
}

public Integer getId() {
    return clientId;
}

public void setId(Integer id) {
    this.clientId = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public int hashCode() {
    return Objects.hashCode(clientId);
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof ClientDto)) {
        return false;
    }
    ClientDto other = (ClientDto) obj;
    return Objects.equals(clientId, other.clientId)
            && Objects.equals(name, other.name);
}

@Override
public String toString() {
    return "Client " + clientId + " / " + name;
}

}

问题是ClientResourceIT的测试功能shouldReturnClientAfterCreation()返回400而不是201.我在这里看不到任何错误。我正在使用Testng进行测试和Netbeans IDE

  public interface WebTarget extends Configurable<WebTarget> {

public URI getUri();

public UriBuilder getUriBuilder();

public WebTarget path(String string);

public WebTarget resolveTemplate(String string, Object o);

public WebTarget resolveTemplate(String string, Object o, boolean bln);

public WebTarget resolveTemplateFromEncoded(String string, Object o);

public WebTarget resolveTemplates(Map<String, Object> map);

public WebTarget resolveTemplates(Map<String, Object> map, boolean bln);

public WebTarget resolveTemplatesFromEncoded(Map<String, Object> map);

public WebTarget matrixParam(String string, Object[] os);

public WebTarget queryParam(String string, Object[] os);

public Invocation.Builder request();

public Invocation.Builder request(String[] strings);

public Invocation.Builder request(MediaType[] mts);

}

0 个答案:

没有答案