Report offending class in SpringFactoriesLoader in exception message
Prior to this commit, the exception message generated by instantiateFactory() in SpringFactoriesLoader did not report the offending class in the top-level exception message. The offending class was in fact included in the message of the nested exception, but the top-level exception message on its own was a bit misleading. This commit improves the diagnostics for such failures by including the offending class and the target factory type in the top-level exception message. Closes gh-22453
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,9 @@ package org.springframework.core.io.support;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -28,9 +30,13 @@ import static org.junit.Assert.*;
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class SpringFactoriesLoaderTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void loadFactoriesInCorrectOrder() {
|
||||
List<DummyFactory> factories = SpringFactoriesLoader.loadFactories(DummyFactory.class, null);
|
||||
@@ -39,17 +45,20 @@ public class SpringFactoriesLoaderTests {
|
||||
assertTrue(factories.get(1) instanceof MyDummyFactory2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void loadInvalid() {
|
||||
SpringFactoriesLoader.loadFactories(String.class, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadPackagePrivateFactory() {
|
||||
List<DummyPackagePrivateFactory> factories =
|
||||
SpringFactoriesLoader.loadFactories(DummyPackagePrivateFactory.class, null);
|
||||
assertEquals(1, factories.size());
|
||||
assertTrue((factories.get(0).getClass().getModifiers() & Modifier.PUBLIC) == 0);
|
||||
assertFalse(Modifier.isPublic(factories.get(0).getClass().getModifiers()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attemptToLoadFactoryOfIncompatibleType() {
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectMessage("Unable to instantiate factory class [org.springframework.core.io.support.MyDummyFactory1] for factory type [java.lang.String]");
|
||||
|
||||
SpringFactoriesLoader.loadFactories(String.class, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user