From 8fd3019c124ff5f32346bb68f243cb29057fd786 Mon Sep 17 00:00:00 2001 From: arnaudlabourel <arnaud.labourel@univ-amu.fr> Date: Tue, 21 Sep 2021 10:30:58 +0200 Subject: [PATCH] Test assertJ --- build.gradle | 6 +- src/test/java/mandelbrot/ComplexTest.java | 125 +++++++++++----------- 2 files changed, 65 insertions(+), 66 deletions(-) diff --git a/build.gradle b/build.gradle index 0056f70..4e8566d 100644 --- a/build.gradle +++ b/build.gradle @@ -13,9 +13,9 @@ repositories { } dependencies { - testImplementation('org.junit.jupiter:junit-jupiter-api:5.7.2', - 'org.hamcrest:hamcrest-library:2.2', 'net.obvj:junit-utils:1.3.1') - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' + testImplementation('org.junit.jupiter:junit-jupiter-api:5.8.0', + "org.assertj:assertj-core:3.21.0", 'net.obvj:junit-utils:1.3.1') + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.0' } test { diff --git a/src/test/java/mandelbrot/ComplexTest.java b/src/test/java/mandelbrot/ComplexTest.java index 7a18886..4d7c585 100644 --- a/src/test/java/mandelbrot/ComplexTest.java +++ b/src/test/java/mandelbrot/ComplexTest.java @@ -1,11 +1,8 @@ package mandelbrot; -import static net.obvj.junit.utils.matchers.AdvancedMatchers.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; +import static org.assertj.core.api.Assertions.*; public class ComplexTest { private Complex onePlusI; @@ -33,147 +30,149 @@ public class ComplexTest { @Test void testEquals(){ - assertThat(onePlusI, is(equalTo(onePlusI))); - assertThat(onePlusI, is(equalTo(new Complex(1, 1)))); - assertThat(two, is(not(equalTo(twoI)))); + assertThat(onePlusI).isEqualTo(onePlusI); + assertThat(onePlusI).isEqualTo(new Complex(1, 1)); + assertThat(two).isNotEqualTo(twoI); } @Test void testGetReal(){ - assertThat(two.getReal(), is(closeTo(2., Helpers.EPSILON))); - assertThat(onePlusI.getReal(), is(closeTo(1., Helpers.EPSILON))); - assertThat(oneMinusI.getReal(), is(closeTo(1., Helpers.EPSILON))); + assertThat(two.getReal()).isCloseTo(2., within(Helpers.EPSILON)); + assertThat(onePlusI.getReal()).isCloseTo(1., within(Helpers.EPSILON)); + assertThat(oneMinusI.getReal()).isCloseTo(1., within(Helpers.EPSILON)); } @Test void testGetImaginary(){ - assertThat(two.getImaginary(), is(closeTo(0., Helpers.EPSILON))); - assertThat(onePlusI.getImaginary(), is(closeTo(1., Helpers.EPSILON))); - assertThat(oneMinusI.getImaginary(), is(closeTo(-1., Helpers.EPSILON))); + assertThat(two.getImaginary()).isCloseTo(0., within(Helpers.EPSILON)); + assertThat(onePlusI.getImaginary()).isCloseTo(1., within(Helpers.EPSILON)); + assertThat(oneMinusI.getImaginary()).isCloseTo(-1., within(Helpers.EPSILON)); } + @Test void testOne(){ - assertThat(Complex.ONE.getReal(), is(closeTo(1., Helpers.EPSILON))); - assertThat(Complex.ONE.getImaginary(), is(closeTo(0., Helpers.EPSILON))); + assertThat(Complex.ONE.getReal()).isCloseTo(1., within(Helpers.EPSILON)); + assertThat(Complex.ONE.getImaginary()).isCloseTo(0., within(Helpers.EPSILON)); } + @Test void testI(){ - assertThat(Complex.I.getReal(), is(closeTo(0., Helpers.EPSILON))); - assertThat(Complex.I.getImaginary(), is(closeTo(1., Helpers.EPSILON))); + assertThat(Complex.I.getReal()).isCloseTo(0., within(Helpers.EPSILON)); + assertThat(Complex.I.getImaginary()).isCloseTo(1., within(Helpers.EPSILON)); } @Test void testZero(){ - assertThat(Complex.ZERO.getReal(), is(closeTo(0., Helpers.EPSILON))); - assertThat(Complex.ZERO.getImaginary(), is(closeTo(0., Helpers.EPSILON))); + assertThat(Complex.ZERO.getReal()).isCloseTo(0., within(Helpers.EPSILON)); + assertThat(Complex.ZERO.getImaginary()).isCloseTo(0., within(Helpers.EPSILON)); } @Test void testNegate(){ - assertThat(two.negate(), is(equalTo(new Complex(-2,0)))); - assertThat(minusI.negate(), is(equalTo(i))); - assertThat(oneMinusI.negate(), is(equalTo(new Complex(-1, 1)))); + assertThat(two.negate()).isEqualTo(new Complex(-2,0)); + assertThat(minusI.negate()).isEqualTo(i); + assertThat(oneMinusI.negate()).isEqualTo(new Complex(-1, 1)); } @Test void testReciprocal(){ - assertThat(one.reciprocal(), is(equalTo(one))); - assertThat(minusI.reciprocal(), is(equalTo(i))); - assertThat(two.reciprocal(), is(equalTo(new Complex(0.5,0)))); - assertThat(oneMinusI.reciprocal(), is(equalTo(new Complex(0.5,0.5)))); + assertThat(one.reciprocal()).isEqualTo(one); + assertThat(minusI.reciprocal()).isEqualTo(i); + assertThat(two.reciprocal()).isEqualTo(new Complex(0.5,0)); + assertThat(oneMinusI.reciprocal()).isEqualTo(new Complex(0.5,0.5)); } @Test void testReciprocalOfZero(){ - assertThat(()->zero.reciprocal(), throwsException(ArithmeticException.class)); + assertThatExceptionOfType(ArithmeticException.class) + .isThrownBy(()->zero.reciprocal()); } - - @Test void testDivide(){ - assertThat(onePlusI.divide(Complex.ONE), equalTo(onePlusI)); - assertThat(Complex.ONE.divide(two), equalTo(new Complex(0.5, 0))); - assertThat(oneMinusI.divide(onePlusI), equalTo(minusI)); + assertThat(onePlusI.divide(Complex.ONE)).isEqualTo(onePlusI); + assertThat(Complex.ONE.divide(two)).isEqualTo(new Complex(0.5, 0)); + assertThat(oneMinusI.divide(onePlusI)).isEqualTo(minusI); } @Test void testDivideByZero(){ - assertThat(()->one.divide(zero), throwsException(ArithmeticException.class)); + assertThatExceptionOfType(ArithmeticException.class) + .isThrownBy(()->one.divide(zero)); } @Test void testConjugate(){ - assertThat(two.conjugate(), equalTo(two)); - assertThat(oneMinusI.conjugate(), equalTo(onePlusI)); + assertThat(two.conjugate()).isEqualTo(two); + assertThat(oneMinusI.conjugate()).isEqualTo(onePlusI); } @Test void testRotation(){ - assertThat(Complex.rotation(Math.PI/2), equalTo(i)); - assertThat(Complex.rotation(-Math.PI/2), equalTo(minusI)); - assertThat(Complex.rotation(0), equalTo(one)); - assertThat(Complex.rotation(Math.PI/4), equalTo(new Complex(Math.sqrt(2)/2., Math.sqrt(2)/2.))); - assertThat(Complex.rotation(Math.PI/3), equalTo(new Complex(1./2., Math.sqrt(3)/2.))); + assertThat(Complex.rotation(Math.PI/2)).isEqualTo(i); + assertThat(Complex.rotation(-Math.PI/2)).isEqualTo(minusI); + assertThat(Complex.rotation(0)).isEqualTo(one); + assertThat(Complex.rotation(Math.PI/4)).isEqualTo(new Complex(Math.sqrt(2)/2., Math.sqrt(2)/2.)); + assertThat(Complex.rotation(Math.PI/3)).isEqualTo(new Complex(1./2., Math.sqrt(3)/2.)); } @Test void testBasicToString(){ - assertThat(two.toString(), containsString("2.0")); - assertThat(i.toString(), containsString("i")); + assertThat(two.toString()).contains("2.0"); + assertThat(i.toString()).contains("i"); } @Test void testToStringFormat(){ - assertThat(oneMinusI.toString(), is(equalTo("1.0 - 1.0i"))); - assertThat(onePlusI.toString(), is(equalTo("1.0 + 1.0i"))); - assertThat(minusI.toString(), is(equalTo("-1.0i"))); - assertThat(twoI.toString(), is(equalTo("2.0i"))); - assertThat(two.toString(), is(equalTo("2.0"))); + assertThat(oneMinusI.toString()).isEqualTo("1.0 - 1.0i"); + assertThat(onePlusI.toString()).isEqualTo("1.0 + 1.0i"); + assertThat(minusI.toString()).isEqualTo("-1.0i"); + assertThat(twoI.toString()).isEqualTo("2.0i"); + assertThat(two.toString()).isEqualTo("2.0"); } @Test void testReal(){ - assertThat(Complex.real(1.), is(equalTo(one))); - assertThat(Complex.real(2.), is(equalTo(two))); + assertThat(Complex.real(1.)).isEqualTo(one); + assertThat(Complex.real(2.)).isEqualTo(two); } @Test void testAdd(){ - assertThat(one.add(one), is(equalTo(two))); - assertThat(one.add(i), is(equalTo(onePlusI))); + assertThat(one.add(one)).isEqualTo(two); + assertThat(one.add(i)).isEqualTo(onePlusI); } @Test void testSubtract(){ - assertThat(zero.subtract(one), is(equalTo(minusOne))); - assertThat(one.subtract(i), is(equalTo(oneMinusI))); + assertThat(zero.subtract(one)).isEqualTo(minusOne); + assertThat(one.subtract(i)).isEqualTo(oneMinusI); } @Test void testMultiply(){ - assertThat(zero.multiply(one), is(equalTo(zero))); - assertThat(i.multiply(i), is(new Complex(-1, 0))); + assertThat(zero.multiply(one)).isEqualTo(zero); + assertThat(i.multiply(i)).isEqualTo(new Complex(-1, 0)); } @Test void testSquaredModulus(){ - assertThat(i.squaredModulus(), is(closeTo(1., Helpers.EPSILON))); - assertThat(two.squaredModulus(), is(closeTo(4., Helpers.EPSILON))); + assertThat(i.squaredModulus()).isCloseTo(1., within(Helpers.EPSILON)); + assertThat(two.squaredModulus()).isCloseTo(4., within(Helpers.EPSILON)); } @Test void testModulus(){ - assertThat(i.modulus(), is(closeTo(1., Helpers.EPSILON))); - assertThat(two.modulus(), is(closeTo(2., Helpers.EPSILON))); + assertThat(i.modulus()).isCloseTo(1., within(Helpers.EPSILON)); + assertThat(two.modulus()).isCloseTo(2., within(Helpers.EPSILON)); } @Test void testPow(){ - assertThat(i.pow(4), is(equalTo(one))); - assertThat(two.pow(4), is(equalTo(new Complex(16,0)))); + assertThat(i.pow(4)).isEqualTo(one); + assertThat(two.pow(4)).isEqualTo(new Complex(16,0)); } @Test void testScale(){ - assertThat(oneMinusI.scale(4), is(equalTo(new Complex(4, -4)))); - assertThat(two.scale(8), is(equalTo(new Complex(16,0)))); + assertThat(oneMinusI.scale(4)).isEqualTo(new Complex(4, -4)); + assertThat(two.scale(8)).isEqualTo(new Complex(16,0)); } } \ No newline at end of file -- GitLab