Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part II (Testing using RestTemplate and JUNIT)

As a follow-up to my previous post Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part I, this post will explain a feature of Apache CXF which enables testing the services using JUNIT simple and easy.

Part I – A simple RESTful Webservices using Apache CXF
Part II – Testing using RestTemplate and JUNIT
Part III – Exception/Error handling using ExceptionMapper
Part IV – BeanParam Example

By including cxf-rt-transports-http-jetty dependency in the project, Apache-CXF deploys the services on an embedded jetty server.

This feature comes very handy in testing the services using JUNIT.

This example also uses Spring Framework’s RestTemplate to invoke the RESTful services.

Maven Dependency XML (pom.xml)


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-cxf-rest-example</groupId>
    <artifactId>spring-cxf-rest-example</artifactId>
    <packaging>war</packaging>

    <version>1.0-SNAPSHOT</version>
    <name>spring-cxf-rest-example</name>

    <properties>
        <spring.version>4.0.0.RELEASE</spring.version>
        <cxf.version>3.0.0</cxf.version>
        <jackson.version>2.0.1</jackson.version>
        <junit.version>4.11</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>spring-cxf-rest-example</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
</project>

Spring/CXF Test Configuration (test-context.xml)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xmlns:cxf="http://cxf.apache.org/core"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

    <context:component-scan base-package="com.idodevjobs" />

    <jaxrs:server id="exampleCxfServer" address="http://localhost:8080/spring-cxf-rest-example/services/">
        <jaxrs:serviceBeans>
            <ref bean="exampleService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json"/>
        </jaxrs:extensionMappings>
        <jaxrs:features>
            <cxf:logging/>
        </jaxrs:features>
        <jaxrs:providers>
            <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
            <bean class="com.idodevjobs.sample.exception.mapper.ClientExceptionMapper" />
            <bean class="com.idodevjobs.sample.exception.mapper.ServerExceptionMapper" />
            <bean class="com.idodevjobs.sample.exception.mapper.ThrowableMapper" />
        </jaxrs:providers>
    </jaxrs:server>

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate" />

</beans>

JUNIT Test (ExampleServiceImplRestTest.java)


package com.idodevjobs.sample.service;

import com.idodevjobs.sample.model.ExampleModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class ExampleServiceImplRestTest {

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void testCxfRestService() {
        ResponseEntity<ExampleModel> entity = restTemplate.getForEntity("http://localhost:8080/spring-cxf-rest-example/services/example/1", ExampleModel.class);

        assertThat(entity.getStatusCode(), equalTo(HttpStatus.OK));
    }
}


Refer to my previous post for other required classes and files.

This test can be run from any IDE or as part of maven build.

Please share and comment if you find this useful.

Please comment for any questions/issues.

Advertisements
This entry was posted in java and tagged , , , , , . Bookmark the permalink.

9 Responses to Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part II (Testing using RestTemplate and JUNIT)

  1. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part III (Exception/Error handling using ExceptionMapper) | iDoDevJobs

  2. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part I | iDoDevJobs

  3. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part III (Exception/Error handling using ExceptionMapper) | iDoDevJobs

  4. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part IV (JAX-RS’ BeanParam Example) | iDoDevJobs

  5. Muhammed Rahman says:

    It is a nice article.
    I have some suggestions:
    1. Add following dependency in pom.xml:

    org.hamcrest
    hamcrest-all
    1.3

    2. Remove test from following dependency in pom.xml:

    junit
    junit
    ${junit.version}

    4. Replace &quot; with ” in ExampleServiceImplRestTest.java
    5. Replace &lt; with in ExampleServiceImplRestTest.java

    Thanks

    Like

    • idodevjobs says:

      Thanks for the feedback.

      I have fixed the “, < issues.

      I don't think there are any issues with the dependencies – JUNIT is a test only dependency and JUNIT brings its dependency hamcrest-core.

      Like

  6. Muhammed Rahman says:

    sorry, some characters are not showing up in the comment

    Like

  7. awanish kumar says:

    Hi,

    This really help me altos, but when i am trying to get request.getSession() like below, it throwing “no sessionManager”. Please help me out from this issue, i am stucked here from last two days.

    Your kind response is awaited….

    @Service(“exampleService”)
    public class ExampleServiceImpl implements ExampleService {

    @Override
    public ExampleModel get(String modelId, HttpServletRequest request) {
    request.getSession();// it throw no sessionmanager exception.
    return new ExampleModel(“example”, 1001);
    }
    }

    Regards,
    Awanish Kumar

    Like

  8. Pingback: Spring Data REST Java Configuration Example | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s