Skip to content

Commit

Permalink
Proper exception for non-matching argument on unique factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Mar 13, 2019
1 parent 88ca255 commit 4bee507
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight &&
}
}

if (factoryMethodToUse == null) {
if (factoryMethodToUse == null || argsToUse == null) {
if (causes != null) {
UnsatisfiedDependencyException ex = causes.removeLast();
for (Exception cause : causes) {
Expand Down Expand Up @@ -615,7 +615,6 @@ else if (ambiguousFactoryMethods != null) {
}
}

Assert.state(argsToUse != null, "Unresolved factory method arguments");
bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, factoryMethodToUse, argsToUse));
return bw;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.TimeUnit;

import org.awaitility.Awaitility;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

Expand All @@ -45,6 +46,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
Expand All @@ -53,10 +55,7 @@
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -188,7 +187,7 @@ public void customAsyncAnnotationIsPropagated() {
}

/**
* Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect
* Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect.
*/
@Test(expected = BeanDefinitionStoreException.class)
public void aspectModeAspectJAttemptsToRegisterAsyncAspect() {
Expand All @@ -199,7 +198,7 @@ public void aspectModeAspectJAttemptsToRegisterAsyncAspect() {
}

@Test
public void customExecutorBean() throws InterruptedException {
public void customExecutorBean() {
// Arrange
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(CustomExecutorBean.class);
Expand Down Expand Up @@ -256,7 +255,7 @@ public void customExecutorConfigWithThrowsException() {
}

@Test
public void customExecutorBeanConfig() throws InterruptedException {
public void customExecutorBeanConfig() {
// Arrange
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(CustomExecutorBeanConfig.class, ExecutorPostProcessor.class);
Expand Down Expand Up @@ -295,7 +294,7 @@ public void customExecutorBeanConfigWithThrowsException() {
}

@Test // SPR-14949
public void findOnInterfaceWithInterfaceProxy() throws InterruptedException {
public void findOnInterfaceWithInterfaceProxy() {
// Arrange
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigA.class);
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);
Expand All @@ -311,7 +310,7 @@ public void findOnInterfaceWithInterfaceProxy() throws InterruptedException {
}

@Test // SPR-14949
public void findOnInterfaceWithCglibProxy() throws InterruptedException {
public void findOnInterfaceWithCglibProxy() {
// Arrange
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigB.class);
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);
Expand All @@ -326,6 +325,21 @@ public void findOnInterfaceWithCglibProxy() throws InterruptedException {
ctx.close();
}

@Test
public void exceptionThrownWithBeanNotOfRequiredTypeRootCause() {
try {
new AnnotationConfigApplicationContext(JdkProxyConfiguration.class);
fail("Should have thrown exception");
}
catch (Throwable ex) {
ex.printStackTrace();
while (ex.getCause() != null) {
ex = ex.getCause();
}
Assert.assertThat(ex, instanceOf(BeanNotOfRequiredTypeException.class));
}
}


static class AsyncBeanWithExecutorQualifiedByName {

Expand Down Expand Up @@ -644,4 +658,26 @@ public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
}
}


@Configuration
@EnableAsync
@Import(UserConfiguration.class)
static class JdkProxyConfiguration {

@Bean
public AsyncBeanWithInterface asyncBean() {
return new AsyncBeanWithInterface();
}
}


@Configuration
static class UserConfiguration {

@Bean
public AsyncBeanUser user(AsyncBeanWithInterface bean) {
return new AsyncBeanUser(bean);
}
}

}

0 comments on commit 4bee507

Please sign in to comment.