Migrate from ExpectedException rule to AssertJ
Replace ExpectedException JUnit rules with AssertJ exception assertions. Closes gh-14336
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package sample.test.domain;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,6 +25,7 @@ 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;
|
||||
|
||||
/**
|
||||
* Data JPA tests for {@link User}.
|
||||
@@ -40,31 +39,25 @@ public class UserEntityTests {
|
||||
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
|
||||
"00000000000000000");
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
private TestEntityManager entityManager;
|
||||
|
||||
@Test
|
||||
public void createWhenUsernameIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Username must not be empty");
|
||||
new User(null, VIN);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new User(null, VIN))
|
||||
.withMessage("Username must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenUsernameIsEmptyShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Username must not be empty");
|
||||
new User("", VIN);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new User("", VIN))
|
||||
.withMessage("Username must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenVinIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("VIN must not be null");
|
||||
new User("sboot", null);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new User("sboot", null))
|
||||
.withMessage("VIN must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,11 +16,10 @@
|
||||
|
||||
package sample.test.domain;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link VehicleIdentificationNumber}.
|
||||
@@ -34,28 +33,25 @@ public class VehicleIdentificationNumberTests {
|
||||
|
||||
private static final String SAMPLE_VIN = "41549485710496749";
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void createWhenVinIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("VIN must not be null");
|
||||
new VehicleIdentificationNumber(null);
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new VehicleIdentificationNumber(null))
|
||||
.withMessage("VIN must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenVinIsMoreThan17CharsShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("VIN must be exactly 17 characters");
|
||||
new VehicleIdentificationNumber("012345678901234567");
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new VehicleIdentificationNumber("012345678901234567"))
|
||||
.withMessage("VIN must be exactly 17 characters");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWhenVinIsLessThan17CharsShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("VIN must be exactly 17 characters");
|
||||
new VehicleIdentificationNumber("0123456789012345");
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new VehicleIdentificationNumber("0123456789012345"))
|
||||
.withMessage("VIN must be exactly 17 characters");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package sample.test.service;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import sample.test.domain.VehicleIdentificationNumber;
|
||||
|
||||
@@ -32,6 +30,8 @@ import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
|
||||
@@ -48,9 +48,6 @@ public class RemoteVehicleDetailsServiceTests {
|
||||
|
||||
private static final String VIN = "00000000000000000";
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
private RemoteVehicleDetailsService service;
|
||||
|
||||
@@ -59,9 +56,9 @@ public class RemoteVehicleDetailsServiceTests {
|
||||
|
||||
@Test
|
||||
public void getVehicleDetailsWhenVinIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("VIN must not be null");
|
||||
this.service.getVehicleDetails(null);
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> this.service.getVehicleDetails(null))
|
||||
.withMessage("VIN must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,16 +76,18 @@ public class RemoteVehicleDetailsServiceTests {
|
||||
public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException() {
|
||||
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
|
||||
.andRespond(withStatus(HttpStatus.NOT_FOUND));
|
||||
this.thrown.expect(VehicleIdentificationNumberNotFoundException.class);
|
||||
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
|
||||
assertThatExceptionOfType(VehicleIdentificationNumberNotFoundException.class)
|
||||
.isThrownBy(() -> this.service
|
||||
.getVehicleDetails(new VehicleIdentificationNumber(VIN)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVehicleDetailsWhenResultIServerErrorShouldThrowException() {
|
||||
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
|
||||
.andRespond(withServerError());
|
||||
this.thrown.expect(HttpServerErrorException.class);
|
||||
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
|
||||
assertThatExceptionOfType(HttpServerErrorException.class)
|
||||
.isThrownBy(() -> this.service
|
||||
.getVehicleDetails(new VehicleIdentificationNumber(VIN)));
|
||||
}
|
||||
|
||||
private ClassPathResource getClassPathResource(String path) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,9 +17,7 @@
|
||||
package sample.test.web;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import sample.test.domain.User;
|
||||
@@ -29,6 +27,8 @@ import sample.test.service.VehicleDetails;
|
||||
import sample.test.service.VehicleDetailsService;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@@ -42,9 +42,6 @@ public class UserVehicleServiceTests {
|
||||
private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
|
||||
"00000000000000000");
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Mock
|
||||
private VehicleDetailsService vehicleDetailsService;
|
||||
|
||||
@@ -62,16 +59,16 @@ public class UserVehicleServiceTests {
|
||||
|
||||
@Test
|
||||
public void getVehicleDetailsWhenUsernameIsNullShouldThrowException() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Username must not be null");
|
||||
this.service.getVehicleDetails(null);
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> this.service.getVehicleDetails(null))
|
||||
.withMessage("Username must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVehicleDetailsWhenUsernameNotFoundShouldThrowException() {
|
||||
given(this.userRepository.findByUsername(anyString())).willReturn(null);
|
||||
this.thrown.expect(UserNameNotFoundException.class);
|
||||
this.service.getVehicleDetails("sboot");
|
||||
assertThatExceptionOfType(UserNameNotFoundException.class)
|
||||
.isThrownBy(() -> this.service.getVehicleDetails("sboot"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user