Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CXF unit testing

Tags:

junit

jax-rs

cxf

I am using Apache CXF 3.0.0 and have few services defined with JAX-RS configuration. We have hierarchical configuration with Spring Framework. These input/output of these services are JSON strings.

I am searching for a working example of Junit test cases to validate my services. Also configure the test in Maven Build.

I referred https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Testing

Is it recommended approach? Nevertheless, I tried to setup but could not succeed, could not understand how to wire it.

like image 949
Yogesh Manware Avatar asked Jan 21 '15 09:01

Yogesh Manware


People also ask

What is Cxf used for?

CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

What is Apache CXF tutorial?

Apache CXF is a JAX-WS fully compliant framework. On top of features defined by JAX-WS standards, Apache CXF provides the capability of conversion between WSDL and Java classes, APIs used to manipulate raw XML messages, the support for JAX-RS, integration with the Spring Framework, etc.


1 Answers

I like the approach you mention in your link, but it depends on your set up. I show how I managed to create junit test for cxf server using spring configuration:

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

}

You need to have in your 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>

You can check out my from my personal git repository the complete example:

like image 68
Antonio Maria Sanchez Berrocal Avatar answered Sep 23 '22 23:09

Antonio Maria Sanchez Berrocal