Skip to content

Commit 9ac01eb

Browse files
authored
Java 44187 Update Each Article for the Guide - Spring Boot Intro (#18685)
1 parent ddfdeb9 commit 9ac01eb

12 files changed

+90
-98
lines changed

spring-boot-modules/spring-boot-simple/pom.xml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<parent>
1313
<groupId>org.springframework.boot</groupId>
1414
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>3.5.0-M1</version>
15+
<version>3.5.4</version>
1616
<relativePath/>
1717
</parent>
1818

@@ -110,22 +110,14 @@
110110
<plugin>
111111
<groupId>org.springframework.boot</groupId>
112112
<artifactId>spring-boot-maven-plugin</artifactId>
113-
<version>3.3.2</version>
114113
</plugin>
115114
</plugins>
116115
</build>
117116

118117
<properties>
119118
<start-class>com.baeldung.bootstrap.Application</start-class>
120119
<subethasmtp.version>7.0.2</subethasmtp.version>
120+
<spring-boot-maven-plugin.version>3.5.4</spring-boot-maven-plugin.version>
121121
</properties>
122122

123-
<repositories>
124-
<repository>
125-
<id>repository.spring.milestones</id>
126-
<name>Spring Milestones Repository</name>
127-
<url>https://repo.spring.io/milestone/</url>
128-
</repository>
129-
</repositories>
130-
131123
</project>

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/actuator/ActuatorInfoIntegrationTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package com.baeldung.actuator;
22

33
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.extension.ExtendWith;
54
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.boot.test.context.SpringBootTest;
76
import org.springframework.boot.test.web.client.TestRestTemplate;
87
import org.springframework.http.HttpStatus;
98
import org.springframework.http.ResponseEntity;
10-
import org.springframework.test.context.junit.jupiter.SpringExtension;
119

1210
import static org.junit.jupiter.api.Assertions.assertEquals;
1311

14-
@ExtendWith(SpringExtension.class)
1512
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
1613
public class ActuatorInfoIntegrationTest {
1714

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/boot/testing/EmployeeControllerIntegrationTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import org.junit.jupiter.api.BeforeEach;
44
import org.junit.jupiter.api.Test;
5-
import org.junit.jupiter.api.extension.ExtendWith;
65
import org.mockito.Mockito;
76
import org.mockito.internal.verification.VerificationModeFactory;
87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
109
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
1110
import org.springframework.boot.test.mock.mockito.MockBean;
1211
import org.springframework.http.MediaType;
13-
import org.springframework.test.context.junit.jupiter.SpringExtension;
1412
import org.springframework.test.web.servlet.MockMvc;
1513

1614
import java.util.Arrays;
@@ -26,7 +24,6 @@
2624
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2725
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2826

29-
@ExtendWith(SpringExtension.class)
3027
@WebMvcTest(value = EmployeeRestController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
3128
public class EmployeeControllerIntegrationTest {
3229

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/boot/testing/EmployeeRepositoryIntegrationTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package com.baeldung.boot.testing;
22

33
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.extension.ExtendWith;
54
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
76
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
8-
import org.springframework.test.context.junit.jupiter.SpringExtension;
97

108
import java.util.List;
119

1210
import static org.assertj.core.api.Assertions.assertThat;
1311

14-
@ExtendWith(SpringExtension.class)
1512
@DataJpaTest
1613
public class EmployeeRepositoryIntegrationTest {
1714

@@ -41,13 +38,15 @@ public void whenFindById_thenReturnEmployee() {
4138
Employee emp = new Employee("test");
4239
entityManager.persistAndFlush(emp);
4340

44-
Employee fromDb = employeeRepository.findById(emp.getId()).orElse(null);
41+
Employee fromDb = employeeRepository.findById(emp.getId())
42+
.orElse(null);
4543
assertThat(fromDb.getName()).isEqualTo(emp.getName());
4644
}
4745

4846
@Test
4947
public void whenInvalidId_thenReturnNull() {
50-
Employee fromDb = employeeRepository.findById(-11l).orElse(null);
48+
Employee fromDb = employeeRepository.findById(-11l)
49+
.orElse(null);
5150
assertThat(fromDb).isNull();
5251
}
5352

@@ -64,6 +63,8 @@ public void givenSetOfEmployees_whenFindAll_thenReturnAllEmployees() {
6463

6564
List<Employee> allEmployees = employeeRepository.findAll();
6665

67-
assertThat(allEmployees).hasSize(3).extracting(Employee::getName).containsOnly(alex.getName(), ron.getName(), bob.getName());
66+
assertThat(allEmployees).hasSize(3)
67+
.extracting(Employee::getName)
68+
.containsOnly(alex.getName(), ron.getName(), bob.getName());
6869
}
6970
}

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/boot/testing/EmployeeRestControllerIntegrationTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import org.junit.jupiter.api.AfterEach;
44
import org.junit.jupiter.api.Test;
5-
import org.junit.jupiter.api.extension.ExtendWith;
65
import org.springframework.beans.factory.annotation.Autowired;
76
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
87
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
98
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
109
import org.springframework.boot.test.context.SpringBootTest;
1110
import org.springframework.http.MediaType;
1211
import org.springframework.test.context.TestPropertySource;
13-
import org.springframework.test.context.junit.jupiter.SpringExtension;
1412
import org.springframework.test.web.servlet.MockMvc;
1513

1614
import java.io.IOException;
@@ -25,10 +23,9 @@
2523
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
2624
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2725

28-
@ExtendWith(SpringExtension.class)
2926
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = Application.class)
30-
@AutoConfigureMockMvc
31-
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
27+
@AutoConfigureMockMvc
28+
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
3229
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
3330
public class EmployeeRestControllerIntegrationTest {
3431

@@ -46,10 +43,12 @@ public void resetDb() {
4643
@Test
4744
public void whenValidInput_thenCreateEmployee() throws IOException, Exception {
4845
Employee bob = new Employee("bob");
49-
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(bob)));
46+
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON)
47+
.content(JsonUtil.toJson(bob)));
5048

5149
List<Employee> found = repository.findAll();
52-
assertThat(found).extracting(Employee::getName).containsOnly("bob");
50+
assertThat(found).extracting(Employee::getName)
51+
.containsOnly("bob");
5352
}
5453

5554
@Test

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/boot/testing/EmployeeServiceImplIntegrationTest.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ public void setUp() {
4444

4545
List<Employee> allEmployees = Arrays.asList(john, bob, alex);
4646

47-
Mockito.when(employeeRepository.findByName(john.getName())).thenReturn(john);
48-
Mockito.when(employeeRepository.findByName(alex.getName())).thenReturn(alex);
49-
Mockito.when(employeeRepository.findByName("wrong_name")).thenReturn(null);
50-
Mockito.when(employeeRepository.findById(john.getId())).thenReturn(Optional.of(john));
51-
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployees);
52-
Mockito.when(employeeRepository.findById(-99L)).thenReturn(Optional.empty());
47+
Mockito.when(employeeRepository.findByName(john.getName()))
48+
.thenReturn(john);
49+
Mockito.when(employeeRepository.findByName(alex.getName()))
50+
.thenReturn(alex);
51+
Mockito.when(employeeRepository.findByName("wrong_name"))
52+
.thenReturn(null);
53+
Mockito.when(employeeRepository.findById(john.getId()))
54+
.thenReturn(Optional.of(john));
55+
Mockito.when(employeeRepository.findAll())
56+
.thenReturn(allEmployees);
57+
Mockito.when(employeeRepository.findById(-99L))
58+
.thenReturn(Optional.empty());
5359
}
5460

