Merge branch '2.1.x'

Closes gh-17079
This commit is contained in:
Andy Wilkinson
2019-06-07 11:00:44 +01:00
2799 changed files with 28402 additions and 47836 deletions

View File

@@ -40,8 +40,7 @@ import static org.mockito.BDDMockito.given;
@AutoConfigureTestDatabase
class SampleTestApplicationWebIntegrationTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"01234567890123456");
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber("01234567890123456");
@Autowired
private TestRestTemplate restTemplate;
@@ -51,8 +50,7 @@ class SampleTestApplicationWebIntegrationTests {
@BeforeEach
public void setup() {
given(this.vehicleDetailsService.getVehicleDetails(VIN))
.willReturn(new VehicleDetails("Honda", "Civic"));
given(this.vehicleDetailsService.getVehicleDetails(VIN)).willReturn(new VehicleDetails("Honda", "Civic"));
}
@Test

View File

@@ -33,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
@DataJpaTest
class UserEntityTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber("00000000000000000");
@Autowired
private TestEntityManager entityManager;

View File

@@ -32,8 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
class UserRepositoryTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber("00000000000000000");
@Autowired
private TestEntityManager entityManager;

View File

@@ -35,22 +35,19 @@ class VehicleIdentificationNumberTests {
@Test
void createWhenVinIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new VehicleIdentificationNumber(null))
assertThatIllegalArgumentException().isThrownBy(() -> new VehicleIdentificationNumber(null))
.withMessage("VIN must not be null");
}
@Test
void createWhenVinIsMoreThan17CharsShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new VehicleIdentificationNumber("012345678901234567"))
assertThatIllegalArgumentException().isThrownBy(() -> new VehicleIdentificationNumber("012345678901234567"))
.withMessage("VIN must be exactly 17 characters");
}
@Test
void createWhenVinIsLessThan17CharsShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new VehicleIdentificationNumber("0123456789012345"))
assertThatIllegalArgumentException().isThrownBy(() -> new VehicleIdentificationNumber("0123456789012345"))
.withMessage("VIN must be exactly 17 characters");
}
@@ -64,8 +61,7 @@ class VehicleIdentificationNumberTests {
void equalsAndHashCodeShouldBeBasedOnVin() {
VehicleIdentificationNumber vin1 = new VehicleIdentificationNumber(SAMPLE_VIN);
VehicleIdentificationNumber vin2 = new VehicleIdentificationNumber(SAMPLE_VIN);
VehicleIdentificationNumber vin3 = new VehicleIdentificationNumber(
"00000000000000000");
VehicleIdentificationNumber vin3 = new VehicleIdentificationNumber("00000000000000000");
assertThat(vin1.hashCode()).isEqualTo(vin2.hashCode());
assertThat(vin1).isEqualTo(vin1).isEqualTo(vin2).isNotEqualTo(vin3);
}

View File

@@ -53,38 +53,31 @@ class RemoteVehicleDetailsServiceTests {
@Test
void getVehicleDetailsWhenVinIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.service.getVehicleDetails(null))
assertThatIllegalArgumentException().isThrownBy(() -> this.service.getVehicleDetails(null))
.withMessage("VIN must not be null");
}
@Test
void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails() {
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withSuccess(getClassPathResource("vehicledetails.json"),
MediaType.APPLICATION_JSON));
VehicleDetails details = this.service
.getVehicleDetails(new VehicleIdentificationNumber(VIN));
.andRespond(withSuccess(getClassPathResource("vehicledetails.json"), MediaType.APPLICATION_JSON));
VehicleDetails details = this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
assertThat(details.getMake()).isEqualTo("Honda");
assertThat(details.getModel()).isEqualTo("Civic");
}
@Test
void getVehicleDetailsWhenResultIsNotFoundShouldThrowException() {
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withStatus(HttpStatus.NOT_FOUND));
this.server.expect(requestTo("/vehicle/" + VIN + "/details")).andRespond(withStatus(HttpStatus.NOT_FOUND));
assertThatExceptionOfType(VehicleIdentificationNumberNotFoundException.class)
.isThrownBy(() -> this.service
.getVehicleDetails(new VehicleIdentificationNumber(VIN)));
.isThrownBy(() -> this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN)));
}
@Test
void getVehicleDetailsWhenResultIServerErrorShouldThrowException() {
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withServerError());
this.server.expect(requestTo("/vehicle/" + VIN + "/details")).andRespond(withServerError());
assertThatExceptionOfType(HttpServerErrorException.class)
.isThrownBy(() -> this.service
.getVehicleDetails(new VehicleIdentificationNumber(VIN)));
.isThrownBy(() -> this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN)));
}
private ClassPathResource getClassPathResource(String path) {

View File

@@ -41,15 +41,13 @@ class VehicleDetailsJsonTests {
assertThat(this.json.write(details)).isEqualTo("vehicledetails.json");
assertThat(this.json.write(details)).isEqualToJson("vehicledetails.json");
assertThat(this.json.write(details)).hasJsonPathStringValue("@.make");
assertThat(this.json.write(details)).extractingJsonPathStringValue("@.make")
.isEqualTo("Honda");
assertThat(this.json.write(details)).extractingJsonPathStringValue("@.make").isEqualTo("Honda");
}
@Test
void deserializeJson() throws Exception {
String content = "{\"make\":\"Ford\",\"model\":\"Focus\"}";
assertThat(this.json.parse(content))
.isEqualTo(new VehicleDetails("Ford", "Focus"));
assertThat(this.json.parse(content)).isEqualTo(new VehicleDetails("Ford", "Focus"));
assertThat(this.json.parseObject(content).getMake()).isEqualTo("Ford");
}

View File

@@ -56,17 +56,15 @@ class UserVehicleControllerApplicationTests {
@Test
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk()).andExpect(content().string("Honda Civic"));
given(this.userVehicleService.getVehicleDetails("sboot")).willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk())
.andExpect(content().string("Honda Civic"));
}
@Test
void welcomeCommandLineRunnerShouldBeAvailable() {
// Since we're a @SpringBootTest all beans should be available.
assertThat(this.applicationContext.getBean(WelcomeCommandLineRunner.class))
.isNotNull();
assertThat(this.applicationContext.getBean(WelcomeCommandLineRunner.class)).isNotNull();
}
}

