AssertJ Exception Assertions (#3600)
* Add XML, JavaConfig and Autowired examples.
* BAEL-1517: Added Java7 style assertions
* BAEL-1517: Upgrade AssertJ to 3.9.0;
add Java 8 style assertion tests
* Revert "Add XML, JavaConfig and Autowired examples."
This reverts commit 8f4df6b903
.
This commit is contained in:
parent
56316fd029
commit
5a34de448e
|
@ -173,7 +173,7 @@
|
|||
<jacoco.version>0.7.7.201606060606</jacoco.version>
|
||||
<guava.version>21.0</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>
|
||||
<truth.version>0.32</truth.version>
|
||||
<jUnitParams.version>1.1.0</jUnitParams.version>
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.testing.assertj.exceptions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Java7StyleAssertions {
|
||||
|
||||
@Test
|
||||
public void whenDividingByZero_thenArithmeticException() {
|
||||
try {
|
||||
int numerator = 10;
|
||||
int denominator = 0;
|
||||
int quotient = numerator / denominator;
|
||||
fail("ArithmeticException expected because dividing by zero yields an ArithmeticException.");
|
||||
failBecauseExceptionWasNotThrown(ArithmeticException.class);
|
||||
} catch (Exception e) {
|
||||
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…
Reference in New Issue