Skip to content

Commit f8bf403

Browse files
authored
Optimize and refactor (eugenp#2302)
1 parent 4e95722 commit f8bf403

32 files changed

Lines changed: 220 additions & 270 deletions

File tree

‎spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
import javax.servlet.FilterRegistration;
1515
import javax.servlet.ServletContext;
1616

17-
import static org.junit.Assert.*;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.Assert.assertTrue;
1820

1921
@RunWith(SpringRunner.class)
2022
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class)
2123
@AutoConfigureMockMvc
22-
@TestPropertySource(properties = { "security.basic.enabled=false" })
24+
@TestPropertySource(properties = {"security.basic.enabled=false"})
2325
public class SpringBootWithServletComponentIntegrationTest {
2426

25-
@Autowired private ServletContext servletContext;
27+
@Autowired
28+
private ServletContext servletContext;
2629

2730
@Test
2831
public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() {
@@ -42,7 +45,8 @@ public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() {
4245
.contains("echo servlet"));
4346
}
4447

45-
@Autowired private TestRestTemplate restTemplate;
48+
@Autowired
49+
private TestRestTemplate restTemplate;
4650

4751
@Test
4852
public void givenServletFilter_whenGetHello_thenRequestFiltered() {
@@ -59,7 +63,6 @@ public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() {
5963
}
6064

6165

62-
6366
}
6467

6568

‎spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@
1414
import javax.servlet.FilterRegistration;
1515
import javax.servlet.ServletContext;
1616

17-
import static org.junit.Assert.*;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.Assert.assertNull;
1820

1921
@RunWith(SpringRunner.class)
2022
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
2123
@AutoConfigureMockMvc
22-
@TestPropertySource(properties = { "security.basic.enabled=false" })
24+
@TestPropertySource(properties = {"security.basic.enabled=false"})
2325
public class SpringBootWithoutServletComponentIntegrationTest {
2426

25-
@Autowired private ServletContext servletContext;
27+
@Autowired
28+
private ServletContext servletContext;
2629

27-
@Autowired private TestRestTemplate restTemplate;
30+
@Autowired
31+
private TestRestTemplate restTemplate;
2832

2933
@Test
3034
public void givenServletContext_whenAccessAttrs_thenNotFound() {

‎spring-boot/src/test/java/com/baeldung/autoconfiguration/AutoconfigurationIntegrationTest.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.baeldung.autoconfiguration;
22

3+
import com.baeldung.autoconfiguration.example.AutoconfigurationApplication;
4+
import com.baeldung.autoconfiguration.example.MyUser;
5+
import com.baeldung.autoconfiguration.example.MyUserRepository;
36
import org.junit.Test;
47
import org.junit.runner.RunWith;
58
import org.springframework.beans.factory.annotation.Autowired;
69
import org.springframework.boot.test.context.SpringBootTest;
710
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
811
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
912

10-
import com.baeldung.autoconfiguration.example.AutoconfigurationApplication;
11-
import com.baeldung.autoconfiguration.example.MyUser;
12-
import com.baeldung.autoconfiguration.example.MyUserRepository;
13-
1413
@RunWith(SpringJUnit4ClassRunner.class)
1514
@SpringBootTest(classes = AutoconfigurationApplication.class)
16-
@EnableJpaRepositories(basePackages = { "com.baeldung.autoconfiguration.example" })
15+
@EnableJpaRepositories(basePackages = {"com.baeldung.autoconfiguration.example"})
1716
public class AutoconfigurationIntegrationTest {
1817

1918
@Autowired

‎spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
package com.baeldung.displayallbeans;
22

3-
import static org.assertj.core.api.BDDAssertions.then;
4-
import static org.hamcrest.CoreMatchers.hasItem;
5-
import static org.junit.Assert.assertThat;
6-
import static org.junit.Assert.assertTrue;
7-
8-
import java.util.Arrays;
9-
import java.util.List;
10-
import java.util.Map;
11-
import java.util.stream.Collectors;
12-
133
import org.junit.Test;
144
import org.junit.runner.RunWith;
155
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,7 +13,15 @@
2313
import org.springframework.test.context.junit4.SpringRunner;
2414
import org.springframework.web.context.WebApplicationContext;
2515

26-
import com.baeldung.displayallbeans.Application;
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.stream.Collectors;
20+
21+
import static org.assertj.core.api.BDDAssertions.then;
22+
import static org.hamcrest.CoreMatchers.hasItem;
23+
import static org.junit.Assert.assertThat;
24+
import static org.junit.Assert.assertTrue;
2725

2826
@RunWith(SpringRunner.class)
2927
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@@ -46,10 +44,10 @@ public class DisplayBeanIntegrationTest {
4644
public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception {
4745
ResponseEntity<String> entity = this.testRestTemplate.getForEntity(
4846
"http://localhost:" + this.port + "/displayallbeans", String.class);
49-
47+
5048
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5149
}
52-
50+
5351
@Test
5452
public void givenRestTemplate_whenAccessEndpointUrl_thenHttpStatusOK() throws Exception {
5553
@SuppressWarnings("rawtypes")
@@ -68,14 +66,14 @@ public void givenRestTemplate_whenAccessEndpointUrl_thenReturnsBeanNames() throw
6866
List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans");
6967
List<String> beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList());
7068

71-
assertThat( beanNamesList, hasItem("fooController"));
72-
assertThat( beanNamesList, hasItem("fooService"));
69+
assertThat(beanNamesList, hasItem("fooController"));
70+
assertThat(beanNamesList, hasItem("fooService"));
7371
}
74-
72+
7573
@Test
7674
public void givenWebApplicationContext_whenAccessGetBeanDefinitionNames_thenReturnsBeanNames() throws Exception {
7775
String[] beanNames = context.getBeanDefinitionNames();
78-
76+
7977
List<String> beanNamesList = Arrays.asList(beanNames);
8078
assertTrue(beanNamesList.contains("fooController"));
8179
assertTrue(beanNamesList.contains("fooService"));

‎spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@RunWith(SpringRunner.class)
1919
@SpringBootTest
2020
@AutoConfigureMockMvc
21-
@TestPropertySource(properties = { "security.basic.enabled=false" })
21+
@TestPropertySource(properties = {"security.basic.enabled=false"})
2222
public class AppLiveTest {
2323

2424
@Autowired
@@ -27,15 +27,15 @@ public class AppLiveTest {
2727
@Test
2828
public void getIndex() throws Exception {
2929
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
30-
.andExpect(status().isOk())
31-
.andExpect(content().string(equalTo("Index Page")));
30+
.andExpect(status().isOk())
31+
.andExpect(content().string(equalTo("Index Page")));
3232
}
3333

3434
@Test
3535
public void getLocal() throws Exception {
3636
mvc.perform(MockMvcRequestBuilders.get("/local").accept(MediaType.APPLICATION_JSON))
37-
.andExpect(status().isOk())
38-
.andExpect(content().string(equalTo("/local")));
37+
.andExpect(status().isOk())
38+
.andExpect(content().string(equalTo("/local")));
3939
}
4040

4141
}

‎spring-boot/src/test/java/com/baeldung/toggle/ToggleIntegrationTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.baeldung.toggle;
22

3-
import static org.junit.Assert.*;
4-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
5-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6-
73
import org.junit.Before;
84
import org.junit.Test;
95
import org.junit.runner.RunWith;
@@ -16,6 +12,10 @@
1612
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1713
import org.springframework.web.context.WebApplicationContext;
1814

15+
import static org.junit.Assert.assertEquals;
16+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18+
1919
@RunWith(SpringJUnit4ClassRunner.class)
2020
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class)
2121
@AutoConfigureMockMvc
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.utils;
22

3+
import com.baeldung.utils.controller.UtilsController;
34
import org.junit.Before;
45
import org.junit.Test;
56
import org.mockito.InjectMocks;
@@ -10,32 +11,29 @@
1011
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
1112
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1213

13-
14-
import com.baeldung.utils.controller.UtilsController;
15-
1614
public class UtilsControllerIntegrationTest {
1715

18-
@InjectMocks
16+
@InjectMocks
1917
private UtilsController utilsController;
20-
18+
2119
private MockMvc mockMvc;
22-
20+
2321
@Before
2422
public void setup() {
2523
MockitoAnnotations.initMocks(this);
2624
this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController)
27-
.build();
25+
.build();
2826

2927
}
30-
28+
3129
@Test
3230
public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception {
33-
String param = "testparam";
34-
this.mockMvc.perform(
35-
post("/setParam")
36-
.param("param", param)
37-
.sessionAttr("parameter", param))
38-
.andExpect(status().isOk());
31+
String param = "testparam";
32+
this.mockMvc.perform(
33+
post("/setParam")
34+
.param("param", param)
35+
.sessionAttr("parameter", param))
36+
.andExpect(status().isOk());
3937
}
40-
38+
4139
}

‎spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerIntegrationTest.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
import org.springframework.messaging.simp.stomp.StompHeaders;
77
import org.springframework.messaging.simp.stomp.StompSession;
88

9-
/**
10-
* Test class for MyStompSessionHandler
11-
* @author Kalyan
12-
*
13-
*/
149
public class MyStompSessionHandlerIntegrationTest {
1510

1611
@Test

‎spring-boot/src/test/java/org/baeldung/boot/boottest/EmployeeControllerIntegrationTest.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public void whenPostEmployee_thenCreateEmployee() throws Exception {
4545
given(service.save(Mockito.anyObject())).willReturn(alex);
4646

4747
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON)
48-
.content(JsonUtil.toJson(alex)))
49-
.andExpect(status().isCreated())
50-
.andExpect(jsonPath("$.name", is("alex")));
48+
.content(JsonUtil.toJson(alex)))
49+
.andExpect(status().isCreated())
50+
.andExpect(jsonPath("$.name", is("alex")));
5151
verify(service, VerificationModeFactory.times(1)).save(Mockito.anyObject());
5252
reset(service);
5353
}
@@ -63,11 +63,11 @@ public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Excepti
6363
given(service.getAllEmployees()).willReturn(allEmployees);
6464

6565
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON))
66-
.andExpect(status().isOk())
67-
.andExpect(jsonPath("$", hasSize(3)))
68-
.andExpect(jsonPath("$[0].name", is(alex.getName())))
69-
.andExpect(jsonPath("$[1].name", is(john.getName())))
70-
.andExpect(jsonPath("$[2].name", is(bob.getName())));
66+
.andExpect(status().isOk())
67+
.andExpect(jsonPath("$", hasSize(3)))
68+
.andExpect(jsonPath("$[0].name", is(alex.getName())))
69+
.andExpect(jsonPath("$[1].name", is(john.getName())))
70+
.andExpect(jsonPath("$[2].name", is(bob.getName())));
7171
verify(service, VerificationModeFactory.times(1)).getAllEmployees();
7272
reset(service);
7373
}

‎spring-boot/src/test/java/org/baeldung/boot/boottest/EmployeeRepositoryIntegrationTest.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package org.baeldung.boot.boottest;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
5-
import java.util.List;
6-
73
import org.junit.Test;
84
import org.junit.runner.RunWith;
95
import org.springframework.beans.factory.annotation.Autowired;
106
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
117
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
128
import org.springframework.test.context.junit4.SpringRunner;
139

10+
import java.util.List;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
1414
@RunWith(SpringRunner.class)
1515
@DataJpaTest
1616
public class EmployeeRepositoryIntegrationTest {
@@ -65,7 +65,7 @@ public void givenSetOfEmployees_whenFindAll_thenReturnAllEmployees() {
6565
List<Employee> allEmployees = employeeRepository.findAll();
6666

6767
assertThat(allEmployees).hasSize(3)
68-
.extracting(Employee::getName)
69-
.containsOnly(alex.getName(), ron.getName(), bob.getName());
68+
.extracting(Employee::getName)
69+
.containsOnly(alex.getName(), ron.getName(), bob.getName());
7070
}
7171
}

0 commit comments

Comments
 (0)