2015-01-21 3 views
5

Apache CXF 3.0.0을 사용 중이며 JAX-RS 구성으로 정의 된 서비스가 거의 없습니다. 우리는 Spring Framework로 계층 구조를 구성했습니다. 이러한 서비스의 입/출력은 JSON 문자열입니다.CXF 유닛 테스트

내 서비스의 유효성을 검사하는 Junit 테스트 케이스의 실제 예를 찾고 있습니다. 또한 Maven 빌드에서 테스트를 구성하십시오. 이 방법을 추천합니다

나는

https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Testing을 언급? 그럼에도 불구하고 설치를 시도했지만 성공하지 못하고 어디로 가는지 이해할 수 없었습니다.

답변

5

나는 당신이 당신의 링크에서 언급 한 접근법을 좋아하지만 당신의 설정에 달려있다. 내 my personal git repository 전체에서 당신은 당신이 체크 아웃 할 수

<dependency> 
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-rt-transports-http-jetty</artifactId> 
     <version>3.0.2</version> 
    </dependency> 

Plugins section: 

<plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>build-helper-maven-plugin</artifactId> 
     <version>1.5</version> 
     <executions> 
      <execution> 
      <id>reserve-network-port</id> 
      <goals> 
       <goal>reserve-network-port</goal> 
      </goals> 
      <phase>process-test-resources</phase> 
      <configuration> 
       <portNames> 
       <portName>test.server.port</portName> 
       </portNames> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 

     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.18.1</version> 
     <configuration> 
      <systemPropertyVariables> 
      <basePath>http://localhost:${test.server.port}/api</basePath> 
      </systemPropertyVariables> 
     </configuration> 
     </plugin> 
    </plugins> 

당신의 메이븐의 pom.xml에 있어야합니다

// Normal Spring Junit integration in my case with dbunit 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:/root-test-context.xml", "classpath:/rest-test-context.xml" }) 
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 
@DatabaseSetup("AuthenticationResourceTest-dataset.xml") 
@DatabaseTearDown("AuthenticationResourceTest-dataset.xml") 
public class AuthenticationResourceTest { 
    // This variable is populated from surfire and reserve port maven plugin 
    @Value("#{systemProperties['basePath'] ?: \"http://localhost:9080/api/\"}") 
    private String basePath; 

    // I assume that you have in your spring context the rest server 
    @Autowired 
    private JAXRSServerFactoryBean serverFactory; 

    private Server server; 

    @Before 
    public void beforeMethod() { 
     serverFactory.setBindingId(JAXRSBindingFactory.JAXRS_BINDING_ID); 
     // Specify where your rest service will be deployed 
     serverFactory.setAddress(basePath); 
     server = serverFactory.create(); 
     server.start(); 
    } 

    @Test 
    public void authenticateTest() throws Exception { 
     // You can test your rest resources here. 
     // Using client factory 
     // AutenticationResourceclient = JAXRSClientFactory.create(basePath, AutenticationResource.class); 
     // Or URLConnection 
     String query = String.format("invitation=%s", URLEncoder.encode(invitation, "UTF-8")); 
     URL url = new URL(endpoint + "/auth?" + query); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     try (InputStream is = connection.getInputStream();) { 
      String line; 
      // read it with BufferedReader 
      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 

      while ((line = br.readLine()) != null) { 
       System.out.println(line); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @After 
    public void afterMethod() { 
     server.stop(); 
     server.destroy(); 
    } 

} 

: 나는 봄 구성하여 CXF 서버에 대한 JUnit 테스트를 만들고 관리하는 방법을 보여줍니다 예 :

+0

답장을 보내 주셔서 감사합니다. 설치와 업데이트시에이 방법을 적용하려고합니다. –