Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 0887378

Browse files
committed
Add unit tests for TestExecutionListenerFactoriesCodeContributor
See gh-1217
1 parent 6fbbb08 commit 0887378

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2019-2021 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.aot.factories;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.function.Consumer;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.mockito.ArgumentCaptor;
26+
import org.mockito.Mockito;
27+
28+
import org.springframework.aot.TypeSystemExtension;
29+
import org.springframework.aot.build.context.BuildContext;
30+
import org.springframework.aot.factories.fixtures.DemoTestExecutionListener;
31+
import org.springframework.aot.factories.fixtures.TestFactory;
32+
import org.springframework.core.type.classreading.TypeSystem;
33+
import org.springframework.nativex.AotOptions;
34+
import org.springframework.nativex.domain.reflect.ClassDescriptor;
35+
import org.springframework.nativex.domain.reflect.MethodDescriptor;
36+
import org.springframework.nativex.domain.reflect.ReflectionDescriptor;
37+
import org.springframework.test.context.TestExecutionListener;
38+
39+
/**
40+
* Unit tests for {@link TestExecutionListenerFactoriesCodeContributor}.
41+
*
42+
* @author Sam Brannen
43+
*/
44+
@ExtendWith(TypeSystemExtension.class)
45+
class TestExecutionListenerFactoriesCodeContributorTests {
46+
47+
private static final String TEL = TestExecutionListener.class.getName();
48+
49+
private final TestExecutionListenerFactoriesCodeContributor contributor =
50+
new TestExecutionListenerFactoriesCodeContributor();
51+
52+
53+
@Test
54+
void shouldNotContributeIfNotTestExecutionListener(TypeSystem typeSystem) {
55+
SpringFactory factory = SpringFactory.resolve(TestFactory.class.getName(),
56+
DemoTestExecutionListener.class.getName(), typeSystem);
57+
assertThat(this.contributor.canContribute(factory)).isFalse();
58+
}
59+
60+
@Test
61+
void shouldContributeIfTestExecutionListener(TypeSystem typeSystem) {
62+
SpringFactory factory = SpringFactory.resolve(TEL, DemoTestExecutionListener.class.getName(), typeSystem);
63+
assertThat(this.contributor.canContribute(factory)).isTrue();
64+
}
65+
66+
@Test
67+
@SuppressWarnings("unchecked")
68+
void shouldContributeFactoryNamesAndReflectionConfig(TypeSystem typeSystem) {
69+
CodeGenerator code = new CodeGenerator(new AotOptions());
70+
SpringFactory factory = SpringFactory.resolve(TEL, DemoTestExecutionListener.class.getName(), typeSystem);
71+
BuildContext buildContext = Mockito.mock(BuildContext.class);
72+
73+
this.contributor.contribute(factory, code, buildContext);
74+
75+
assertThat(code.generateStaticSpringFactories()).asString()
76+
.contains("names.add(TestExecutionListener.class, \"org.springframework.aot.factories.fixtures.DemoTestExecutionListener\");");
77+
78+
ArgumentCaptor<Consumer<ReflectionDescriptor>> consumerArgumentCaptor = ArgumentCaptor.forClass(Consumer.class);
79+
Mockito.verify(buildContext).describeReflection(consumerArgumentCaptor.capture());
80+
Consumer<ReflectionDescriptor> reflectionConsumer = consumerArgumentCaptor.getValue();
81+
82+
ReflectionDescriptor reflectionDescriptor = Mockito.mock(ReflectionDescriptor.class);
83+
reflectionConsumer.accept(reflectionDescriptor);
84+
85+
ArgumentCaptor<ClassDescriptor> classDescriptorArgumentCaptor = ArgumentCaptor.forClass(ClassDescriptor.class);
86+
Mockito.verify(reflectionDescriptor).add(classDescriptorArgumentCaptor.capture());
87+
ClassDescriptor classDescriptor = classDescriptorArgumentCaptor.getValue();
88+
assertThat(classDescriptor.getName()).isEqualTo(DemoTestExecutionListener.class.getName());
89+
assertThat(classDescriptor.getMethods()).containsExactly(MethodDescriptor.defaultConstructor());
90+
}
91+
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2019-2021 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.aot.factories.fixtures;
18+
19+
import org.springframework.test.context.TestExecutionListener;
20+
21+
public class DemoTestExecutionListener implements TestExecutionListener {
22+
}

0 commit comments

Comments
 (0)