Skip to content

Commit 6299a9d

Browse files
committed
Add tests for SimpleInstantiationStrategy
1 parent 796080a commit 6299a9d

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.support;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.BeanInstantiationException;
24+
import org.springframework.util.ReflectionUtils;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
28+
29+
/**
30+
* Tests for {@link SimpleInstantiationStrategy}.
31+
*
32+
* @author Stephane Nicoll
33+
*/
34+
class SimpleInstantiationStrategyTests {
35+
36+
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
37+
38+
private final SimpleInstantiationStrategy strategy = new SimpleInstantiationStrategy();
39+
40+
@Test
41+
void instantiateWithNoArg() {
42+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
43+
Object simpleBean = instantiate(bd, new SampleFactory(),
44+
method(SampleFactory.class, "simpleBean"));
45+
assertThat(simpleBean).isEqualTo("Hello");
46+
}
47+
48+
@Test
49+
void instantiateWitArgs() {
50+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
51+
Object simpleBean = instantiate(bd, new SampleFactory(),
52+
method(SampleFactory.class, "beanWithTwoArgs"), "Test", 42);
53+
assertThat(simpleBean).isEqualTo("Test42");
54+
}
55+
56+
@Test
57+
void instantiateWithNullValueReturnsNullBean() {
58+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
59+
Object simpleBean = instantiate(bd, new SampleFactory(),
60+
method(SampleFactory.class, "cloneBean"), new Object[] { null });
61+
assertThat(simpleBean).isNotNull().isInstanceOf(NullBean.class);
62+
}
63+
64+
@Test
65+
void instantiateWithArgumentTypeMismatch() {
66+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
67+
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> instantiate(
68+
bd, new SampleFactory(),
69+
method(SampleFactory.class, "beanWithTwoArgs"), 42, "Test"))
70+
.withMessageContaining("Illegal arguments to factory method 'beanWithTwoArgs'")
71+
.withMessageContaining("args: 42,Test");
72+
}
73+
74+
@Test
75+
void instantiateWithException() {
76+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
77+
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> instantiate(
78+
bd, new SampleFactory(),
79+
method(SampleFactory.class, "errorBean"), "This a test message"))
80+
.withMessageContaining("Factory method 'errorBean' threw exception")
81+
.withMessageContaining("This a test message")
82+
.havingCause().isInstanceOf(IllegalStateException.class).withMessage("This a test message");
83+
}
84+
85+
private Object instantiate(RootBeanDefinition bd, Object factory, Method method, Object... args) {
86+
return this.strategy.instantiate(bd, "simpleBean", this.beanFactory,
87+
factory, method, args);
88+
}
89+
90+
private static Method method(Class<?> target, String methodName) {
91+
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(
92+
target, method -> methodName.equals(method.getName()));
93+
assertThat(methods).as("No unique method named " + methodName + " found of " + target.getName())
94+
.hasSize(1);
95+
return methods[0];
96+
}
97+
98+
99+
static class SampleFactory {
100+
101+
String simpleBean() {
102+
return "Hello";
103+
}
104+
105+
String beanWithTwoArgs(String first, Integer second) {
106+
return first + second;
107+
}
108+
109+
String cloneBean(String arg) {
110+
return arg;
111+
}
112+
113+
String errorBean(String msg) {
114+
throw new IllegalStateException(msg);
115+
}
116+
117+
}
118+
}

0 commit comments

Comments
 (0)