Junit CXF单元测试

Junit CXF单元测试,junit,cxf,jax-rs,Junit,Cxf,Jax Rs,我使用的是ApacheCXF3.0.0,使用JAX-RS配置定义的服务很少。我们使用Spring框架进行分层配置。这些服务的输入/输出是JSON字符串 我正在搜索Junit测试用例的工作示例,以验证我的服务。还要在Maven Build中配置测试 我提到 这是推荐的方法吗? 尽管如此,我尝试设置,但未能成功,无法理解如何连接。我喜欢您在链接中提到的方法,但这取决于您的设置。 我将展示如何使用spring配置为cxf服务器创建junit测试: // Normal Spring Junit inte

我使用的是ApacheCXF3.0.0,使用JAX-RS配置定义的服务很少。我们使用Spring框架进行分层配置。这些服务的输入/输出是JSON字符串

我正在搜索Junit测试用例的工作示例,以验证我的服务。还要在Maven Build中配置测试

我提到

这是推荐的方法吗?
尽管如此,我尝试设置,但未能成功,无法理解如何连接。

我喜欢您在链接中提到的方法,但这取决于您的设置。 我将展示如何使用spring配置为cxf服务器创建junit测试:

// 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();
    }

}
您需要在maven pom.xml中包含

    <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>

org.apache.cxf

完整示例:

非常感谢您的回答,我将尝试在设置和更新中采用这种方法。