From 811a28deb144f05deecf180da52d5ebc3544cadc Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 21 Mar 2018 16:26:42 +0100 Subject: [PATCH] Added documentation with a explicit mode sample and web app context sample; fixes gh-370 --- docs/src/main/asciidoc/verifier_setup.adoc | 60 ++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/docs/src/main/asciidoc/verifier_setup.adoc b/docs/src/main/asciidoc/verifier_setup.adoc index 5991f34c96..54f642adbc 100644 --- a/docs/src/main/asciidoc/verifier_setup.adoc +++ b/docs/src/main/asciidoc/verifier_setup.adoc @@ -613,16 +613,68 @@ import org.mycompany.ExampleSpringController import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc import spock.lang.Specification -class MvcSpec extends Specification { +class MvcSpec extends Specification { def setup() { RestAssuredMockMvc.standaloneSetup(new ExampleSpringController()) } } ---- -If you use `Explicit` mode, you can use a base class to initialize the whole tested app -similarly, as you might find in regular integration tests. If you use the `JAXRSCLIENT` -mode, this base class should also contain a `protected WebTarget webTarget` field. Right +You can also setup the whole context if necessary. + +[source,java,indent=0] +---- +import io.restassured.module.mockmvc.RestAssuredMockMvc; +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.test.context.junit4.SpringRunner; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = SomeConfig.class, properties="some=property") +public abstract class BaseTestClass { + + @Autowired + WebApplicationContext context; + + @Before + public void setup() { + RestAssuredMockMvc.webAppContextSetup(this.context); + } +} +---- + +If you use `EXPLICIT` mode, you can use a base class to initialize the whole tested app +similarly, as you might find in regular integration tests. + +[source,java,indent=0] +---- +import io.restassured.RestAssured; +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.web.server.LocalServerPort +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = SomeConfig.class, properties="some=property") +public abstract class BaseTestClass { + + @LocalServerPort + int port; + + @Before + public void setup() { + RestAssured.baseURI = "http://localhost:" + this.port; + } +} +---- + +If you use the `JAXRSCLIENT` mode, this base class should also contain a `protected WebTarget webTarget` field. Right now, the only option to test the JAX-RS API is to start a web server. [[maven-different-base]]