DATACMNS-1554 - Polishing.

Replace AtTest(expected = …) and ExpectedException with the corresponding AssertJ assertThatExceptionOfType(…) and assertThatIllegalArgumentException().isThrownBy(…).
This commit is contained in:
Mark Paluch
2019-07-10 11:47:46 +02:00
parent 660006b8c9
commit 704913c866
79 changed files with 457 additions and 474 deletions

View File

@@ -30,9 +30,9 @@ import org.springframework.util.ReflectionUtils;
*/
public class AnnotationDetectionFieldCallbackUnitTests {
@Test(expected = IllegalArgumentException.class) // DATACMNS-616
@Test // DATACMNS-616
public void rejectsNullAnnotationType() {
new AnnotationDetectionFieldCallback(null);
assertThatIllegalArgumentException().isThrownBy(() -> new AnnotationDetectionFieldCallback(null));
}
@Test // DATACMNS-616

View File

@@ -17,9 +17,8 @@ package org.springframework.data.util;
import static org.assertj.core.api.Assertions.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ReflectionUtils;
@@ -27,11 +26,10 @@ import org.springframework.util.ReflectionUtils;
* Unit tests for {@link AnnotationDetectionMethodCallback}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class AnnotationDetectionMethodCallbackUnitTests {
public @Rule ExpectedException exception = ExpectedException.none();
@Test // DATACMNS-452
public void findsMethodWithAnnotation() throws Exception {
@@ -47,13 +45,13 @@ public class AnnotationDetectionMethodCallbackUnitTests {
@Test // DATACMNS-452
public void detectsAmbiguousAnnotations() {
exception.expect(IllegalStateException.class);
exception.expectMessage("Value");
exception.expectMessage("getValue");
exception.expectMessage("getOtherValue");
AnnotationDetectionMethodCallback<Value> callback = new AnnotationDetectionMethodCallback<>(Value.class, true);
ReflectionUtils.doWithMethods(Multiple.class, callback);
assertThatIllegalStateException() //
.isThrownBy(() -> ReflectionUtils.doWithMethods(Multiple.class, callback)) //
.withMessageContaining("Value") //
.withMessageContaining("getValue") //
.withMessageContaining("getOtherValue");
}
interface Sample {

View File

@@ -42,7 +42,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void discoversTypeForSimpleGenericField() {
TypeInformation<ConcreteType> discoverer = ClassTypeInformation.from(ConcreteType.class);
TypeInformation<ConcreteType> discoverer = from(ConcreteType.class);
assertThat(discoverer.getType()).isEqualTo(ConcreteType.class);
@@ -56,7 +56,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void discoversTypeForNestedGenericField() {
TypeInformation<ConcreteWrapper> discoverer = ClassTypeInformation.from(ConcreteWrapper.class);
TypeInformation<ConcreteWrapper> discoverer = from(ConcreteWrapper.class);
assertThat(discoverer.getType()).isEqualTo(ConcreteWrapper.class);
assertThat(discoverer.getProperty("wrapped")).satisfies(it -> {
@@ -72,15 +72,14 @@ public class ClassTypeInformationUnitTests {
@SuppressWarnings("rawtypes")
public void discoversBoundType() {
TypeInformation<GenericTypeWithBound> information = ClassTypeInformation.from(GenericTypeWithBound.class);
TypeInformation<GenericTypeWithBound> information = from(GenericTypeWithBound.class);
assertThat(information.getProperty("person")).satisfies(it -> assertThat(it.getType()).isEqualTo(Person.class));
}
@Test
public void discoversBoundTypeForSpecialization() {
TypeInformation<SpecialGenericTypeWithBound> information = ClassTypeInformation
.from(SpecialGenericTypeWithBound.class);
TypeInformation<SpecialGenericTypeWithBound> information = from(SpecialGenericTypeWithBound.class);
assertThat(information.getProperty("person"))
.satisfies(it -> assertThat(it.getType()).isEqualTo(SpecialPerson.class));
}
@@ -89,7 +88,7 @@ public class ClassTypeInformationUnitTests {
@SuppressWarnings("rawtypes")
public void discoversBoundTypeForNested() {
TypeInformation<AnotherGenericType> information = ClassTypeInformation.from(AnotherGenericType.class);
TypeInformation<AnotherGenericType> information = from(AnotherGenericType.class);
assertThat(information.getProperty("nested"))
.satisfies(it -> assertThat(it.getType()).isEqualTo(GenericTypeWithBound.class));
@@ -100,7 +99,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void discoversArraysAndCollections() {
TypeInformation<StringCollectionContainer> information = ClassTypeInformation.from(StringCollectionContainer.class);
TypeInformation<StringCollectionContainer> information = from(StringCollectionContainer.class);
TypeInformation<?> array = information.getProperty("array");
@@ -126,7 +125,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void discoversMapValueType() {
TypeInformation<StringMapContainer> information = ClassTypeInformation.from(StringMapContainer.class);
TypeInformation<StringMapContainer> information = from(StringMapContainer.class);
TypeInformation<?> genericMap = information.getProperty("genericMap");
@@ -142,8 +141,8 @@ public class ClassTypeInformationUnitTests {
@Test
public void typeInfoDoesNotEqualForGenericTypesWithDifferentParent() {
TypeInformation<ConcreteWrapper> first = ClassTypeInformation.from(ConcreteWrapper.class);
TypeInformation<AnotherConcreteWrapper> second = ClassTypeInformation.from(AnotherConcreteWrapper.class);
TypeInformation<ConcreteWrapper> first = from(ConcreteWrapper.class);
TypeInformation<AnotherConcreteWrapper> second = from(AnotherConcreteWrapper.class);
assertThat(first.getProperty("wrapped").equals(second.getProperty("wrapped"))).isFalse();
}
@@ -151,7 +150,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void handlesPropertyFieldMismatchCorrectly() {
TypeInformation<PropertyGetter> from = ClassTypeInformation.from(PropertyGetter.class);
TypeInformation<PropertyGetter> from = from(PropertyGetter.class);
assertThat(from.getProperty("_name")).satisfies(it -> assertThat(it.getType()).isEqualTo(String.class));
assertThat(from.getProperty("name")).satisfies(it -> assertThat(it.getType()).isEqualTo(byte[].class));
@@ -160,14 +159,14 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-77
public void returnsSameInstanceForCachedClass() {
TypeInformation<PropertyGetter> info = ClassTypeInformation.from(PropertyGetter.class);
assertThat(ClassTypeInformation.from(PropertyGetter.class)).isSameAs(info);
TypeInformation<PropertyGetter> info = from(PropertyGetter.class);
assertThat(from(PropertyGetter.class)).isSameAs(info);
}
@Test // DATACMNS-39
public void resolvesWildCardTypeCorrectly() {
TypeInformation<ClassWithWildCardBound> information = ClassTypeInformation.from(ClassWithWildCardBound.class);
TypeInformation<ClassWithWildCardBound> information = from(ClassWithWildCardBound.class);
TypeInformation<?> wildcard = information.getProperty("wildcard");
@@ -186,7 +185,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void resolvesTypeParametersCorrectly() {
TypeInformation<ConcreteType> information = ClassTypeInformation.from(ConcreteType.class);
TypeInformation<ConcreteType> information = from(ConcreteType.class);
TypeInformation<?> superTypeInformation = information.getSuperTypeInformation(GenericType.class);
List<TypeInformation<?>> parameters = superTypeInformation.getTypeArguments();
@@ -198,7 +197,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void resolvesNestedInheritedTypeParameters() {
TypeInformation<SecondExtension> information = ClassTypeInformation.from(SecondExtension.class);
TypeInformation<SecondExtension> information = from(SecondExtension.class);
TypeInformation<?> superTypeInformation = information.getSuperTypeInformation(Base.class);
List<TypeInformation<?>> parameters = superTypeInformation.getTypeArguments();
@@ -209,7 +208,7 @@ public class ClassTypeInformationUnitTests {
@Test
public void discoveresMethodParameterTypesCorrectly() throws Exception {
TypeInformation<SecondExtension> information = ClassTypeInformation.from(SecondExtension.class);
TypeInformation<SecondExtension> information = from(SecondExtension.class);
Method method = SecondExtension.class.getMethod("foo", Base.class);
List<TypeInformation<?>> informations = information.getParameterTypes(method);
TypeInformation<?> returnTypeInformation = information.getReturnType(method);
@@ -281,9 +280,9 @@ public class ClassTypeInformationUnitTests {
assertThat(information.getProperty("category.id")).isEqualTo(from(Long.class));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-387
@Test // DATACMNS-387
public void rejectsNullClass() {
ClassTypeInformation.from(null);
assertThatIllegalArgumentException().isThrownBy(() -> ClassTypeInformation.from(null));
}
@Test // DATACMNS-422
@@ -296,7 +295,7 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-440
public void detectsSpecialMapAsMapValueType() {
TypeInformation<?> seriously = ClassTypeInformation.from(SuperGenerics.class).getProperty("seriously");
TypeInformation<?> seriously = from(SuperGenerics.class).getProperty("seriously");
// Type
assertThat(seriously.getType()).isEqualTo(SortedMap.class);
@@ -331,7 +330,7 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-594
public void considersGenericsOfTypeBounds() {
assertThat(ClassTypeInformation.from(ConcreteRootIntermediate.class)
assertThat(from(ConcreteRootIntermediate.class)
.getProperty("intermediate.content.intermediate.content").getType())//
.isEqualTo(Leaf.class);
}
@@ -339,13 +338,13 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-783, DATACMNS-853
public void specializesTypeUsingTypeVariableContext() {
ClassTypeInformation<Foo> root = ClassTypeInformation.from(Foo.class);
ClassTypeInformation<Foo> root = from(Foo.class);
assertThat(root.getProperty("abstractBar").specialize(ClassTypeInformation.from(Bar.class)))//
assertThat(root.getProperty("abstractBar").specialize(from(Bar.class)))//
.satisfies(it -> {
assertThat(it.getType()).isEqualTo(Bar.class);
assertThat(it.getProperty("field").getType()).isEqualTo(Character.class);
assertThat(it.getProperty("anotherField").getType()).isEqualTo(Integer.class);
assertThat(it.getProperty("field").getType()).isEqualTo(Character.class);
assertThat(it.getProperty("anotherField").getType()).isEqualTo(Integer.class);
});
}
@@ -376,7 +375,7 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-896
public void prefersLocalTypeMappingOverNestedWithSameGenericType() {
ClassTypeInformation<Concrete> information = ClassTypeInformation.from(Concrete.class);
ClassTypeInformation<Concrete> information = from(Concrete.class);
assertThat(information.getProperty("field").getType()).isEqualTo(Nested.class);
}
@@ -384,7 +383,7 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-940
public void detectsVavrTraversableComponentType() {
ClassTypeInformation<SampleTraversable> information = ClassTypeInformation.from(SampleTraversable.class);
ClassTypeInformation<SampleTraversable> information = from(SampleTraversable.class);
assertThat(information.getComponentType().getType()).isAssignableFrom(Integer.class);
}
@@ -392,7 +391,7 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-940
public void detectsVavrMapComponentAndValueType() {
ClassTypeInformation<SampleMap> information = ClassTypeInformation.from(SampleMap.class);
ClassTypeInformation<SampleMap> information = from(SampleMap.class);
assertThat(information.getComponentType().getType()).isAssignableFrom(String.class);
@@ -402,8 +401,8 @@ public class ClassTypeInformationUnitTests {
@Test // DATACMNS-1138
public void usesTargetTypeForWildcardedBaseOnSpecialization() {
ClassTypeInformation<WildcardedWrapper> wrapper = ClassTypeInformation.from(WildcardedWrapper.class);
ClassTypeInformation<SomeConcrete> concrete = ClassTypeInformation.from(SomeConcrete.class);
ClassTypeInformation<WildcardedWrapper> wrapper = from(WildcardedWrapper.class);
ClassTypeInformation<SomeConcrete> concrete = from(SomeConcrete.class);
TypeInformation<?> property = wrapper.getRequiredProperty("wildcarded");

View File

@@ -51,18 +51,22 @@ public class DirectFieldAccessFallbackBeanWrapperUnitTests {
assertThat(sample.firstname).isEqualTo("Dave");
}
@Test(expected = NotReadablePropertyException.class) // DATACMNS-452
@Test // DATACMNS-452
public void throwsAppropriateExceptionIfNoFieldFoundForRead() {
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(new Sample());
wrapper.getPropertyValue("lastname");
assertThatExceptionOfType(NotReadablePropertyException.class)
.isThrownBy(() -> wrapper.getPropertyValue("lastname"));
}
@Test(expected = NotWritablePropertyException.class) // DATACMNS-452
@Test // DATACMNS-452
public void throwsAppropriateExceptionIfNoFieldFoundForWrite() {
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(new Sample());
wrapper.setPropertyValue("lastname", "Matthews");
assertThatExceptionOfType(NotWritablePropertyException.class)
.isThrownBy(() -> wrapper.setPropertyValue("lastname", "Matthews"));
}
static class Sample {

View File

@@ -35,14 +35,14 @@ public class PairUnitTests {
assertThat(pair.getSecond()).isEqualTo(2);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-790
@Test // DATACMNS-790
public void rejectsNullFirstElement() {
Pair.of(null, 1);
assertThatIllegalArgumentException().isThrownBy(() -> Pair.of(null, 1));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-790
@Test // DATACMNS-790
public void rejectsNullSecondElement() {
Pair.of(1, null);
assertThatIllegalArgumentException().isThrownBy(() -> Pair.of(1, null));
}
@Test // DATACMNS-790

View File

@@ -60,9 +60,10 @@ public class ReflectionUtilsUnitTests {
assertThat(field).isNull();
}
@Test(expected = IllegalStateException.class)
@Test
public void rejectsNonUniqueField() {
ReflectionUtils.findField(Sample.class, new ReflectionUtils.AnnotationFieldFilter(Autowired.class));
assertThatIllegalStateException().isThrownBy(
() -> ReflectionUtils.findField(Sample.class, new ReflectionUtils.AnnotationFieldFilter(Autowired.class)));
}
@Test

View File

@@ -47,9 +47,9 @@ public class TypeDiscovererUnitTests {
@Mock Map<TypeVariable<?>, Type> firstMap;
@Mock Map<TypeVariable<?>, Type> secondMap;
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullType() {
new TypeDiscoverer<>(null, null);
assertThatIllegalArgumentException().isThrownBy(() -> new TypeDiscoverer<>(null, null));
}
@Test