5561
@Test
@@ -107,21 +113,26 @@ public void given3Employees_whengetAll_thenReturn3Records() {
107113

108114
List<Employee> allEmployees = employeeService.getAllEmployees();
109115
verifyFindAllEmployeesIsCalledOnce();
110-
assertThat(allEmployees).hasSize(3).extracting(Employee::getName).contains(alex.getName(), john.getName(), bob.getName());
116+
assertThat(allEmployees).hasSize(3)
117+
.extracting(Employee::getName)
118+
.contains(alex.getName(), john.getName(), bob.getName());
111119
}
112120

113121
private void verifyFindByNameIsCalledOnce(String name) {
114-
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findByName(name);
122+
Mockito.verify(employeeRepository, VerificationModeFactory.times(1))
123+
.findByName(name);
115124
Mockito.reset(employeeRepository);
116125
}
117126

118127
private void verifyFindByIdIsCalledOnce() {
119-
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());
128+
Mockito.verify(employeeRepository, VerificationModeFactory.times(1))
129+
.findById(Mockito.anyLong());
120130
Mockito.reset(employeeRepository);
121131
}
122132

123133
private void verifyFindAllEmployeesIsCalledOnce() {
124-
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findAll();
134+
Mockito.verify(employeeRepository, VerificationModeFactory.times(1))
135+
.findAll();
125136
Mockito.reset(employeeRepository);
126137
}
127138
}

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/bootstrap/SpringBootBootstrapLiveTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.baeldung.bootstrap;
22

3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
38
import com.baeldung.bootstrap.persistence.model.Book;
9+
410
import io.restassured.RestAssured;
511
import io.restassured.response.Response;
12+
613
import org.junit.jupiter.api.BeforeEach;
714
import org.junit.jupiter.api.Test;
815
import org.springframework.boot.test.context.SpringBootTest;
@@ -12,11 +19,6 @@
1219

1320
import java.util.List;
1421

