Validate our own tests work with JUnit5 and the vintage engine

Closes gh-14737

Co-authored-by: Stephane Nicoll <snicoll@pivotal.io>
This commit is contained in:
Madhura Bhave
2018-12-05 13:56:29 -08:00
committed by Stephane Nicoll
parent d9f339a1b6
commit 1db1c8b03c
821 changed files with 2279 additions and 2787 deletions

View File

@@ -16,9 +16,8 @@
package sample.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import sample.test.domain.VehicleIdentificationNumber;
import sample.test.service.VehicleDetails;
import sample.test.service.VehicleDetailsService;
@@ -29,7 +28,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.mockito.BDDMockito.given;
@@ -38,10 +36,9 @@ import static org.mockito.BDDMockito.given;
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase
public class SampleTestApplicationWebIntegrationTests {
class SampleTestApplicationWebIntegrationTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"01234567890123456");
@@ -52,14 +49,14 @@ public class SampleTestApplicationWebIntegrationTests {
@MockBean
private VehicleDetailsService vehicleDetailsService;
@Before
@BeforeEach
public void setup() {
given(this.vehicleDetailsService.getVehicleDetails(VIN))
.willReturn(new VehicleDetails("Honda", "Civic"));
}
@Test
public void test() {
void test() {
this.restTemplate.getForEntity("/{username}/vehicle", String.class, "sframework");
}

View File

@@ -16,13 +16,11 @@
package sample.test.domain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -32,9 +30,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@DataJpaTest
public class UserEntityTests {
class UserEntityTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
@@ -43,25 +40,25 @@ public class UserEntityTests {
private TestEntityManager entityManager;
@Test
public void createWhenUsernameIsNullShouldThrowException() {
void createWhenUsernameIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new User(null, VIN))
.withMessage("Username must not be empty");
}
@Test
public void createWhenUsernameIsEmptyShouldThrowException() {
void createWhenUsernameIsEmptyShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new User("", VIN))
.withMessage("Username must not be empty");
}
@Test
public void createWhenVinIsNullShouldThrowException() {
void createWhenVinIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new User("sboot", null))
.withMessage("VIN must not be null");
}
@Test
public void saveShouldPersistData() {
void saveShouldPersistData() {
User user = this.entityManager.persistFlushFind(new User("sboot", VIN));
assertThat(user.getUsername()).isEqualTo("sboot");
assertThat(user.getVin()).isEqualTo(VIN);

View File

@@ -16,13 +16,11 @@
package sample.test.domain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,9 +29,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@DataJpaTest
public class UserRepositoryTests {
class UserRepositoryTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
@@ -45,7 +42,7 @@ public class UserRepositoryTests {
private UserRepository repository;
@Test
public void findByUsernameShouldReturnUser() {
void findByUsernameShouldReturnUser() {
this.entityManager.persist(new User("sboot", VIN));
User user = this.repository.findByUsername("sboot");
assertThat(user.getUsername()).isEqualTo("sboot");
@@ -53,7 +50,7 @@ public class UserRepositoryTests {
}
@Test
public void findByUsernameWhenNoUserShouldReturnNull() {
void findByUsernameWhenNoUserShouldReturnNull() {
this.entityManager.persist(new User("sboot", VIN));
User user = this.repository.findByUsername("mmouse");
assertThat(user).isNull();

View File

@@ -16,7 +16,7 @@
package sample.test.domain;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -29,39 +29,39 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* Naming standards for unit tests</a>
* @see <a href="https://joel-costigliola.github.io/assertj/">AssertJ</a>
*/
public class VehicleIdentificationNumberTests {
class VehicleIdentificationNumberTests {
private static final String SAMPLE_VIN = "41549485710496749";
@Test
public void createWhenVinIsNullShouldThrowException() {
void createWhenVinIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new VehicleIdentificationNumber(null))
.withMessage("VIN must not be null");
}
@Test
public void createWhenVinIsMoreThan17CharsShouldThrowException() {
void createWhenVinIsMoreThan17CharsShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new VehicleIdentificationNumber("012345678901234567"))
.withMessage("VIN must be exactly 17 characters");
}
@Test
public void createWhenVinIsLessThan17CharsShouldThrowException() {
void createWhenVinIsLessThan17CharsShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new VehicleIdentificationNumber("0123456789012345"))
.withMessage("VIN must be exactly 17 characters");
}
@Test
public void toStringShouldReturnVin() {
void toStringShouldReturnVin() {
VehicleIdentificationNumber vin = new VehicleIdentificationNumber(SAMPLE_VIN);
assertThat(vin.toString()).isEqualTo(SAMPLE_VIN);
}
@Test
public void equalsAndHashCodeShouldBeBasedOnVin() {
void equalsAndHashCodeShouldBeBasedOnVin() {
VehicleIdentificationNumber vin1 = new VehicleIdentificationNumber(SAMPLE_VIN);
VehicleIdentificationNumber vin2 = new VehicleIdentificationNumber(SAMPLE_VIN);
VehicleIdentificationNumber vin3 = new VehicleIdentificationNumber(

View File

@@ -16,8 +16,7 @@
package sample.test.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import sample.test.domain.VehicleIdentificationNumber;
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,7 +24,6 @@ import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.HttpServerErrorException;
@@ -42,9 +40,8 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@RestClientTest({ RemoteVehicleDetailsService.class, ServiceProperties.class })
public class RemoteVehicleDetailsServiceTests {
class RemoteVehicleDetailsServiceTests {
private static final String VIN = "00000000000000000";
@@ -55,14 +52,14 @@ public class RemoteVehicleDetailsServiceTests {
private MockRestServiceServer server;
@Test
public void getVehicleDetailsWhenVinIsNullShouldThrowException() {
void getVehicleDetailsWhenVinIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.service.getVehicleDetails(null))
.withMessage("VIN must not be null");
}
@Test
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails() {
void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails() {
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withSuccess(getClassPathResource("vehicledetails.json"),
MediaType.APPLICATION_JSON));
@@ -73,7 +70,7 @@ public class RemoteVehicleDetailsServiceTests {
}
@Test
public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException() {
void getVehicleDetailsWhenResultIsNotFoundShouldThrowException() {
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withStatus(HttpStatus.NOT_FOUND));
assertThatExceptionOfType(VehicleIdentificationNumberNotFoundException.class)
@@ -82,7 +79,7 @@ public class RemoteVehicleDetailsServiceTests {
}
@Test
public void getVehicleDetailsWhenResultIServerErrorShouldThrowException() {
void getVehicleDetailsWhenResultIServerErrorShouldThrowException() {
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withServerError());
assertThatExceptionOfType(HttpServerErrorException.class)

View File

@@ -16,13 +16,11 @@
package sample.test.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,15 +29,14 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@JsonTest
public class VehicleDetailsJsonTests {
class VehicleDetailsJsonTests {
@Autowired
private JacksonTester<VehicleDetails> json;
@Test
public void serializeJson() throws Exception {
void serializeJson() throws Exception {
VehicleDetails details = new VehicleDetails("Honda", "Civic");
assertThat(this.json.write(details)).isEqualTo("vehicledetails.json");
assertThat(this.json.write(details)).isEqualToJson("vehicledetails.json");
@@ -49,7 +46,7 @@ public class VehicleDetailsJsonTests {
}
@Test
public void deserializeJson() throws Exception {
void deserializeJson() throws Exception {
String content = "{\"make\":\"Ford\",\"model\":\"Focus\"}";
assertThat(this.json.parse(content))
.isEqualTo(new VehicleDetails("Ford", "Focus"));

View File

@@ -16,8 +16,7 @@
package sample.test.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import sample.test.WelcomeCommandLineRunner;
import sample.test.service.VehicleDetails;
@@ -28,7 +27,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat;
@@ -42,11 +40,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureTestDatabase
public class UserVehicleControllerApplicationTests {
class UserVehicleControllerApplicationTests {
@Autowired
private MockMvc mvc;
@@ -58,7 +55,7 @@ public class UserVehicleControllerApplicationTests {
private UserVehicleService userVehicleService;
@Test
public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
@@ -66,7 +63,7 @@ public class UserVehicleControllerApplicationTests {
}
@Test
public void welcomeCommandLineRunnerShouldBeAvailable() {
void welcomeCommandLineRunnerShouldBeAvailable() {
// Since we're a @SpringBootTest all beans should be available.
assertThat(this.applicationContext.getBean(WelcomeCommandLineRunner.class))
.isNotNull();

View File

@@ -18,14 +18,12 @@ package sample.test.web;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import sample.test.service.VehicleDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@@ -35,9 +33,8 @@ import static org.mockito.BDDMockito.given;
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(UserVehicleController.class)
public class UserVehicleControllerHtmlUnitTests {
class UserVehicleControllerHtmlUnitTests {
@Autowired
private WebClient webClient;
@@ -46,7 +43,7 @@ public class UserVehicleControllerHtmlUnitTests {
private UserVehicleService userVehicleService;
@Test
public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
HtmlPage page = this.webClient.getPage("/sboot/vehicle.html");

View File

@@ -16,8 +16,7 @@
package sample.test.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
@@ -26,7 +25,6 @@ import sample.test.service.VehicleDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@@ -36,9 +34,8 @@ import static org.mockito.BDDMockito.given;
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(UserVehicleController.class)
public class UserVehicleControllerSeleniumTests {
class UserVehicleControllerSeleniumTests {
@Autowired
private WebDriver webDriver;
@@ -47,7 +44,7 @@ public class UserVehicleControllerSeleniumTests {
private UserVehicleService userVehicleService;
@Test
public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() {
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.webDriver.get("/sboot/vehicle.html");

View File

@@ -17,8 +17,7 @@
package sample.test.web;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import sample.test.WelcomeCommandLineRunner;
import sample.test.domain.VehicleIdentificationNumber;
import sample.test.service.VehicleDetails;
@@ -30,7 +29,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
@@ -44,9 +42,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(UserVehicleController.class)
public class UserVehicleControllerTests {
class UserVehicleControllerTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
@@ -61,7 +58,7 @@ public class UserVehicleControllerTests {
private UserVehicleService userVehicleService;
@Test
public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
@@ -69,7 +66,7 @@ public class UserVehicleControllerTests {
}
@Test
public void getVehicleWhenRequestingJsonShouldReturnMakeAndModel() throws Exception {
void getVehicleWhenRequestingJsonShouldReturnMakeAndModel() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willReturn(new VehicleDetails("Honda", "Civic"));
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.APPLICATION_JSON))
@@ -78,7 +75,7 @@ public class UserVehicleControllerTests {
}
@Test
public void getVehicleWhenRequestingHtmlShouldReturnMakeAndModel() throws Exception {
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))
@@ -87,21 +84,21 @@ public class UserVehicleControllerTests {
}
@Test
public void getVehicleWhenUserNotFoundShouldReturnNotFound() throws Exception {
void getVehicleWhenUserNotFoundShouldReturnNotFound() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willThrow(new UserNameNotFoundException("sboot"));
this.mvc.perform(get("/sboot/vehicle")).andExpect(status().isNotFound());
}
@Test
public void getVehicleWhenVinNotFoundShouldReturnNotFound() throws Exception {
void getVehicleWhenVinNotFoundShouldReturnNotFound() throws Exception {
given(this.userVehicleService.getVehicleDetails("sboot"))
.willThrow(new VehicleIdentificationNumberNotFoundException(VIN));
this.mvc.perform(get("/sboot/vehicle")).andExpect(status().isNotFound());
}
@Test
public void welcomeCommandLineRunnerShouldBeAvailable() {
void welcomeCommandLineRunnerShouldBeAvailable() {
// Since we're a @WebMvcTest WelcomeCommandLineRunner should not be available.
Assertions.assertThatThrownBy(
() -> this.applicationContext.getBean(WelcomeCommandLineRunner.class))

View File

@@ -16,8 +16,8 @@
package sample.test.web;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import sample.test.domain.User;
@@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.given;
*
* @author Phillip Webb
*/
public class UserVehicleServiceTests {
class UserVehicleServiceTests {
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
"00000000000000000");
@@ -50,7 +50,7 @@ public class UserVehicleServiceTests {
private UserVehicleService service;
@Before
@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
this.service = new UserVehicleService(this.userRepository,
@@ -58,21 +58,21 @@ public class UserVehicleServiceTests {
}
@Test
public void getVehicleDetailsWhenUsernameIsNullShouldThrowException() {
void getVehicleDetailsWhenUsernameIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.service.getVehicleDetails(null))
.withMessage("Username must not be null");
}
@Test
public void getVehicleDetailsWhenUsernameNotFoundShouldThrowException() {
void getVehicleDetailsWhenUsernameNotFoundShouldThrowException() {
given(this.userRepository.findByUsername(anyString())).willReturn(null);
assertThatExceptionOfType(UserNameNotFoundException.class)
.isThrownBy(() -> this.service.getVehicleDetails("sboot"));
}
@Test
public void getVehicleDetailsShouldReturnMakeAndModel() {
void getVehicleDetailsShouldReturnMakeAndModel() {
given(this.userRepository.findByUsername(anyString()))
.willReturn(new User("sboot", VIN));
VehicleDetails details = new VehicleDetails("Honda", "Civic");