BAEL-1517: Upgrade AssertJ to 3.9.0;
add Java 8 style assertion tests
This commit is contained in:
parent
4344479d7a
commit
145db05949
@ -173,7 +173,7 @@
|
|||||||
<jacoco.version>0.7.7.201606060606</jacoco.version>
|
<jacoco.version>0.7.7.201606060606</jacoco.version>
|
||||||
<guava.version>21.0</guava.version>
|
<guava.version>21.0</guava.version>
|
||||||
<assertj-guava.version>3.1.0</assertj-guava.version>
|
<assertj-guava.version>3.1.0</assertj-guava.version>
|
||||||
<assertj-core.version>3.6.1</assertj-core.version>
|
<assertj-core.version>3.9.0</assertj-core.version>
|
||||||
<assertj-generator.version>2.1.0</assertj-generator.version>
|
<assertj-generator.version>2.1.0</assertj-generator.version>
|
||||||
<truth.version>0.32</truth.version>
|
<truth.version>0.32</truth.version>
|
||||||
<jUnitParams.version>1.1.0</jUnitParams.version>
|
<jUnitParams.version>1.1.0</jUnitParams.version>
|
||||||
|
@ -15,9 +15,10 @@ public class Java7StyleAssertions {
|
|||||||
int denominator = 0;
|
int denominator = 0;
|
||||||
int quotient = numerator / denominator;
|
int quotient = numerator / denominator;
|
||||||
fail("ArithmeticException expected because dividing by zero yields an ArithmeticException.");
|
fail("ArithmeticException expected because dividing by zero yields an ArithmeticException.");
|
||||||
failBecauseExceptionWasNotThrown(IndexOutOfBoundsException.class);
|
failBecauseExceptionWasNotThrown(ArithmeticException.class);
|
||||||
} catch (ArithmeticException e) {
|
} catch (Exception e) {
|
||||||
assertThat(e).hasMessage("/ by zero");
|
assertThat(e).hasMessage("/ by zero");
|
||||||
|
assertThat(e).isInstanceOf(ArithmeticException.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.testing.assertj.exceptions;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Java8StyleAssertions {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDividingByZero_thenArithmeticException() {
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
int numerator = 10;
|
||||||
|
int denominator = 0;
|
||||||
|
int quotient = numerator / denominator;
|
||||||
|
}).isInstanceOf(ArithmeticException.class)
|
||||||
|
.hasMessageContaining("/ by zero");
|
||||||
|
|
||||||
|
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
|
||||||
|
int numerator = 10;
|
||||||
|
int denominator = 0;
|
||||||
|
int quotient = numerator / denominator;
|
||||||
|
})
|
||||||
|
.withMessageContaining("/ by zero");
|
||||||
|
|
||||||
|
// BDD style:
|
||||||
|
|
||||||
|
// when
|
||||||
|
Throwable thrown = catchThrowable(() -> {
|
||||||
|
int numerator = 10;
|
||||||
|
int denominator = 0;
|
||||||
|
int quotient = numerator / denominator;
|
||||||
|
});
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(thrown).isInstanceOf(ArithmeticException.class)
|
||||||
|
.hasMessageContaining("/ by zero");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user