Skip to content

Instantly share code, notes, and snippets.

@espendennis
espendennis / OfferApiDocumentation.class
Created October 17, 2016 09:34
RestDocs for OfferApi
package com.espen.documentation;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@espendennis
espendennis / pom.xml
Created October 17, 2016 09:40
Spring REST Docs dependency
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
</dependency>
@espendennis
espendennis / pom.xml
Created October 17, 2016 09:43
Plugins for Spring REST Docs
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Documentation.java</include>
</includes>
</configuration>
</plugin>
<plugin>
@espendennis
espendennis / pom.xml
Created October 17, 2016 09:49
Plugins for Spring REST Docs
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Documentation.java</include>
</includes>
</configuration>
</plugin>
<plugin>
@espendennis
espendennis / pom.xml
Created October 17, 2016 09:52
Properties for Spring REST Docs
<properties>
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
</properties>
@espendennis
espendennis / OfferApiDocumentation.class
Created October 17, 2016 11:31
First tests for the OffersApi
package com.espen.documentation;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
package com.espen.documentation;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@espendennis
espendennis / OfferApiDocumentation.class
Created October 17, 2016 12:00
Added field descriptions
@Test
public void postOffer() throws Exception {
Offer newOffer = new Offer("Innsbruck", "Lindau", "admin", "2016-10-10T09:50", 40);
String json = mapper.writeValueAsString(newOffer);
mockMvc.perform(post("/api/offers").contentType(MediaType.APPLICATION_JSON_UTF8).content(json))
.andExpect(status().isCreated())
.andDo(document("index", responseFields(
fieldWithPath("destination").description("The trips' destination"),
fieldWithPath("id").description("The offer's id"),
fieldWithPath("startingPoint").description("The trips' startingPoint"),
@espendennis
espendennis / http-response.adoc
Created October 17, 2016 12:02
Http response
HTTP/1.1 201 Created
Content-Type: application/json;charset=UTF-8
Content-Length: 144

{"id":3,"startingPoint":"Innsbruck","destination":"Lindau","username":"admin","date":"2016-10-10T09:50","price":40.0,"dateObject":1476085800000}
@espendennis
espendennis / response-fields.adoc
Created October 17, 2016 12:03
response fields with descriptions
Path Type Description

destination

String

The trips' destination

id

Number

The offer’s id