formatting work

This commit is contained in:
Eugen Paraschiv
2018-03-04 17:53:14 +02:00
parent 5fd8e4293e
commit 8d633d6402
48 changed files with 101 additions and 248 deletions
@@ -40,8 +40,7 @@ public class SpringBootWithServletComponentIntegrationTest {
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
assertNotNull(filterRegistration);
assertTrue(filterRegistration.getServletNameMappings()
.contains("echo servlet"));
assertTrue(filterRegistration.getServletNameMappings().contains("echo servlet"));
}
@Autowired
@@ -60,11 +60,8 @@ public class DisplayBeanIntegrationTest {
@SuppressWarnings("rawtypes")
ResponseEntity<List> entity = this.testRestTemplate.getForEntity("http://localhost:" + this.mgt + "/springbeans", List.class);
List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody()
.get(0)).get("beans");
List<String> beanNamesList = allBeans.stream()
.map(x -> (String) x.get("bean"))
.collect(Collectors.toList());
List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans");
List<String> beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList());
assertThat(beanNamesList, hasItem("fooController"));
assertThat(beanNamesList, hasItem("fooService"));
@@ -26,18 +26,12 @@ public class AppLiveTest {
@Test
public void getIndex() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Index Page")));
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("Index Page")));
}
@Test
public void getLocal() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/local")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("/local")));
mvc.perform(MockMvcRequestBuilders.get("/local").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("/local")));
}
}
@@ -39,7 +39,8 @@ public class KongAdminAPILiveTest {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
@Autowired TestRestTemplate restTemplate;
@Autowired
TestRestTemplate restTemplate;
@Test
public void givenEndpoint_whenQueryStockPrice_thenPriceCorrect() {
@@ -30,7 +30,8 @@ public class KongLoadBalanceLiveTest {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
@Autowired TestRestTemplate restTemplate;
@Autowired
TestRestTemplate restTemplate;
@Test
public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception {
@@ -35,8 +35,7 @@ public class ToggleIntegrationTest {
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
.build();
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
@@ -46,8 +45,7 @@ public class ToggleIntegrationTest {
System.setProperty("employee.feature", "false");
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + ""))
.andExpect(status().is(200));
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200));
emp = employeeRepository.findOne(1L);
assertEquals("salary incorrect", 2000, emp.getSalary(), 0.5);
@@ -60,8 +58,7 @@ public class ToggleIntegrationTest {
System.setProperty("employee.feature", "true");
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + ""))
.andExpect(status().is(200));
mockMvc.perform(post("/increaseSalary").param("id", emp.getId() + "")).andExpect(status().is(200));
emp = employeeRepository.findOne(1L);
assertEquals("salary incorrect", 2200, emp.getSalary(), 0.5);
@@ -21,17 +21,14 @@ public class UtilsControllerIntegrationTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController)
.build();
this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController).build();
}
@Test
public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception {
String param = "testparam";
this.mockMvc.perform(post("/setParam").param("param", param)
.sessionAttr("parameter", param))
.andExpect(status().isOk());
this.mockMvc.perform(post("/setParam").param("param", param).sessionAttr("parameter", param)).andExpect(status().isOk());
}
}
@@ -14,9 +14,7 @@ public class MyStompSessionHandlerIntegrationTest {
StompHeaders mockHeader = Mockito.mock(StompHeaders.class);
MyStompSessionHandler sessionHandler = new MyStompSessionHandler();
sessionHandler.afterConnected(mockSession, mockHeader);
Mockito.verify(mockSession)
.subscribe("/topic/messages", sessionHandler);
Mockito.verify(mockSession)
.send(Mockito.anyString(), Mockito.anyObject());
Mockito.verify(mockSession).subscribe("/topic/messages", sessionHandler);
Mockito.verify(mockSession).send(Mockito.anyString(), Mockito.anyObject());
}
}
@@ -34,56 +34,36 @@ public class SpringBootApplicationIntegrationTest {
@Before
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all"))
.andExpect(MockMvcResultMatchers.status()
.isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(contentType))
.andExpect(jsonPath("$", hasSize(4)));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4)));
}
@Test
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30"))
.andExpect(MockMvcResultMatchers.status()
.isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(contentType))
.andExpect(jsonPath("$.id", equalTo(1)));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
.andExpect(jsonPath("$.id", equalTo(1)));
}
@Test
public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name()))
.andExpect(MockMvcResultMatchers.status()
.isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(contentType))
.andExpect(jsonPath("$.id", equalTo(1)));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$.id", equalTo(1)));
}
@Test
public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion")
.header("Version", "1.0.0"))
.andExpect(MockMvcResultMatchers.status()
.isOk())
.andExpect(MockMvcResultMatchers.content()
.contentType(contentType))
.andExpect(jsonPath("$.id", equalTo(1)));
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
.andExpect(jsonPath("$.id", equalTo(1)));
}
}
@@ -61,15 +61,11 @@ public class SpringBootMailIntegrationTest {
}
private String getMessage(WiserMessage wiserMessage) throws MessagingException, IOException {
return wiserMessage.getMimeMessage()
.getContent()
.toString()
.trim();
return wiserMessage.getMimeMessage().getContent().toString().trim();
}
private String getSubject(WiserMessage wiserMessage) throws MessagingException {
return wiserMessage.getMimeMessage()
.getSubject();
return wiserMessage.getMimeMessage().getSubject();
}
private SimpleMailMessage composeEmailMessage() {
@@ -33,8 +33,7 @@ public class DetailsServiceClientIntegrationTest {
@Before
public void setUp() throws Exception {
String detailsString = objectMapper.writeValueAsString(new Details("John Smith", "john"));
this.server.expect(requestTo("/john/details"))
.andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON));
this.server.expect(requestTo("/john/details")).andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON));
}
@Test
@@ -41,7 +41,7 @@ public class H2TestProfileJPAConfig {
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.domain", "org.baeldung.boot.boottest","org.baeldung.model" });
em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.domain", "org.baeldung.boot.boottest", "org.baeldung.model" });
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.setJpaProperties(additionalProperties());
return em;
@@ -33,8 +33,7 @@ public class CustomConverterIntegrationTest {
public void whenConvertStringToEmployee_thenSuccess() {
Employee employee = conversionService.convert("1,50000.00", Employee.class);
Employee actualEmployee = new Employee(1, 50000.00);
assertThat(conversionService.convert("1,50000.00", Employee.class))
.isEqualToComparingFieldByField(actualEmployee);
assertThat(conversionService.convert("1,50000.00", Employee.class)).isEqualToComparingFieldByField(actualEmployee);
}
@Test
@@ -44,11 +43,8 @@ public class CustomConverterIntegrationTest {
@Test
public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() {
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class))
.isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN));
assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class))
.isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23)));
assertThat(conversionService.convert("2.32", BigDecimal.class))
.isEqualTo(BigDecimal.valueOf(2.32));
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN));
assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)).isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23)));
assertThat(conversionService.convert("2.32", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(2.32));
}
}
@@ -26,10 +26,6 @@ public class StringToEmployeeConverterControllerIntegrationTest {
@Test
public void getStringToEmployeeTest() throws Exception {
mockMvc.perform(get("/string-to-employee?employee=1,2000"))
.andDo(print())
.andExpect(jsonPath("$.id", is(1)))
.andExpect(jsonPath("$.salary", is(2000.0)))
.andExpect(status().isOk());
mockMvc.perform(get("/string-to-employee?employee=1,2000")).andDo(print()).andExpect(jsonPath("$.id", is(1))).andExpect(jsonPath("$.salary", is(2000.0))).andExpect(status().isOk());
}
}
@@ -47,10 +47,7 @@ public class EmployeeControllerIntegrationTest {
Employee alex = new Employee("alex");
given(service.save(Mockito.anyObject())).willReturn(alex);
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(alex)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name", is("alex")));
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(alex))).andExpect(status().isCreated()).andExpect(jsonPath("$.name", is("alex")));
verify(service, VerificationModeFactory.times(1)).save(Mockito.anyObject());
reset(service);
}
@@ -65,12 +62,8 @@ public class EmployeeControllerIntegrationTest {
given(service.getAllEmployees()).willReturn(allEmployees);
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].name", is(alex.getName())))
.andExpect(jsonPath("$[1].name", is(john.getName())))
.andExpect(jsonPath("$[2].name", is(bob.getName())));
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(3))).andExpect(jsonPath("$[0].name", is(alex.getName()))).andExpect(jsonPath("$[1].name", is(john.getName())))
.andExpect(jsonPath("$[2].name", is(bob.getName())));
verify(service, VerificationModeFactory.times(1)).getAllEmployees();
reset(service);
}
@@ -66,8 +66,6 @@ public class EmployeeRepositoryIntegrationTest {
List<Employee> allEmployees = employeeRepository.findAll();
assertThat(allEmployees).hasSize(3)
.extracting(Employee::getName)
.containsOnly(alex.getName(), ron.getName(), bob.getName());
assertThat(allEmployees).hasSize(3).extracting(Employee::getName).containsOnly(alex.getName(), ron.getName(), bob.getName());
}
}
@@ -50,12 +50,10 @@ public class EmployeeRestControllerIntegrationTest {
@Test
public void whenValidInput_thenCreateEmployee() throws IOException, Exception {
Employee bob = new Employee("bob");
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(bob)));
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(bob)));
List<Employee> found = repository.findAll();
assertThat(found).extracting(Employee::getName)
.containsOnly("bob");
assertThat(found).extracting(Employee::getName).containsOnly("bob");
}
@Test
@@ -64,13 +62,8 @@ public class EmployeeRestControllerIntegrationTest {
createTestEmployee("bob");
createTestEmployee("alex");
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2))))
.andExpect(jsonPath("$[0].name", is("bob")))
.andExpect(jsonPath("$[1].name", is("alex")));
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2))))
.andExpect(jsonPath("$[0].name", is("bob"))).andExpect(jsonPath("$[1].name", is("alex")));
}
private void createTestEmployee(String name) {
@@ -47,18 +47,12 @@ public class EmployeeServiceImplIntegrationTest {
List<Employee> allEmployees = Arrays.asList(john, bob, alex);
Mockito.when(employeeRepository.findByName(john.getName()))
.thenReturn(john);
Mockito.when(employeeRepository.findByName(alex.getName()))
.thenReturn(alex);
Mockito.when(employeeRepository.findByName("wrong_name"))
.thenReturn(null);
Mockito.when(employeeRepository.findById(john.getId()))
.thenReturn(john);
Mockito.when(employeeRepository.findAll())
.thenReturn(allEmployees);
Mockito.when(employeeRepository.findById(-99L))
.thenReturn(null);
Mockito.when(employeeRepository.findByName(john.getName())).thenReturn(john);
Mockito.when(employeeRepository.findByName(alex.getName())).thenReturn(alex);
Mockito.when(employeeRepository.findByName("wrong_name")).thenReturn(null);
Mockito.when(employeeRepository.findById(john.getId())).thenReturn(john);
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployees);
Mockito.when(employeeRepository.findById(-99L)).thenReturn(null);
}
@Test
@@ -116,26 +110,21 @@ public class EmployeeServiceImplIntegrationTest {
List<Employee> allEmployees = employeeService.getAllEmployees();
verifyFindAllEmployeesIsCalledOnce();
assertThat(allEmployees).hasSize(3)
.extracting(Employee::getName)
.contains(alex.getName(), john.getName(), bob.getName());
assertThat(allEmployees).hasSize(3).extracting(Employee::getName).contains(alex.getName(), john.getName(), bob.getName());
}
private void verifyFindByNameIsCalledOnce(String name) {
Mockito.verify(employeeRepository, VerificationModeFactory.times(1))
.findByName(name);
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findByName(name);
Mockito.reset(employeeRepository);
}
private void verifyFindByIdIsCalledOnce() {
Mockito.verify(employeeRepository, VerificationModeFactory.times(1))
.findById(Mockito.anyLong());
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());
Mockito.reset(employeeRepository);
}
private void verifyFindAllEmployeesIsCalledOnce() {
Mockito.verify(employeeRepository, VerificationModeFactory.times(1))
.findAll();
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findAll();
Mockito.reset(employeeRepository);
}
}
@@ -23,30 +23,21 @@ public class ConfigPropertiesIntegrationTest {
@Test
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients()
.size() == 2);
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients()
.size() == 2);
Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2);
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2);
}
@Test
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null);
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders()
.size() == 3);
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3);
}
@Test
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null);
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials()
.getAuthMethod()
.equals("SHA1"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials()
.getUsername()
.equals("john"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials()
.getPassword()
.equals("password"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password"));
}
}
@@ -47,8 +47,7 @@ public class UserRepositoryIntegrationTest {
Optional<User> foundUser = userRepository.findOneByName(USER_NAME_ADAM);
assertThat(foundUser.isPresent(), equalTo(true));
assertThat(foundUser.get()
.getName(), equalTo(USER_NAME_ADAM));
assertThat(foundUser.get().getName(), equalTo(USER_NAME_ADAM));
}
@Test
@@ -84,8 +83,7 @@ public class UserRepositoryIntegrationTest {
CompletableFuture<User> userByStatus = userRepository.findOneByStatus(ACTIVE_STATUS);
assertThat(userByStatus.get()
.getName(), equalTo(USER_NAME_ADAM));
assertThat(userByStatus.get().getName(), equalTo(USER_NAME_ADAM));
}