diff --git a/README.adoc b/README.adoc index 2290ec0a2048c8a4c6267eda1c421a48f72d8b8f..073bf991d701f30df370343b545611b4366835d0 100644 --- a/README.adoc +++ b/README.adoc @@ -44,7 +44,7 @@ NOTE: Spring Boot doesn't generate code or make edits to your files. Instead, wh Now you can create a web controller for a simple web application. `src/main/java/hello/HelloController.java` -[source,java] +[source,java,tabsize=2] ---- include::initial/src/main/java/hello/HelloController.java[] ---- @@ -55,7 +55,7 @@ The class is flagged as a `@RestController`, meaning it's ready for use by Sprin Here you create an `Application` class with the components: `src/main/java/hello/Application.java` -[source,java] +[source,java,tabsize=2] ---- include::complete/src/main/java/hello/Application.java[] ---- @@ -149,7 +149,7 @@ include::complete/pom.xml[tag=tests] Now write a simple unit test that mocks the servlet request and response through your endpoint: `src/test/java/hello/HelloControllerTest.java` -[source,java] +[source,java,tabsize=2] ---- include::complete/src/test/java/hello/HelloControllerTest.java[] ---- @@ -159,7 +159,7 @@ The `MockMvc` comes from Spring Test and allows you, via a set of convenient bui As well as mocking the HTTP request cycle we can also use Spring Boot to write a very simple full-stack integration test. For example, instead of (or as well as) the mock test above we could do this: `src/test/java/hello/HelloControllerIT.java` -[source,java] +[source,java,tabsize=2] ---- include::complete/src/test/java/hello/HelloControllerIT.java[] ---- diff --git a/complete/src/main/java/hello/Application.java b/complete/src/main/java/hello/Application.java index 5a129e462e1a422b57b4a60d77e2f70de586219a..4f12d19cdc7cc458244ce70814f769494316143d 100644 --- a/complete/src/main/java/hello/Application.java +++ b/complete/src/main/java/hello/Application.java @@ -11,23 +11,23 @@ import org.springframework.context.annotation.Bean; @SpringBootApplication public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } - @Bean - public CommandLineRunner commandLineRunner(ApplicationContext ctx) { - return args -> { + @Bean + public CommandLineRunner commandLineRunner(ApplicationContext ctx) { + return args -> { - System.out.println("Let's inspect the beans provided by Spring Boot:"); + System.out.println("Let's inspect the beans provided by Spring Boot:"); - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - System.out.println(beanName); - } + String[] beanNames = ctx.getBeanDefinitionNames(); + Arrays.sort(beanNames); + for (String beanName : beanNames) { + System.out.println(beanName); + } - }; - } + }; + } } diff --git a/complete/src/main/java/hello/HelloController.java b/complete/src/main/java/hello/HelloController.java index d9c1069cc5974931d491d6502ccb1d258663ad1c..2e82e973b697878c3758fb6f247d89fbfd9c0746 100644 --- a/complete/src/main/java/hello/HelloController.java +++ b/complete/src/main/java/hello/HelloController.java @@ -5,10 +5,10 @@ import org.springframework.web.bind.annotation.RequestMapping; @RestController public class HelloController { - - @RequestMapping("/") - public String index() { - return "Greetings from Spring Boot!"; - } - + + @RequestMapping("/") + public String index() { + return "Greetings from Spring Boot!"; + } + } diff --git a/complete/src/test/java/hello/HelloControllerIT.java b/complete/src/test/java/hello/HelloControllerIT.java index 8bda42870e9f4bb039a46399b6f696b3c7b33119..fad7beec688567fae8f39ae19fe854acd875934a 100644 --- a/complete/src/test/java/hello/HelloControllerIT.java +++ b/complete/src/test/java/hello/HelloControllerIT.java @@ -19,23 +19,23 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class HelloControllerIT { - @LocalServerPort - private int port; + @LocalServerPort + private int port; - private URL base; + private URL base; - @Autowired - private TestRestTemplate template; + @Autowired + private TestRestTemplate template; - @Before - public void setUp() throws Exception { - this.base = new URL("http://localhost:" + port + "/"); - } + @Before + public void setUp() throws Exception { + this.base = new URL("http://localhost:" + port + "/"); + } - @Test - public void getHello() throws Exception { - ResponseEntity response = template.getForEntity(base.toString(), - String.class); - assertThat(response.getBody(), equalTo("Greetings from Spring Boot!")); - } + @Test + public void getHello() throws Exception { + ResponseEntity response = template.getForEntity(base.toString(), + String.class); + assertThat(response.getBody(), equalTo("Greetings from Spring Boot!")); + } } diff --git a/complete/src/test/java/hello/HelloControllerTest.java b/complete/src/test/java/hello/HelloControllerTest.java index 5ff9d4badb72e02bb371921e035c4ebdf6dc1cb9..4a4684c3ce64741f94d7c4c38b5d3c715e8b2470 100644 --- a/complete/src/test/java/hello/HelloControllerTest.java +++ b/complete/src/test/java/hello/HelloControllerTest.java @@ -19,13 +19,13 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @AutoConfigureMockMvc public class HelloControllerTest { - @Autowired - private MockMvc mvc; + @Autowired + private MockMvc mvc; - @Test - public void getHello() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().string(equalTo("Greetings from Spring Boot!"))); - } + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Greetings from Spring Boot!"))); + } } diff --git a/initial/src/main/java/hello/Application.java b/initial/src/main/java/hello/Application.java index 84ca6380401c50e74f9ff2caf594d981e4d81f47..780ac4091c486a6ac577c6572817014a86882ed4 100644 --- a/initial/src/main/java/hello/Application.java +++ b/initial/src/main/java/hello/Application.java @@ -8,17 +8,17 @@ import org.springframework.context.ApplicationContext; @SpringBootApplication public class Application { - - public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(Application.class, args); - - System.out.println("Let's inspect the beans provided by Spring Boot:"); - - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - System.out.println(beanName); - } - } + + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(Application.class, args); + + System.out.println("Let's inspect the beans provided by Spring Boot:"); + + String[] beanNames = ctx.getBeanDefinitionNames(); + Arrays.sort(beanNames); + for (String beanName : beanNames) { + System.out.println(beanName); + } + } } diff --git a/initial/src/main/java/hello/HelloController.java b/initial/src/main/java/hello/HelloController.java index d9c1069cc5974931d491d6502ccb1d258663ad1c..2e82e973b697878c3758fb6f247d89fbfd9c0746 100644 --- a/initial/src/main/java/hello/HelloController.java +++ b/initial/src/main/java/hello/HelloController.java @@ -5,10 +5,10 @@ import org.springframework.web.bind.annotation.RequestMapping; @RestController public class HelloController { - - @RequestMapping("/") - public String index() { - return "Greetings from Spring Boot!"; - } - + + @RequestMapping("/") + public String index() { + return "Greetings from Spring Boot!"; + } + }