2012-06-07 4 views
0

Java 프로젝트에서 RESTful 애플리케이션을 사용하고 있습니다. 일반적으로 내 단위 테스트 클래스에서,이 같은 방법을 사용일반 메소드, RESTful 앱

public Employee getEmployeeByEmail(String email) { 
    ClientResponse clientResponse = webResource.path(beginPath + "getByEmail/" + email).get(
      ClientResponse.class); 

    Employee employee = null; 
    if (200 == clientResponse.getStatus()) { 
     employee = clientResponse.getEntity(Employee.class); 
    } 

    return employee; 
} 

을 ...하지만 나는 거의 12 다른 클래스에 유사한 방법을 사용합니다. '

javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException: Error creating JSON-based XMLStreamReader - with linked exception:[javax.xml.stream.XMLStreamException: java.io.IOException: stream is closed] 

그것은 내가 할 수있는 것 같다

public class TestManager<T> { 

private WebResource webResource; 
private String beginPath; 
private Class<T> clazz; 

public TestManager(WebResource webResource, String beginPath, Class<T> clazz) { 
    this.webResource = webResource; 
    this.beginPath = beginPath; 
    this.clazz = clazz; 
} 

public boolean objectExists(String methodPath, String uniqueFieldName, String uniqueField) { 
    boolean check = false; 
    ClientResponse clientResponse = webResource.path(beginPath + methodPath + "/" + uniqueField).get(
       ClientResponse.class); 
    JSONObject jsonObject = clientResponse.getEntity(JSONObject.class); 

    if (200 == clientResponse.getStatus() && !jsonObject.isNull(uniqueFieldName)) { 
     check = true; 
    } 

    return check; 
} 

public T getObjectById(String methodPath, long id) { 
    ClientResponse clientResponse = webResource.path(beginPath + methodPath + "/" + id).get(
      ClientResponse.class); 
    T object = null; 
    if (200 == clientResponse.getStatus() && !clientResponse.getEntity(JSONObject.class).isNull("id")) { 
     object = clientResponse.getEntity(clazz); 
    } 

    return object; 
} 

}

메소드 objectExists()가 잘 작동하지만는 getObjectById() 메소드는 스택 추적을 생성 : 이것은 내가하기로 결정 무엇인가 이 작업을 수행하십시오 :

object = clientResponse.getEntity(clazz); 

그러나 어떻게 수정해야하는지 잘 모릅니다. P

편집 : 임 사용 저지

Edit2가 : 솔루션 : 내 영어 죄송합니다 문제는 내가 두 번 getEntity() 메소드를 사용 ... 내가 한 번만 사용하는 경우했다 ... 그것은 작동합니다 ... 젠장.

답변

0

GET 호출입니다. 브라우저에서 호출 해보십시오.

동일한 오류가 발생하면 클라이언트에 아무런 문제가 없습니다.

+0

브라우저에서 전화를 걸면 문제가 없으며 안심할 수 있습니다. 하지만 내가 정상적인 방법으로 (첫 번째 코드 예제와 같은) 작동하지만, clientResponse.getEntity (clazz);를 시도 할 때 작동하지 않는 단위 테스트가 필요합니다. 제 생각에는 제네릭 타입에 문제가 있습니다 ... –

0

단위 테스트와 관련하여 문제가있는 경우 저지 테스트 프레임 워크를 살펴볼 수 있습니다. Here은 저지 문서에 대한 링크이며, Google은 추가 자습서를 가리킬 수 있습니다.

+0

저지를 사용하고 있지만 링크에 감사드립니다. –

관련 문제