Skip to content

Commit 0b6dccf

Browse files
committed
GH-190 - Introduce @EnableScenarios.
The dedicated annotation allows using Scenario as test parameter in @SpringBootTest-based integration tests, too. ScenarioParameterResolver now throws a better error message if a TransactionTemplate bean is missing from the ApplicationContext.
1 parent c9729bd commit 0b6dccf

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 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+
package org.springframework.modulith.test;
17+
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Inherited;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
27+
/**
28+
* Allows using {@link Scenario} as method parameter in integration tests.
29+
*
30+
* @author Oliver Drotbohm
31+
* @see Scenario
32+
*/
33+
@Documented
34+
@Inherited
35+
@Target(value = ElementType.TYPE)
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@ExtendWith(ScenarioParameterResolver.class)
38+
public @interface EnableScenarios {}

spring-modulith-test/src/main/java/org/springframework/modulith/test/ScenarioParameterResolver.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.jupiter.api.extension.ParameterContext;
2121
import org.junit.jupiter.api.extension.ParameterResolutionException;
2222
import org.junit.jupiter.api.extension.ParameterResolver;
23+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
24+
import org.springframework.context.ApplicationContext;
2325
import org.springframework.test.context.junit.jupiter.SpringExtension;
2426
import org.springframework.transaction.support.TransactionTemplate;
2527

@@ -30,6 +32,8 @@
3032
*/
3133
class ScenarioParameterResolver implements ParameterResolver, BeforeAllCallback {
3234

35+
private static final String MISSING_TRANSACTION_TEMPLATE = "To use a Scenario in an integration test you need to define a bean of type TransactionTemplate! Please check your ApplicationContext setup.";
36+
3337
private final PublishedEventsParameterResolver delegate;
3438

3539
/**
@@ -70,9 +74,18 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
7074
throws ParameterResolutionException {
7175

7276
var context = SpringExtension.getApplicationContext(extensionContext);
73-
var operations = context.getBean(TransactionTemplate.class);
77+
var operations = resolveTransactionTemplate(context);
7478
var events = (AssertablePublishedEvents) delegate.resolveParameter(parameterContext, extensionContext);
7579

7680
return new Scenario(operations, context, events);
7781
}
82+
83+
private TransactionTemplate resolveTransactionTemplate(ApplicationContext context) {
84+
85+
try {
86+
return context.getBean(TransactionTemplate.class);
87+
} catch (NoSuchBeanDefinitionException o_O) {
88+
throw new ParameterResolutionException(MISSING_TRANSACTION_TEMPLATE, o_O);
89+
}
90+
}
7891
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 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+
package org.springframework.modulith.test;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import example.TestConfiguration;
22+
23+
import org.junit.jupiter.api.Nested;
24+
import org.junit.jupiter.api.Test;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.modulith.test.EnableScenarioIntegrationTests.TransactionTemplateTestConfiguration;
29+
import org.springframework.transaction.PlatformTransactionManager;
30+
import org.springframework.transaction.support.TransactionTemplate;
31+
32+
/**
33+
* Integration tests for the usage of {@link EnableScenarios}.
34+
*
35+
* @author Oliver Drotbohm
36+
*/
37+
@Nested
38+
@EnableScenarios
39+
@SpringBootTest(classes = { TestConfiguration.class, TransactionTemplateTestConfiguration.class })
40+
class EnableScenarioIntegrationTests {
41+
42+
@Configuration
43+
static class TransactionTemplateTestConfiguration {
44+
45+
@Bean
46+
TransactionTemplate transactionTemplate() {
47+
return new TransactionTemplate(mock(PlatformTransactionManager.class));
48+
}
49+
}
50+
51+
@Test // GH-190
52+
void injectsScenario(Scenario scenario) {
53+
assertThat(scenario).isNotNull();
54+
}
55+
}

0 commit comments

Comments
 (0)