15-
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
16-
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
17-
import static org.junit.jupiter.api.Assertions.assertEquals;
18-
import static org.junit.jupiter.api.Assertions.assertTrue;
19-
2022
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2123
public class SpringBootBootstrapLiveTest {
2224

@@ -128,10 +130,11 @@ private Book createRandomBook() {
128130

129131
private String createBookAsUri(Book book) {
130132
final Response response = RestAssured.given()
131-
.contentType(MediaType.APPLICATION_JSON_VALUE)
132-
.body(book)
133-
.post(API_ROOT);
134-
return API_ROOT + "/" + response.jsonPath().get("id");
133+
.contentType(MediaType.APPLICATION_JSON_VALUE)
134+
.body(book)
135+
.post(API_ROOT);
136+
return API_ROOT + "/" + response.jsonPath()
137+
.get("id");
135138
}
136139

137140
}

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/bootstrap/SpringContextTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.baeldung.bootstrap;
22

33
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.extension.ExtendWith;
54
import org.springframework.boot.test.context.SpringBootTest;
6-
import org.springframework.test.context.junit.jupiter.SpringExtension;
75

8-
@ExtendWith(SpringExtension.class)
96
@SpringBootTest
107
public class SpringContextTest {
118

spring-boot-modules/spring-boot-simple/src/test/java/com/baeldung/configurationproperties/PropertiesConversionIntegrationTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.baeldung.configurationproperties;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
35
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.extension.ExtendWith;
6+
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.boot.test.context.SpringBootTest;
79
import org.springframework.test.context.TestPropertySource;
8-
import org.springframework.test.context.junit.jupiter.SpringExtension;
910
import org.springframework.util.unit.DataSize;
1011

1112
import java.time.Duration;
1213

13-
import static org.junit.jupiter.api.Assertions.assertEquals;
14-
15-
@ExtendWith(SpringExtension.class)
1614
@SpringBootTest(classes = PropertiesConversionApplication.class)
1715
@TestPropertySource("classpath:conversion.properties")
1816
public class PropertiesConversionIntegrationTest {
@@ -31,13 +29,15 @@ public void whenUseTimeUnitPropertyConversion_thenSuccess() throws Exception {
3129
public void whenUseDataSizePropertyConversion_thenSuccess() throws Exception {
3230
assertEquals(DataSize.ofBytes(300), properties.getSizeInDefaultUnit());
3331
assertEquals(DataSize.ofGigabytes(2), properties.getSizeInGB());
34-
assertEquals(DataSize.ofTerabytes(4), properties.getSizeInTB());
32+
assertEquals(DataSize.ofTerabytes(4), properties.getSizeInTB());
3533
}
36-
34+
3735
@Test
3836
public void whenUseCustomPropertyConverter_thenSuccess() throws Exception {
39-
assertEquals("john", properties.getEmployee().getName());
40-
assertEquals(2000.0, properties.getEmployee().getSalary());
37+
assertEquals("john", properties.getEmployee()
38+
.getName());
39+
assertEquals(2000.0, properties.getEmployee()
40+
.getSalary());
4141
}
42-
42+
4343
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package com.baeldung.starter;
22

3+
import static org.hamcrest.Matchers.hasSize;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
5+
36
import org.junit.jupiter.api.BeforeEach;
47
import org.junit.jupiter.api.Test;
5-
import org.junit.jupiter.api.extension.ExtendWith;
8+
69
import org.springframework.beans.factory.annotation.Autowired;
710
import org.springframework.boot.test.context.SpringBootTest;
811
import org.springframework.http.MediaType;
9-
import org.springframework.test.context.junit.jupiter.SpringExtension;
1012
import org.springframework.test.context.web.WebAppConfiguration;
1113
import org.springframework.test.web.servlet.MockMvc;
1214
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
1315
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1416
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1517
import org.springframework.web.context.WebApplicationContext;
1618

17-
import static org.hamcrest.Matchers.equalTo;
18-
import static org.hamcrest.Matchers.hasSize;
19-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
20-
21-
@ExtendWith(SpringExtension.class)
2219
@SpringBootTest(classes = Application.class)
2320
@WebAppConfiguration
2421
public class SpringBootApplicationIntegrationTest {
@@ -30,24 +27,17 @@ public class SpringBootApplicationIntegrationTest {
3027

3128
@BeforeEach
3229
public void setupMockMvc() {
33-
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
30+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
31+
.build();
3432
}
3533

3634
@Test
37-
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect()
38-
throws Exception {
35+
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
3936
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all"))
40-
.andExpect(MockMvcResultMatchers.status().isOk())
41-
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
42-
.andExpect(jsonPath("$", hasSize(4)));
37+
.andExpect(MockMvcResultMatchers.status()
38+
.isOk())
39+
.andExpect(MockMvcResultMatchers.content()
40+
.contentType(MediaType.APPLICATION_JSON))
41+
.andExpect(jsonPath("$", hasSize(4)));
4342
}
44-
45-
@Test
46-
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect()
47-
throws Exception {
48-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30"))
49-
.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
50-
.andExpect(jsonPath("$.id", equalTo(1)));
51-
}
52-
5343
}

0 commit comments

Comments
 (0)