View File

@@ -44,8 +44,7 @@ class UserVehicleControllerHtmlUnitTests {
@Test
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
given(this.userVehicleService.getVehicleDetails("sboot")).willReturn(new VehicleDetails("Honda", "Civic"));
HtmlPage page = this.webClient.getPage("/sboot/vehicle.html");
assertThat(page.getBody().getTextContent()).isEqualTo("Honda Civic");
}

View File

@@ -45,8 +45,7 @@ class UserVehicleControllerSeleniumTests {
@Test
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
given(this.userVehicleService.getVehicleDetails("sboot")).willReturn(new VehicleDetails("Honda", "Civic"));
this.webDriver.get("/sboot/vehicle.html");
WebElement element = this.webDriver.findElement(By.tagName("h1"));
assertThat(element.getText()).isEqualTo("Honda Civic");

View File

@@ -45,8 +45,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(UserVehicleController.class)
class UserVehicleControllerTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber("00000000000000000");
@Autowired
private MockMvc mvc;
@@ -59,34 +58,28 @@ class UserVehicleControllerTests {
@Test
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk()).andExpect(content().string("Honda Civic"));
given(this.userVehicleService.getVehicleDetails("sboot")).willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk())
.andExpect(content().string("Honda Civic"));
}
@Test
void getVehicleWhenRequestingJsonShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
given(this.userVehicleService.getVehicleDetails("sboot")).willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(content().json("{'make':'Honda','model':'Civic'}"));
}
@Test
void getVehicleWhenRequestingHtmlShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle.html").accept(MediaType.TEXT_HTML))
.andExpect(status().isOk())
given(this.userVehicleService.getVehicleDetails("sboot")).willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle.html").accept(MediaType.TEXT_HTML)).andExpect(status().isOk())
.andExpect(content().string(containsString("<h1>Honda Civic</h1>")));
}
@Test
void getVehicleWhenUserNotFoundShouldReturnNotFound() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willThrow(new UserNameNotFoundException("sboot"));
given(this.userVehicleService.getVehicleDetails("sboot")).willThrow(new UserNameNotFoundException("sboot"));
this.mvc.perform(get("/sboot/vehicle")).andExpect(status().isNotFound());
}
@@ -100,8 +93,7 @@ class UserVehicleControllerTests {
@Test
void welcomeCommandLineRunnerShouldBeAvailable() {
// Since we're a @WebMvcTest WelcomeCommandLineRunner should not be available.
Assertions.assertThatThrownBy(
() -> this.applicationContext.getBean(WelcomeCommandLineRunner.class))
Assertions.assertThatThrownBy(() -> this.applicationContext.getBean(WelcomeCommandLineRunner.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
}

View File

@@ -39,8 +39,7 @@ import static org.mockito.BDDMockito.given;
*/
class UserVehicleServiceTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber("00000000000000000");
@Mock
private VehicleDetailsService vehicleDetailsService;
@@ -53,14 +52,12 @@ class UserVehicleServiceTests {
@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
this.service = new UserVehicleService(this.userRepository,
this.vehicleDetailsService);
this.service = new UserVehicleService(this.userRepository, this.vehicleDetailsService);
}
@Test
void getVehicleDetailsWhenUsernameIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.service.getVehicleDetails(null))
assertThatIllegalArgumentException().isThrownBy(() -> this.service.getVehicleDetails(null))
.withMessage("Username must not be null");
}
@@ -73,8 +70,7 @@ class UserVehicleServiceTests {
@Test
void getVehicleDetailsShouldReturnMakeAndModel() {
given(this.userRepository.findByUsername(anyString()))
.willReturn(new User("sboot", VIN));
given(this.userRepository.findByUsername(anyString())).willReturn(new User("sboot", VIN));
VehicleDetails details = new VehicleDetails("Honda", "Civic");
given(this.vehicleDetailsService.getVehicleDetails(VIN)).willReturn(details);
VehicleDetails actual = this.service.getVehicleDetails("sboot");