Skip to content

Commit

Permalink
2.x Replace try/catch in tests on assertThrows (#7376)
Browse files Browse the repository at this point in the history
  • Loading branch information
Captain1653 authored Sep 12, 2023
1 parent fd92244 commit e50fb56
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -262,11 +262,7 @@ public void testMap() {

@Test
public void testMapNullMapper() {
try {
Single.just("foo").map(null);
fail("NullPointerException should be thrown");
} catch (NullPointerException ex) {
}
assertThrows(NullPointerException.class, () -> Single.just("foo").map(null), "NullPointerException should be thrown");
}

@Test
Expand Down Expand Up @@ -375,11 +371,7 @@ public void testNeverFlatMap() {

@Test
public void testFlatMapNullMapper() {
try {
Single.just("bar").flatMap(null);
fail("NullPointerException should have been thrown");
} catch (NullPointerException ex) {
}
assertThrows(NullPointerException.class, () -> Single.just("bar").flatMap(null), "NullPointerException should have been thrown");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,23 +16,18 @@

package io.helidon.config.mp;

import java.util.LinkedList;
import java.util.List;

import io.helidon.config.ConfigException;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.junit.jupiter.api.Assertions.assertThrows;


class MpMetaConfigTest {
Expand Down Expand Up @@ -112,10 +107,7 @@ void testMetaPropertiesPathProfile() {
void testMetaPropertiesNotOptional() {
System.setProperty(MpMetaConfig.META_CONFIG_SYSTEM_PROPERTY,
"custom-mp-meta-config-not-optional.properties");
try {
config = ConfigProvider.getConfig();
Assertions.fail("Expecting meta-config to fail due to not optional non-existent config source");
} catch (ConfigException e) {}
assertThrows(ConfigException.class, ConfigProvider::getConfig, "Expecting meta-config to fail due to not optional non-existent config source");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,13 +22,13 @@
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class HoconJsonMpMetaConfigTest {
private static ConfigProviderResolver configResolver;
Expand Down Expand Up @@ -150,9 +150,6 @@ void testMetaOrdinal() {
@Test
void testMetaHoconNonExistentNotOptional() {
System.setProperty(META_CONFIG_SYSTEM_PROPERTY, "custom-mp-meta-config-hocon-path-not-optional.yaml");
try {
config = ConfigProvider.getConfig();
Assertions.fail("Expecting meta-config to fail due to not optional non-existent config source");
} catch (ConfigException e) {}
assertThrows(ConfigException.class, ConfigProvider::getConfig, "Expecting meta-config to fail due to not optional non-existent config source");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,6 @@
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -34,6 +33,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class YamlMpMetaConfigTest {
private static ConfigProviderResolver configResolver;
Expand Down Expand Up @@ -126,9 +126,6 @@ void testMetaOrdinal() {
@Test
void testMetaNonExistentNotOptional() {
System.setProperty(META_CONFIG_SYSTEM_PROPERTY, "custom-mp-meta-config-path-not-optional.yaml");
try {
config = ConfigProvider.getConfig();
Assertions.fail("Expecting meta-config to fail due to not optional non-existent config source");
} catch (ConfigException e) {}
assertThrows(ConfigException.class, ConfigProvider::getConfig, "Expecting meta-config to fail due to not optional non-existent config source");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
* Copyright (c) 2018, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,6 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Unit test for {@link SignedJwt}.
Expand Down Expand Up @@ -94,12 +93,8 @@ public void testParsingInvalidEncodingOnTokenParts() {
{"payload", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9+lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"},
{"signature", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV/adQssw5c"}
};
for (int i=0; i < tokens.length; i++) {
try {
SignedJwt.parseToken(tokens[i][1]);
fail("Parsing should have failed as the token " + tokens[i][0] + " is incorrectly encoded");
} catch (JwtException t) {
}
for (String[] token : tokens) {
assertThrows(JwtException.class, () -> SignedJwt.parseToken(token[1]), "Parsing should have failed as the token " + token[0] + " is incorrectly encoded");
}
}

Expand Down

0 comments on commit e50fb56

Please sign in to comment.