Skip to content

Commit e32c6e5

Browse files
Thangtq211pedja4
authored andcommitted
BAEL-635 Overview of Spring 5 (eugenp#1633)
* Overview of Spring 5 * Overview of Spring 5 * BAEL-635 Formatting
1 parent 3286018 commit e32c6e5

13 files changed

Lines changed: 684 additions & 137 deletions

‎spring-5/pom.xml‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.baeldung</groupId>
@@ -15,7 +15,7 @@
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
1717
<version>2.0.0.BUILD-SNAPSHOT</version>
18-
<relativePath /> <!-- lookup parent from repository -->
18+
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

2121
<dependencies>
@@ -58,11 +58,39 @@
5858
<artifactId>h2</artifactId>
5959
<scope>runtime</scope>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.junit.jupiter</groupId>
63+
<artifactId>junit-jupiter-api</artifactId>
64+
<version>${junit.jupiter.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework</groupId>
68+
<artifactId>spring-test</artifactId>
69+
<version>${spring.test.version}</version>
70+
</dependency>
6171
<dependency>
6272
<groupId>org.springframework.boot</groupId>
6373
<artifactId>spring-boot-starter-test</artifactId>
6474
<scope>test</scope>
6575
</dependency>
76+
<dependency>
77+
<groupId>org.junit.jupiter</groupId>
78+
<artifactId>junit-jupiter-engine</artifactId>
79+
<version>${junit.jupiter.version}</version>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.junit.platform</groupId>
84+
<artifactId>junit-platform-surefire-provider</artifactId>
85+
<version>${junit.platform.version}</version>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.junit.platform</groupId>
90+
<artifactId>junit-platform-runner</artifactId>
91+
<version>${junit.platform.version}</version>
92+
<scope>test</scope>
93+
</dependency>
6694
</dependencies>
6795

6896
<build>
@@ -92,7 +120,7 @@
92120
<plugin>
93121
<groupId>org.apache.maven.plugins</groupId>
94122
<artifactId>maven-surefire-plugin</artifactId>
95-
<version>2.19.1</version>
123+
<version>${maven-surefire-plugin.version}</version>
96124
<configuration>
97125
<parallel>methods</parallel>
98126
<useUnlimitedThreads>true</useUnlimitedThreads>
@@ -143,6 +171,10 @@
143171
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
144172
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
145173
<java.version>1.8</java.version>
174+
<junit.platform.version>1.0.0-M3</junit.platform.version>
175+
<junit.jupiter.version>5.0.0-M3</junit.jupiter.version>
176+
<spring.test.version>4.3.7.RELEASE</spring.test.version>
177+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
146178
</properties>
147179

148180
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.jupiter;
2+
3+
import org.springframework.core.MethodParameter;
4+
import org.springframework.core.annotation.SynthesizingMethodParameter;
5+
import org.springframework.util.Assert;
6+
7+
import java.lang.reflect.Constructor;
8+
import java.lang.reflect.Executable;
9+
import java.lang.reflect.Method;
10+
import java.lang.reflect.Parameter;
11+
12+
abstract class MethodParameterFactory {
13+
14+
private MethodParameterFactory() {
15+
}
16+
17+
public static MethodParameter createMethodParameter(Parameter parameter) {
18+
Assert.notNull(parameter, "Parameter must not be null");
19+
Executable executable = parameter.getDeclaringExecutable();
20+
if (executable instanceof Method) {
21+
return new MethodParameter((Method) executable, getIndex(parameter));
22+
}
23+
return new MethodParameter((Constructor<?>) executable, getIndex(parameter));
24+
}
25+
26+
public static SynthesizingMethodParameter createSynthesizingMethodParameter(Parameter parameter) {
27+
Assert.notNull(parameter, "Parameter must not be null");
28+
Executable executable = parameter.getDeclaringExecutable();
29+
if (executable instanceof Method) {
30+
return new SynthesizingMethodParameter((Method) executable, getIndex(parameter));
31+
}
32+
throw new UnsupportedOperationException("Cannot create a SynthesizingMethodParameter for a constructor parameter: " + parameter);
33+
}
34+
35+
private static int getIndex(Parameter parameter) {
36+
Assert.notNull(parameter, "Parameter must not be null");
37+
Executable executable = parameter.getDeclaringExecutable();
38+
Parameter[] parameters = executable.getParameters();
39+
for (int i = 0; i < parameters.length; i++) {
40+
if (parameters[i] == parameter) {
41+
return i;
42+
}
43+
}
44+
throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]", parameter, executable.toGenericString()));
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.jupiter;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.beans.factory.config.DependencyDescriptor;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.core.MethodParameter;
9+
import org.springframework.core.annotation.AnnotatedElementUtils;
10+
11+
import java.lang.annotation.Annotation;
12+
import java.lang.reflect.AnnotatedElement;
13+
import java.lang.reflect.Method;
14+
import java.lang.reflect.Parameter;
15+
import java.util.Optional;
16+
17+
import static org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation;
18+
19+
abstract class ParameterAutowireUtils {
20+
21+
private ParameterAutowireUtils() {
22+
}
23+
24+
public static boolean isAutowirable(Parameter parameter) {
25+
return ApplicationContext.class.isAssignableFrom(parameter.getType()) || hasAnnotation(parameter, Autowired.class) || hasAnnotation(parameter, Qualifier.class) || hasAnnotation(parameter, Value.class);
26+
}
27+
28+
public static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {
29+
30+
boolean required = findMergedAnnotation(parameter, Autowired.class)
31+
.map(Autowired::required)
32+
.orElse(true);
33+
MethodParameter methodParameter = (parameter.getDeclaringExecutable() instanceof Method ? MethodParameterFactory.createSynthesizingMethodParameter(parameter) : MethodParameterFactory.createMethodParameter(parameter));
34+
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
35+
descriptor.setContainingClass(containingClass);
36+
37+
return applicationContext
38+
.getAutowireCapableBeanFactory()
39+
.resolveDependency(descriptor, null);
40+
}
41+
42+
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
43+
44+
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
45+
}
46+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.baeldung.jupiter;
2+
3+
import org.junit.jupiter.api.extension.*;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.core.annotation.AnnotatedElementUtils;
7+
import org.springframework.test.context.TestContextManager;
8+
import org.springframework.util.Assert;
9+
10+
import java.lang.reflect.Constructor;
11+
import java.lang.reflect.Executable;
12+
import java.lang.reflect.Method;
13+
import java.lang.reflect.Parameter;
14+
15+
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver {
16+
17+
private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(SpringExtension.class);
18+
19+
@Override
20+
public void beforeAll(ContainerExtensionContext context) throws Exception {
21+
getTestContextManager(context).beforeTestClass();
22+
}
23+
24+
@Override
25+
public void afterAll(ContainerExtensionContext context) throws Exception {
26+
try {
27+
getTestContextManager(context).afterTestClass();
28+
} finally {
29+
context
30+
.getStore(namespace)
31+
.remove(context
32+
.getTestClass()
33+
.get());
34+
}
35+
}
36+
37+
@Override
38+
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
39+
getTestContextManager(context).prepareTestInstance(testInstance);
40+
}
41+
42+
@Override
43+
public void beforeEach(TestExtensionContext context) throws Exception {
44+
Object testInstance = context.getTestInstance();
45+
Method testMethod = context
46+
.getTestMethod()
47+
.get();
48+
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
49+
}
50+
51+
@Override
52+
public void afterEach(TestExtensionContext context) throws Exception {
53+
Object testInstance = context.getTestInstance();
54+
Method testMethod = context
55+
.getTestMethod()
56+
.get();
57+
Throwable testException = context
58+
.getTestException()
59+
.orElse(null);
60+
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
61+
}
62+
63+
@Override
64+
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) {
65+
Parameter parameter = parameterContext.getParameter();
66+
Executable executable = parameter.getDeclaringExecutable();
67+
return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter);
68+
}
69+
70+
@Override
71+
public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) {
72+
Parameter parameter = parameterContext.getParameter();
73+
Class<?> testClass = extensionContext
74+
.getTestClass()
75+
.get();
76+
ApplicationContext applicationContext = getApplicationContext(extensionContext);
77+
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
78+
}
79+
80+
private ApplicationContext getApplicationContext(ExtensionContext context) {
81+
return getTestContextManager(context)
82+
.getTestContext()
83+
.getApplicationContext();
84+
}
85+
86+
private TestContextManager getTestContextManager(ExtensionContext context) {
87+
Assert.notNull(context, "ExtensionContext must not be null");
88+
Class<?> testClass = context
89+
.getTestClass()
90+
.get();
91+
ExtensionContext.Store store = context.getStore(namespace);
92+
return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
93+
}
94+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.jupiter;
2+
3+
import org.junit.jupiter.api.extension.ExtendWith;
4+
import org.springframework.context.ApplicationContextInitializer;
5+
import org.springframework.context.ConfigurableApplicationContext;
6+
import org.springframework.core.annotation.AliasFor;
7+
import org.springframework.test.context.ContextConfiguration;
8+
9+
import java.lang.annotation.*;
10+
11+
@ExtendWith(SpringExtension.class)
12+
@ContextConfiguration
13+
@Documented
14+
@Inherited
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Target(ElementType.TYPE)
17+
public @interface SpringJUnit5Config {
18+
19+
@AliasFor(annotation = ContextConfiguration.class, attribute = "classes")
20+
Class<?>[] value() default {};
21+
22+
@AliasFor(annotation = ContextConfiguration.class)
23+
Class<?>[] classes() default {};
24+
25+
@AliasFor(annotation = ContextConfiguration.class)
26+
String[] locations() default {};
27+
28+
@AliasFor(annotation = ContextConfiguration.class)
29+
Class<? extends ApplicationContextInitializer<?
30+
extends ConfigurableApplicationContext>>[] initializers() default {};
31+
32+
@AliasFor(annotation = ContextConfiguration.class)
33+
boolean inheritLocations() default true;
34+
35+
@AliasFor(annotation = ContextConfiguration.class)
36+
boolean inheritInitializers() default true;
37+
38+
@AliasFor(annotation = ContextConfiguration.class)
39+
String name() default "";
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.jupiter;
2+
3+
import com.baeldung.web.reactive.Task;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
7+
8+
@Configuration
9+
public class TestConfig {
10+
11+
@Bean
12+
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
13+
return new PropertySourcesPlaceholderConfigurer();
14+
}
15+
16+
@Bean
17+
Task taskName() {
18+
return new Task("taskName", 1);
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.web.reactive;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public class Task {
6+
7+
private final String name;
8+
9+
private final int id;
10+
11+
public Task(@JsonProperty("name") String name, @JsonProperty("id") int id) {
12+
this.name = name;
13+
this.id = id;
14+
}
15+
16+
public String getName() {
17+
return this.name;
18+
}
19+
20+
public int getId() {
21+
return this.id;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "Task{" +
27+
"name='" + name + '\'' +
28+
", id=" + id +
29+
'}';
30+
}
31+
}

0 commit comments

Comments
 (0)