2014-11-26 1 views

답변

14

많은 HTTP 클라이언트 API가 있습니다 (예 : Apache HttpClient). 은 클라이언트 측 테스트를 수행해야합니다. 우리는 어떻게해서든지 HTTP를 통해 우리의 서비스에 접근해야 할 것이므로이 API 중 하나가 단위 테스트를 위해 필요합니다. Jersey를 이미 사용하고 있으므로 Jersey 클라이언트 API을 선택하는 것이 좋습니다. 클라이언트 API는 우리 서비스를 호출 할 필요가 없습니다 볼 수 있듯이 예는

final String url = "http://stackoverflow.com/questions/27160440/" + 
            "jersey-client-api-vs-jersey-test-framework"; 
Client client = ClientBuilder.newClient(); 
WebTarget target = client.target(url); 
Response response = target.request().accept(MediaType..get(); 
String html = response.readEntity(String.class); 
System.out.println(html); 
response.close(); 

과 같을 수 있습니다. 그것은 "Rest"기능과 함께 HTTP 호출에 대한 인터페이스 일뿐입니다. 자체적 인 서비스를 실행하려면 먼저 컨테이너/전체 컨테이너 또는 일부 내장형 컨테이너에 배포해야합니다. 프레임 워크없이, 전체 시험은 더 복잡한 컨테이너 배포 옵션이있는 저지 테스트 프레임 워크 우리가 더 쉽게 반 통합/통합 테스트를 수행 할 수 있습니다

<dependencies> 
    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-grizzly2-http</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.9</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

public class SimpleTest { 

    static final String BASE_URI = "http://localhost:8080/myapp/"; 
    static HttpServer server; 
    static Client client; 

    private static HttpServer startServer() { 
     final ResourceConfig resourceConfig = new ResourceConfig() 
       .packages("where.my.resources.are") 
       .register(HelloResource.class); 
       // normally the resource class would not be in the unit test class 
       // and would be in the `where.my.resources.are` package pr sub package 
     return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig); 
    } 

    @Path("hello") 
    public static class HelloResource { 
     @GET 
     public String getHello() { 
      return "Hello World!"; 
     } 
    } 

    @BeforeClass 
    public static void setUpClass() { 
     server = startServer(); 
     client = ClientBuilder.newClient(); 
    } 

    @AfterClass 
    public static void tearDownClass() { 
     server.shutdown(); 
    } 

    @Test 
    public void test() { 
     WebTarget target = client.target(BASE_URI); 
     Response response = target.path("hello").request().get(); 
     String hello = response.readEntity(String.class); 
     assertEquals("Hello World!", hello); 
     response.close(); 
    } 
} 

같은 무언가를 볼 수 있습니다. 서비스는 단위 테스트 실행시 자동으로 시작되고 중지되는 경량 임베디드 컨테이너 (다른 유형)로 실행될 수 있습니다. 프레임 워크는 Jersey Client API에 실제로 종속되어 있으므로 프레임 워크를 사용하는 경우 테스트 케이스에서 Client API를 사용할 수 있습니다. 예

<dependencies> 
    <dependency> 
     <groupId>org.glassfish.jersey.test-framework.providers</groupId> 
     <artifactId>jersey-test-framework-provider-grizzly2</artifactId> 
     <version>2.13</version> 
    </dependency> 
</dependencies> 

public class SimpleTest extends JerseyTest { 

    @Path("hello") 
    public static class HelloResource { 
     @GET 
     public String getHello() { 
      return "Hello World!"; 
     } 
    } 

    @Override 
    protected Application configure() { 
     return new ResourceConfig(HelloResource.class); 
    } 

    @Test 
    public void test() { 
     Response response = target("hello").request().get(); 
     String hello = response.readEntity(String.class); 
     assertEquals("Hello World!", hello); 
     response.close(); 
    } 
} 

유사점 (@Test)을 볼 수 있습니다. 왜냐하면 우리는 클라이언트 API를 사용하고 있기 때문입니다. Client을 명시 적으로 구성 할 필요가 없습니다. 이는 프레임 워크가 우리를 위해 수행하기 때문입니다.

그래서 테스트 프레임 워크를 사용할지 여부는 실제로 선택됩니다. 읽기


당신이 그것을 어느 쪽이든을 사용하는 것입니다 당신이, 저지 클라이언트 API를 사용하는 방법을 알고 있어야합니다 어느 쪽이든 (그냥 HttpClient를 같은 다른 HTTTP 클라이언트 API와 함께 가기로 결정하지 않는 즉) 더

+1

저지 테스트 프레임 워크가 시작되고 각 테스트에 대한 컨테이너를 중지합니다. 모든 테스트를 위해 Grizzly 컨테이너를 재사용하고 싶습니다. 당신은 A) JerseyTest를 사용하고 TestContainerFactory를 서브 클래 싱하는 B) JerseyTest를 무시하고 클라이언트 API에서 위의 접근법을 사용합니까? 어느 쪽이 성능 친화인지 궁금해. (DI를 달성하기 위해 저지 스프링 다리를 사용하는 저어지 앱을 가지고 있으므로 DAO, 서비스, 리소스와 같은 모든 콩은 스프링 주석으로 주석 처리됩니다.) – Raf