I am having a play with testing a RESTful api which I have set up using Jersey. I want to test it using JUnit while running it with Grizzly to provide the Http container.
General testing using the server works ok, i.e. sending requests and receiving responses etc.
The api is called CMSApi and it has a dependency called skyService which I would like to mock out so I'm testing just the api. So the question is how can I inject the CMSApi with a mockSkyService object created using Mockito? The relevant code is below:
Grizzly Server start up:
public static HttpServer startServer() {
final ResourceConfig rc = new ResourceConfig().packages("com.sky");
rc.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
My JUnit calls the above start method:
@Before
public void setUpServer() throws Exception {
// start the server and create the client
server = Main.startServer();
Client client = ClientBuilder.newClient();
target = client.target(Main.BASE_URI);
}
I run the test using the @RunWith(MockitoJUnitRunner.class)
.
Here are the relevant objects in the test:
//System Under Test
private CMSApi cmsApi;
//Mock
@Mock private static SkyService mockSkyService;
Here is the test method calling:
@Test
public void testSaveTile() {
//given
final Invocation.Builder invocationBuilder = target.path(PATH_TILE).request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
Tile tile = new Tile(8, "LABEL", clientId, new Date(), null);
//when
Response response = invocationBuilder.post(Entity.entity(tile, MediaType.APPLICATION_JSON_TYPE));
//then
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
Maven dependencies
<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>com.sky</groupId>
<artifactId>sky-service</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sky-service</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-bundle</artifactId>
<type>pom</type>
<scope>test</scope>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- Dependency for Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.sky.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.15</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>