Build optimization (#2591)

This commit is contained in:
Grzegorz Piwowarek 2017-09-09 16:55:05 +02:00 committed by GitHub
parent 4589860efb
commit f01d1e257e
9 changed files with 148 additions and 58 deletions

View File

@ -71,9 +71,96 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/AutoconfigurationTest.java</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>autoconfiguration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties> <properties>
<mustache.compiler.api.version>0.9.2</mustache.compiler.api.version> <mustache.compiler.api.version>0.9.2</mustache.compiler.api.version>
<assertj.version>3.7.0</assertj.version> <assertj.version>3.7.0</assertj.version>

View File

@ -1,100 +1,98 @@
package com.baeldung.mustache; package com.baeldung.mustache;
import static org.assertj.core.api.Assertions.assertThat; import com.baeldung.mustache.model.Todo;
import com.github.mustachejava.Mustache;
import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.TemporalUnit;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.mustache.model.Todo;
import com.github.mustachejava.Mustache;
public class TodoMustacheServiceTest { public class TodoMustacheServiceTest {
private String executeTemplate(Mustache m, Map<String, Object> context) throws IOException{ private String executeTemplate(Mustache m, Map<String, Object> context) throws IOException {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
m.execute(writer, context).flush(); m.execute(writer, context).flush();
return writer.toString(); return writer.toString();
} }
private String executeTemplate(Mustache m, Todo todo) throws IOException{ private String executeTemplate(Mustache m, Todo todo) throws IOException {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
m.execute(writer, todo).flush(); m.execute(writer, todo).flush();
return writer.toString(); return writer.toString();
} }
@Test @Test
public void givenTodoObject_whenGetHtml_thenSuccess() throws IOException{ public void givenTodoObject_whenGetHtml_thenSuccess() throws IOException {
Todo todo = new Todo("Todo 1", "Todo description"); Todo todo = new Todo("Todo 1", "Todo description");
Mustache m = MustacheUtil.getMustacheFactory().compile("todo.mustache"); Mustache m = MustacheUtil.getMustacheFactory().compile("todo.mustache");
Map<String, Object> context = new HashMap<>(); Map<String, Object> context = new HashMap<>();
context.put("todo", todo); context.put("todo", todo);
String expected = "<h2>Todo 1</h2>"; String expected = "<h2>Todo 1</h2>";
assertThat(executeTemplate(m, todo)).contains(expected); assertThat(executeTemplate(m, todo)).contains(expected);
} }
@Test @Test
public void givenNullTodoObject_whenGetHtml_thenEmptyHtml() throws IOException{ public void givenNullTodoObject_whenGetHtml_thenEmptyHtml() throws IOException {
Mustache m = MustacheUtil.getMustacheFactory().compile("todo-section.mustache"); Mustache m = MustacheUtil.getMustacheFactory().compile("todo-section.mustache");
Map<String, Object> context = new HashMap<>(); Map<String, Object> context = new HashMap<>();
assertThat(executeTemplate(m, context)).isEmpty(); assertThat(executeTemplate(m, context)).isEmpty();
} }
@Test @Test
public void givenTodoList_whenGetHtml_thenSuccess() throws IOException{ public void givenTodoList_whenGetHtml_thenSuccess() throws IOException {
Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache"); Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache");
List<Todo> todos = Arrays.asList( List<Todo> todos = Arrays.asList(
new Todo("Todo 1", "Todo description"), new Todo("Todo 1", "Todo description"),
new Todo("Todo 2", "Todo description another"), new Todo("Todo 2", "Todo description another"),
new Todo("Todo 3", "Todo description another") new Todo("Todo 3", "Todo description another")
); );
Map<String, Object> context = new HashMap<>(); Map<String, Object> context = new HashMap<>();
context.put("todos", todos); context.put("todos", todos);
assertThat(executeTemplate(m, context)) assertThat(executeTemplate(m, context))
.contains("<h2>Todo 1</h2>") .contains("<h2>Todo 1</h2>")
.contains("<h2>Todo 2</h2>") .contains("<h2>Todo 2</h2>")
.contains("<h2>Todo 3</h2>"); .contains("<h2>Todo 3</h2>");
} }
@Test @Test
public void givenEmptyList_whenGetHtml_thenEmptyHtml() throws IOException{ public void givenEmptyList_whenGetHtml_thenEmptyHtml() throws IOException {
Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache"); Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache");
Map<String, Object> context = new HashMap<>(); Map<String, Object> context = new HashMap<>();
assertThat(executeTemplate(m, context)).isEmpty();; assertThat(executeTemplate(m, context)).isEmpty();
;
} }
@Test @Test
public void givenEmptyList_whenGetHtmlUsingInvertedSection_thenHtml() throws IOException{ public void givenEmptyList_whenGetHtmlUsingInvertedSection_thenHtml() throws IOException {
Mustache m = MustacheUtil.getMustacheFactory().compile("todos-inverted-section.mustache"); Mustache m = MustacheUtil.getMustacheFactory().compile("todos-inverted-section.mustache");
Map<String, Object> context = new HashMap<>(); Map<String, Object> context = new HashMap<>();
assertThat(executeTemplate(m, context).trim()).isEqualTo("<p>No todos!</p>"); assertThat(executeTemplate(m, context).trim()).isEqualTo("<p>No todos!</p>");
} }
@Test @Test
public void givenTodoList_whenGetHtmlUsingLamdba_thenHtml() throws IOException{ public void givenTodoList_whenGetHtmlUsingLamdba_thenHtml() throws IOException {
Mustache m = MustacheUtil.getMustacheFactory().compile("todos-lambda.mustache"); Mustache m = MustacheUtil.getMustacheFactory().compile("todos-lambda.mustache");
List<Todo> todos = Arrays.asList( List<Todo> todos = Arrays.asList(
new Todo("Todo 1", "Todo description"), new Todo("Todo 1", "Todo description"),
new Todo("Todo 2", "Todo description another"), new Todo("Todo 2", "Todo description another"),
new Todo("Todo 3", "Todo description another") new Todo("Todo 3", "Todo description another")
); );
todos.get(2).setDone(true); todos.get(2).setDone(true);
todos.get(2).setCompletedOn(Date.from(Instant.now().plusSeconds(300))); todos.get(2).setCompletedOn(Date.from(Instant.now().plusSeconds(300)));
Map<String, Object> context = new HashMap<>(); Map<String, Object> context = new HashMap<>();
context.put("todos", todos); context.put("todos", todos);
assertThat(executeTemplate(m, context).trim()).contains("Done 5 minutes ago"); assertThat(executeTemplate(m, context).trim()).contains("Done 5 minutes ago");

View File

@ -13,7 +13,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SpringMustacheApplicationTests { public class SpringMustacheApplicationIntegrationTest {
@Autowired @Autowired
private TestRestTemplate restTemplate; private TestRestTemplate restTemplate;

View File

@ -12,5 +12,4 @@ public class BeanA {
public BeanA() { public BeanA() {
super(); super();
} }
} }

View File

@ -8,5 +8,4 @@ public class BeanB {
public BeanB() { public BeanB() {
super(); super();
} }
} }

View File

@ -5,7 +5,6 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-rest-angular</artifactId> <artifactId>spring-rest-angular</artifactId>
<name>spring-rest-angular</name> <name>spring-rest-angular</name>
<groupId>com.baeldung</groupId>
<version>1.0</version> <version>1.0</version>
<packaging>war</packaging> <packaging>war</packaging>

View File

@ -2,6 +2,7 @@ package com.baeldung.controllers;
import com.baeldung.services.ExampleService; import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm; import com.baeldung.transfer.LoginForm;
import org.baeldung.web.main.Application;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -12,7 +13,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.baeldung.web.main.Application;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@ -21,11 +21,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
public class ExamplePostControllerRequestUnitTest { public class ExamplePostControllerRequestIntegrationTest {
MockMvc mockMvc; MockMvc mockMvc;
@Mock private ExampleService exampleService; @Mock
@InjectMocks private ExamplePostController exampleController; private ExampleService exampleService;
@InjectMocks
private ExamplePostController exampleController;
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}"; private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
private LoginForm lf = new LoginForm(); private LoginForm lf = new LoginForm();

View File

@ -2,6 +2,7 @@ package com.baeldung.controllers;
import com.baeldung.services.ExampleService; import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm; import com.baeldung.transfer.LoginForm;
import org.baeldung.web.main.Application;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -10,7 +11,6 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.baeldung.web.main.Application;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@ -22,12 +22,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
public class ExamplePostControllerResponseUnitTest { public class ExamplePostControllerResponseIntegrationTest {
private MockMvc mockMvc;
@Mock
private ExampleService exampleService;
@InjectMocks
private ExamplePostController exampleController;
MockMvc mockMvc;
@Mock private ExampleService exampleService;
@InjectMocks private ExamplePostController exampleController;
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
private LoginForm lf = new LoginForm(); private LoginForm lf = new LoginForm();
@Before @Before
@ -44,6 +48,7 @@ public class ExamplePostControllerResponseUnitTest {
public void requestBodyTest() { public void requestBodyTest() {
try { try {
when(exampleService.fakeAuthenticate(lf)).thenReturn(true); when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
mockMvc mockMvc
.perform(post("/post/response") .perform(post("/post/response")
.content(jsonBody) .content(jsonBody)

View File

@ -1,10 +1,5 @@
package org.baeldung.web.service; package org.baeldung.web.service;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.hamcrest.core.IsEqual.equalTo;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.baeldung.web.main.Application; import org.baeldung.web.main.Application;
import org.junit.Test; import org.junit.Test;
@ -13,6 +8,11 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.hamcrest.core.IsEqual.equalTo;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT) @SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class StudentServiceIntegrationTest { public class StudentServiceIntegrationTest {
@ -21,7 +21,8 @@ public class StudentServiceIntegrationTest {
@Test @Test
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() { public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("content.name", hasItems("Bryan", "Ben")); given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat()
.body("content.name", hasItems("Bryan", "Ben"));
} }
@Test @Test
@ -58,5 +59,4 @@ public class StudentServiceIntegrationTest {
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("first", equalTo(true)); given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("first", equalTo(true));
} }
} }