Skip to content

Commit

Permalink
Use assertThrows
Browse files Browse the repository at this point in the history
Replace fail with assertThrows in serveral places
  • Loading branch information
marschall committed Dec 1, 2023
1 parent db56a3b commit 3c65dbd
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 181 deletions.
15 changes: 0 additions & 15 deletions src/test/java/org/eclipse/yasson/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,6 @@

public class Assertions {

/**
* Asserts that the given operation will fail with a JsonbException
* @param operation The operation that is expected to fail
*/
public static void shouldFail(Supplier<?> operation) {
shouldFail(operation, JsonbException.class, msg -> true);
}

public static void shouldFail(Runnable operation) {
shouldFail(() -> {
operation.run();
return null;
});
}

/**
* Asserts that the given operation will fail with a JsonbException
* @param operation The operation that is expected to fail
Expand Down
9 changes: 2 additions & 7 deletions src/test/java/org/eclipse/yasson/Issue456Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package org.eclipse.yasson;

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

Expand All @@ -23,12 +23,7 @@ public class Issue456Test {

@Test
public void dontInvokeToString() {
try {
JsonbBuilder.create().toJson(new Example());
fail("JsonbException is expected");
} catch (JsonbException e) {
// Expected
}
assertThrows(JsonbException.class, () -> JsonbBuilder.create().toJson(new Example()));
}

public static class Example {
Expand Down
9 changes: 3 additions & 6 deletions src/test/java/org/eclipse/yasson/JavaxNamingExcludedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ public class JavaxNamingExcludedTest {

@Test
public void testNoJavaxNamingModule() {
try {
Class.forName(JsonbComponentInstanceCreatorFactory.INITIAL_CONTEXT_CLASS);
fail("Class [" + JsonbComponentInstanceCreatorFactory.INITIAL_CONTEXT_CLASS
//OK, java.naming is not observable
assertThrows(ClassNotFoundException.class, () -> Class.forName(JsonbComponentInstanceCreatorFactory.INITIAL_CONTEXT_CLASS),
() -> "Class [" + JsonbComponentInstanceCreatorFactory.INITIAL_CONTEXT_CLASS
+ "] should not be available for this test.");
} catch (ClassNotFoundException e) {
//OK, java.naming is not observable
}

final String result = defaultJsonb.toJson(new AdaptedPojo());
assertEquals("{\"adaptedValue1\":1111,\"adaptedValue2\":1001,\"adaptedValue3\":1010}", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ public static class AnnotatedPojo<T,X> {
public void testIncompatibleAdapter() throws Exception {
IncompatibleAdapterPojo incompatibleAdapterFieldPojo = new IncompatibleAdapterPojo();
incompatibleAdapterFieldPojo.str = "STR";
try {
defaultJsonb.toJson(incompatibleAdapterFieldPojo);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("Adapter of runtime type class"));
assertTrue(e.getMessage().contains("does not match property type "));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.toJson(incompatibleAdapterFieldPojo));
assertTrue(e.getMessage().startsWith("Adapter of runtime type class"));
assertTrue(e.getMessage().contains("does not match property type "));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* @author Roman Grigoriadi
Expand Down Expand Up @@ -79,22 +78,14 @@ public void testRootCreatorWithInnerCreator() {

@Test
public void testIncompatibleFactoryMethodReturnType() {
try {
defaultJsonb.fromJson("{\"s1\":\"abc\"}", CreatorIncompatibleTypePojo.class);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("Return type of creator"));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.fromJson("{\"s1\":\"abc\"}", CreatorIncompatibleTypePojo.class));
assertTrue(e.getMessage().startsWith("Return type of creator"));
}

@Test
public void testMultipleCreatorsError() {
try {
defaultJsonb.fromJson("{\"s1\":\"abc\"}", CreatorMultipleDeclarationErrorPojo.class);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("More than one @JsonbCreator"));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.fromJson("{\"s1\":\"abc\"}", CreatorMultipleDeclarationErrorPojo.class));
assertTrue(e.getMessage().startsWith("More than one @JsonbCreator"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,77 +67,53 @@ public void testTransientCollidesOnProperty() throws Exception {
JsonbTransientCollisionOnProperty pojo = new JsonbTransientCollisionOnProperty();
pojo.setTransientProperty("TRANSIENT");

try {
defaultJsonb.toJson(pojo);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.toJson(pojo));
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}

@Test
public void testTransientCollidesOnGetter() throws Exception {
JsonbTransientCollisionOnGetter pojo = new JsonbTransientCollisionOnGetter();
pojo.setTransientProperty("TRANSIENT");

try {
defaultJsonb.toJson(pojo);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.toJson(pojo));
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}

@Test
public void testTransientCollidesOnPropertyAndGetter() throws Exception {
JsonbTransientCollisionOnPropertyAndGetter pojo = new JsonbTransientCollisionOnPropertyAndGetter();
pojo.setTransientProperty("TRANSIENT");

try {
defaultJsonb.toJson(pojo);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.toJson(pojo));
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}

@Test
public void testTransientCollidesOnSetter() throws Exception {
JsonbTransientCollisionOnSetter pojo = new JsonbTransientCollisionOnSetter();
pojo.setTransientProperty("TRANSIENT");

try {
defaultJsonb.toJson(pojo);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.toJson(pojo));
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}

@Test
public void testTransientCollidesOnPropertyAndSetter() throws Exception {
JsonbTransientCollisionOnPropertyAndSetter pojo = new JsonbTransientCollisionOnPropertyAndSetter();
pojo.setTransientProperty("TRANSIENT");

try {
defaultJsonb.toJson(pojo);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.toJson(pojo));
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}

@Test
public void testTransientCollidesOnPropertyAndGetterAndSetter() throws Exception {
JsonbTransientCollisionOnPropertyAndGetterAndSetter pojo = new JsonbTransientCollisionOnPropertyAndGetterAndSetter();
pojo.setTransientProperty("TRANSIENT");

try {
defaultJsonb.toJson(pojo);
fail();
} catch (JsonbException e) {
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.toJson(pojo));
assertTrue(e.getMessage().startsWith("JsonbTransient annotation cannot be used with other jsonb annotations on the same property."));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonWriter;
import jakarta.json.bind.JsonbException;
import jakarta.json.stream.JsonGenerator;

import java.io.StringWriter;
Expand Down Expand Up @@ -216,7 +217,7 @@ public static class NumberContainer {

@Test
public void testSerializeInvalidDouble() {
shouldFail(() -> defaultJsonb.toJson(Double.POSITIVE_INFINITY));
assertThrows(JsonbException.class, () -> defaultJsonb.toJson(Double.POSITIVE_INFINITY));

NumberContainer obj = new NumberContainer();
obj.doubleProp = Double.POSITIVE_INFINITY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public void testSingleValue() {
assertEquals("5", bindingJsonb.toJson(5));

Jsonb jsonb = new JsonBindingBuilder().withConfig(new JsonbConfig().withStrictIJSON(true)).build();
try {
jsonb.toJson(5);
fail();
} catch (JsonbException exception){
assertEquals(Messages.getMessage(MessageKeys.IJSON_ENABLED_SINGLE_VALUE), exception.getMessage());
}
JsonbException exception = assertThrows(JsonbException.class, () -> jsonb.toJson(5));
assertEquals(Messages.getMessage(MessageKeys.IJSON_ENABLED_SINGLE_VALUE), exception.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import static org.eclipse.yasson.Jsonbs.bindingJsonb;
import static org.eclipse.yasson.Jsonbs.defaultJsonb;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* This class contains tests for marshalling/unmarshalling dates.
Expand Down Expand Up @@ -194,9 +194,7 @@ private void testSqlDateWithTZ(TimeZone tz) {
@Test
public void testMarshallLocalDate() {
String jsonString = bindingJsonb.toJson(new LocalDateObj());
if (jsonString.contains("T")) {
fail("JSON contains time for a non Date that doesn't include time");
}
assertFalse(jsonString.contains("T"), "JSON contains time for a non Date that doesn't include time");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,11 @@ public void testMethodModifiers() {

@Test
public void testConstructorModifiers() {
try{
ProtectedConstructorClass instance = defaultJsonb.fromJson("{\"randomField\":\"test\"}", ProtectedConstructorClass.class);
assertEquals(instance.randomField, "test");
} catch (JsonbException e){
fail("No exception should be thrown for protected constructor");
throw e;
}
try {
defaultJsonb.fromJson("{\"randomField\":\"test\"}", PrivateConstructorClass.class);
fail("Exception should have been thrown");
}catch (JsonbException e){
assertTrue(e.getMessage().endsWith("Can't create instance"));
}
ProtectedConstructorClass instance = assertDoesNotThrow(() -> defaultJsonb.fromJson("{\"randomField\":\"test\"}", ProtectedConstructorClass.class), "No exception should be thrown for protected constructor");
assertEquals(instance.randomField, "test");

JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.fromJson("{\"randomField\":\"test\"}", PrivateConstructorClass.class), "Exception should have been thrown");
assertTrue(e.getMessage().endsWith("Can't create instance"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package org.eclipse.yasson.defaultmapping.specific;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;

Expand Down Expand Up @@ -44,44 +44,32 @@ public class RecursiveReferenceTest {
public void testSerializeRecursiveReference() {
Chain recursive = new Chain("test");
recursive.setLinksTo(recursive);
try {
Jsonbs.defaultJsonb.toJson(recursive);
fail("Exception should be caught");
} catch (JsonbException e) {
assertEquals(
"Unable to serialize property 'linksTo' from org.eclipse.yasson.adapters.model.Chain",
e.getMessage());
assertEquals(
"Recursive reference has been found in class class org.eclipse.yasson.adapters.model.Chain.",
e.getCause().getMessage());
}
JsonbException e = assertThrows(JsonbException.class, () -> Jsonbs.defaultJsonb.toJson(recursive), "Exception should be caught");
assertEquals(
"Unable to serialize property 'linksTo' from org.eclipse.yasson.adapters.model.Chain",
e.getMessage());
assertEquals(
"Recursive reference has been found in class class org.eclipse.yasson.adapters.model.Chain.",
e.getCause().getMessage());
}

@Test
public void testSerializeRecursiveReferenceCustomAdapter() {
Chain recursive = new Chain("test");
recursive.setLinksTo(recursive);
try {
adapterSerializerJsonb.toJson(recursive);
fail("Exception should be caught");
} catch (JsonbException e) {
assertEquals("Problem adapting object of type class org.eclipse.yasson.adapters.model.Chain to java.util.Map<java.lang"
+ ".String, java.lang.Object> in class class org.eclipse.yasson.adapters.model.ChainAdapter",
e.getMessage());
}
JsonbException e = assertThrows(JsonbException.class, () -> adapterSerializerJsonb.toJson(recursive), "Exception should be caught");
assertEquals("Problem adapting object of type class org.eclipse.yasson.adapters.model.Chain to java.util.Map<java.lang"
+ ".String, java.lang.Object> in class class org.eclipse.yasson.adapters.model.ChainAdapter",
e.getMessage());
}

@Test
public void testSerializeRecursiveReferenceCustomSerializer() {
Chain recursive = new Chain("test");
recursive.setLinksTo(recursive);
try {
userSerializerJsonb.toJson(recursive);
fail("Exception should be caught");
} catch (JsonbException e) {
assertEquals("Recursive reference has been found in class class org.eclipse.yasson.adapters.model.Chain.",
e.getMessage());
}
JsonbException e = assertThrows(JsonbException.class, () -> userSerializerJsonb.toJson(recursive), "Exception should be caught");
assertEquals("Recursive reference has been found in class class org.eclipse.yasson.adapters.model.Chain.",
e.getMessage());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* @author Roman Grigoriadi
Expand All @@ -68,13 +67,9 @@ public void setValue(String value) {
};
String expected = "{\"customInterface\":{\"value\":\"value1\"}}";
assertEquals(expected, defaultJsonb.toJson(unsupported));
try {
defaultJsonb.fromJson(expected, ClassWithUnsupportedFields.class);
fail("Should report an error");
} catch (JsonbException e) {
assertTrue(e.getMessage().contains("Cannot infer a type"));
assertTrue(e.getMessage().contains("customInterface"));
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.fromJson(expected, ClassWithUnsupportedFields.class), "Should report an error");
assertTrue(e.getMessage().contains("Cannot infer a type"));
assertTrue(e.getMessage().contains("customInterface"));
}

@Test
Expand Down Expand Up @@ -225,14 +220,9 @@ public void testEmptyStringAsOptionalLong() {
}

private void assertFail(String json, Type type, String failureProperty, Class<?> failurePropertyClass) {
try {
defaultJsonb.fromJson(json, type);
fail();
} catch (JsonbException e) {
if(!e.getMessage().contains(failureProperty) || !e.getMessage().contains(failurePropertyClass.getName())) {
fail("Expected error message to contain '" + failureProperty + "' and '" + failurePropertyClass.getName() + "', but was: " +
e.getMessage());
}
}
JsonbException e = assertThrows(JsonbException.class, () -> defaultJsonb.fromJson(json, type));
assertTrue(e.getMessage().contains(failureProperty) && e.getMessage().contains(failurePropertyClass.getName()), () ->
"Expected error message to contain '" + failureProperty + "' and '" + failurePropertyClass.getName() + "', but was: " +
e.getMessage());
}
}
Loading

0 comments on commit 3c65dbd

Please sign in to comment.