-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConditionalTest.java
More file actions
73 lines (62 loc) · 2.32 KB
/
ConditionalTest.java
File metadata and controls
73 lines (62 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package tobySpringBoot.study;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.beans.BeanProperty;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
//import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
public class ConditionalTest {
@Test
void conditional() {
// // true
// ApplicationContextRunner contextRunner = new ApplicationContextRunner();
// contextRunner.withUserConfiguration(Config1.class)
// .run(context -> {
// assertThat(context).hasSingleBean(MyBean.class);
// assertThat(context).hasSingleBean(Config1.class);
// });
//
// // false
// new ApplicationContextRunner().withUserConfiguration(Config1.class)
// .run(context -> {
// assertThat(context).doesNotHaveBean(MyBean.class);
// assertThat(context).doesNotHaveBean(Config1.class);
// });
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Conditional(BooleanCondition.class)
@interface BooleanConditional {
boolean value();
}
@Configuration
@BooleanConditional(true)
static class Config1 { // <- static으로 선언하면 inner class 처럼 취급되지 않는다. 대신 상위 클래스가 마치 package 역할을 한다.
@Bean
MyBean myBean() {
return new MyBean();
}
}
@Configuration
@BooleanConditional(false)
static class Config2 {
@Bean
MyBean myBean() {
return new MyBean();
}
}
static class MyBean {
}
static class BooleanCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(BooleanConditional.class.getName());
return (Boolean) annotationAttributes.get("value");
}
}
}