Skip to content

Instantly share code, notes, and snippets.

@entrofi
Created March 20, 2019 10:26
Show Gist options
  • Save entrofi/c72382f6dedc655e426de8fff92b125e to your computer and use it in GitHub Desktop.
Save entrofi/c72382f6dedc655e426de8fff92b125e to your computer and use it in GitHub Desktop.
Rest Api Documentation with Rest Assured and Spring Boot
@Entity
public class Note {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String details;
public Note() {
}
public Note(String title, String details) {
this.title = title;
this.details = details;
}
//... getters and setters
}
package net.entrofi.spring.restassuredasciidoctor.data.repository;
import net.entrofi.spring.restassuredasciidoctor.data.entity.Note;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface NoteRepository extends JpaRepository<Note, Long> {
}
package net.entrofi.spring.restassuredasciidoctor.data.repository;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import net.entrofi.spring.restassuredasciidoctor.RestAssuredAsciiDoctorApplication;
import net.entrofi.spring.restassuredasciidoctor.data.entity.Note;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import static io.restassured.RestAssured.given;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RestAssuredAsciiDoctorApplication.class}, webEnvironment = RANDOM_PORT)
@AutoConfigureRestDocs
public class NoteRepositoryIntTest {
@Value("${local.server.port}")
private int port;
@Value("${deployment.environment.host:http://localhost}")
private String host;
@Autowired
private RequestSpecification documentationSpec;
@Test
public void saveNote_given_a_valid_note_request_should_return_created() {
Note note = new Note("Documented call", "Documented call details");
given(documentationSpec).filter(document("save-note-created"))
.body(note).contentType(ContentType.JSON)
.when().post(host + "/" + "notes")
.then().statusCode(HttpStatus.CREATED.value());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment