-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error message when abstract class cannot be constructed (#1814)
- Loading branch information
1 parent
565b7a1
commit 47dea2e
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
gson/src/test/java/com/google/gson/internal/ConstructorConstructorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.google.gson.internal; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
|
||
import com.google.gson.InstanceCreator; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
public class ConstructorConstructorTest { | ||
private static final Map<Type, InstanceCreator<?>> NO_INSTANCE_CREATORS = Collections.emptyMap(); | ||
|
||
private abstract static class AbstractClass { | ||
@SuppressWarnings("unused") | ||
public AbstractClass() { } | ||
} | ||
private interface Interface { } | ||
|
||
/** | ||
* Verify that ConstructorConstructor does not try to invoke no-arg constructor | ||
* of abstract class. | ||
*/ | ||
@Test | ||
public void testGet_AbstractClassNoArgConstructor() { | ||
ConstructorConstructor constructorFactory = new ConstructorConstructor(NO_INSTANCE_CREATORS, true); | ||
ObjectConstructor<AbstractClass> constructor = constructorFactory.get(TypeToken.get(AbstractClass.class)); | ||
try { | ||
constructor.construct(); | ||
fail("Expected exception"); | ||
} catch (RuntimeException exception) { | ||
assertEquals( | ||
"Unable to create instance of class com.google.gson.internal.ConstructorConstructorTest$AbstractClass. " | ||
+ "Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.", | ||
exception.getMessage() | ||
); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGet_Interface() { | ||
ConstructorConstructor constructorFactory = new ConstructorConstructor(NO_INSTANCE_CREATORS, true); | ||
ObjectConstructor<Interface> constructor = constructorFactory.get(TypeToken.get(Interface.class)); | ||
try { | ||
constructor.construct(); | ||
fail("Expected exception"); | ||
} catch (RuntimeException exception) { | ||
assertEquals( | ||
"Unable to create instance of interface com.google.gson.internal.ConstructorConstructorTest$Interface. " | ||
+ "Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.", | ||
exception.getMessage() | ||
); | ||
} | ||
} | ||
} |