Optimize AssertJ exception checks

pull/140/head
Richard Körber 2023-05-07 09:25:04 +02:00
parent aa5e78c525
commit c26d6b1f8a
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
5 changed files with 22 additions and 24 deletions

View File

@ -308,7 +308,7 @@ public class OrderTest {
var order = login.bindOrder(locationUrl); var order = login.bindOrder(locationUrl);
try (var softly = new AutoCloseableSoftAssertions()) { try (var softly = new AutoCloseableSoftAssertions()) {
softly.assertThatExceptionOfType(IllegalStateException.class) softly.assertThatIllegalStateException()
.isThrownBy(order::getCertificate); .isThrownBy(order::getCertificate);
softly.assertThat(order.getAutoRenewalCertificate().orElseThrow().getLocation()) softly.assertThat(order.getAutoRenewalCertificate().orElseThrow().getLocation())
.isEqualTo(url("https://example.com/acme/cert/1234")); .isEqualTo(url("https://example.com/acme/cert/1234"));

View File

@ -14,7 +14,7 @@
package org.shredzone.acme4j.connector; package org.shredzone.acme4j.connector;
import static org.assertj.core.api.Assertions.assertThat; 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 java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
@ -40,7 +40,7 @@ public class SessionProviderTest {
*/ */
@Test @Test
public void testNone() { public void testNone() {
assertThatExceptionOfType(IllegalArgumentException.class) assertThatIllegalArgumentException()
.isThrownBy(() -> new Session(new URI("acme://example.org")).provider()) .isThrownBy(() -> new Session(new URI("acme://example.org")).provider())
.withMessage("No ACME provider found for acme://example.org"); .withMessage("No ACME provider found for acme://example.org");
} }
@ -67,7 +67,7 @@ public class SessionProviderTest {
*/ */
@Test @Test
public void testDuplicate() { public void testDuplicate() {
assertThatExceptionOfType(IllegalArgumentException.class) assertThatIllegalArgumentException()
.isThrownBy(() -> new Session(new URI("acme://example.net")).provider()) .isThrownBy(() -> new Session(new URI("acme://example.net")).provider())
.withMessage("Both ACME providers Provider1 and Provider2 accept" + .withMessage("Both ACME providers Provider1 and Provider2 accept" +
" acme://example.net. Please check your classpath."); " acme://example.net. Please check your classpath.");

View File

@ -14,8 +14,7 @@
package org.shredzone.acme4j.smime.csr; package org.shredzone.acme4j.smime.csr;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -155,16 +154,16 @@ public class SMIMECSRBuilderTest {
assertThat(builder.toString()).isEqualTo(",TYPE=SIGNING_AND_ENCRYPTION"); assertThat(builder.toString()).isEqualTo(",TYPE=SIGNING_AND_ENCRYPTION");
assertThatExceptionOfType(NullPointerException.class) assertThatNullPointerException()
.as("addValue(String, String)") .as("addValue(String, String)")
.isThrownBy(() -> new SMIMECSRBuilder().addValue((String) null, "value")); .isThrownBy(() -> new SMIMECSRBuilder().addValue((String) null, "value"));
assertThatExceptionOfType(NullPointerException.class) assertThatNullPointerException()
.as("addValue(ASN1ObjectIdentifier, String)") .as("addValue(ASN1ObjectIdentifier, String)")
.isThrownBy(() -> new SMIMECSRBuilder().addValue((ASN1ObjectIdentifier) null, "value")); .isThrownBy(() -> new SMIMECSRBuilder().addValue((ASN1ObjectIdentifier) null, "value"));
assertThatExceptionOfType(NullPointerException.class) assertThatNullPointerException()
.as("addValue(String, null)") .as("addValue(String, null)")
.isThrownBy(() -> new SMIMECSRBuilder().addValue("C", null)); .isThrownBy(() -> new SMIMECSRBuilder().addValue("C", null));
assertThatExceptionOfType(IllegalArgumentException.class) assertThatIllegalArgumentException()
.as("addValue(String, null)") .as("addValue(String, null)")
.isThrownBy(() -> new SMIMECSRBuilder().addValue("UNKNOWNATT", "val")) .isThrownBy(() -> new SMIMECSRBuilder().addValue("UNKNOWNATT", "val"))
.withMessage(invAttNameExMessage); .withMessage(invAttNameExMessage);

View File

@ -234,7 +234,7 @@ public class EmailProcessorTest extends SMIMETests {
@Test @Test
public void textNoChallengeFails1() { public void textNoChallengeFails1() {
assertThatExceptionOfType(IllegalStateException.class) assertThatIllegalStateException()
.isThrownBy(() -> { .isThrownBy(() -> {
var processor = EmailProcessor.plainMessage(message); var processor = EmailProcessor.plainMessage(message);
processor.getToken(); processor.getToken();
@ -244,7 +244,7 @@ public class EmailProcessorTest extends SMIMETests {
@Test @Test
public void textNoChallengeFails2() { public void textNoChallengeFails2() {
assertThatExceptionOfType(IllegalStateException.class) assertThatIllegalStateException()
.isThrownBy(() -> { .isThrownBy(() -> {
var processor = EmailProcessor.plainMessage(message); var processor = EmailProcessor.plainMessage(message);
processor.getAuthorization(); processor.getAuthorization();
@ -254,7 +254,7 @@ public class EmailProcessorTest extends SMIMETests {
@Test @Test
public void textNoChallengeFails3() { public void textNoChallengeFails3() {
assertThatExceptionOfType(IllegalStateException.class) assertThatIllegalStateException()
.isThrownBy(() -> { .isThrownBy(() -> {
var processor = EmailProcessor.plainMessage(message); var processor = EmailProcessor.plainMessage(message);
processor.respond(); processor.respond();

View File

@ -13,8 +13,7 @@
*/ */
package org.shredzone.acme4j.util; package org.shredzone.acme4j.util;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -133,17 +132,17 @@ public class CSRBuilderTest {
public void testNoSign() { public void testNoSign() {
var builder = new CSRBuilder(); var builder = new CSRBuilder();
assertThatExceptionOfType(IllegalStateException.class) assertThatIllegalStateException()
.isThrownBy(builder::getCSR) .isThrownBy(builder::getCSR)
.as("getCSR()") .as("getCSR()")
.withMessage("sign CSR first"); .withMessage("sign CSR first");
assertThatExceptionOfType(IllegalStateException.class) assertThatIllegalStateException()
.isThrownBy(builder::getEncoded) .isThrownBy(builder::getEncoded)
.as("getCSR()") .as("getCSR()")
.withMessage("sign CSR first"); .withMessage("sign CSR first");
assertThatExceptionOfType(IllegalStateException.class) assertThatIllegalStateException()
.isThrownBy(() -> { .isThrownBy(() -> {
try (StringWriter w = new StringWriter()) { try (StringWriter w = new StringWriter()) {
builder.write(w); builder.write(w);
@ -167,16 +166,16 @@ public class CSRBuilderTest {
assertThat(builder.toString()).isEqualTo(""); assertThat(builder.toString()).isEqualTo("");
assertThatExceptionOfType(NullPointerException.class) assertThatNullPointerException()
.isThrownBy(() -> new CSRBuilder().addValue((String) null, "value")) .isThrownBy(() -> new CSRBuilder().addValue((String) null, "value"))
.as("addValue(String, String)"); .as("addValue(String, String)");
assertThatExceptionOfType(NullPointerException.class) assertThatNullPointerException()
.isThrownBy(() -> new CSRBuilder().addValue((ASN1ObjectIdentifier) null, "value")) .isThrownBy(() -> new CSRBuilder().addValue((ASN1ObjectIdentifier) null, "value"))
.as("addValue(ASN1ObjectIdentifier, String)"); .as("addValue(ASN1ObjectIdentifier, String)");
assertThatExceptionOfType(NullPointerException.class) assertThatNullPointerException()
.isThrownBy(() -> new CSRBuilder().addValue("C", null)) .isThrownBy(() -> new CSRBuilder().addValue("C", null))
.as("addValue(String, null)"); .as("addValue(String, null)");
assertThatExceptionOfType(IllegalArgumentException.class) assertThatIllegalArgumentException()
.isThrownBy(() -> new CSRBuilder().addValue("UNKNOWNATT", "val")) .isThrownBy(() -> new CSRBuilder().addValue("UNKNOWNATT", "val"))
.as("addValue(String, null)") .as("addValue(String, null)")
.withMessage(invAttNameExMessage); .withMessage(invAttNameExMessage);