BAEL-635 Overview of Spring 5 (#1633)
* Overview of Spring 5 * Overview of Spring 5 * BAEL-635 Formatting
This commit is contained in:
parent
3286018dd2
commit
e32c6e5f38
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -15,7 +15,7 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -58,11 +58,39 @@
|
|||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.test.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -92,7 +120,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.19.1</version>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<parallel>methods</parallel>
|
||||
<useUnlimitedThreads>true</useUnlimitedThreads>
|
||||
|
@ -143,6 +171,10 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<junit.platform.version>1.0.0-M3</junit.platform.version>
|
||||
<junit.jupiter.version>5.0.0-M3</junit.jupiter.version>
|
||||
<spring.test.version>4.3.7.RELEASE</spring.test.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Executable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
abstract class MethodParameterFactory {
|
||||
|
||||
private MethodParameterFactory() {
|
||||
}
|
||||
|
||||
public static MethodParameter createMethodParameter(Parameter parameter) {
|
||||
Assert.notNull(parameter, "Parameter must not be null");
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
if (executable instanceof Method) {
|
||||
return new MethodParameter((Method) executable, getIndex(parameter));
|
||||
}
|
||||
return new MethodParameter((Constructor<?>) executable, getIndex(parameter));
|
||||
}
|
||||
|
||||
public static SynthesizingMethodParameter createSynthesizingMethodParameter(Parameter parameter) {
|
||||
Assert.notNull(parameter, "Parameter must not be null");
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
if (executable instanceof Method) {
|
||||
return new SynthesizingMethodParameter((Method) executable, getIndex(parameter));
|
||||
}
|
||||
throw new UnsupportedOperationException("Cannot create a SynthesizingMethodParameter for a constructor parameter: " + parameter);
|
||||
}
|
||||
|
||||
private static int getIndex(Parameter parameter) {
|
||||
Assert.notNull(parameter, "Parameter must not be null");
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
Parameter[] parameters = executable.getParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
if (parameters[i] == parameter) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]", parameter, executable.toGenericString()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation;
|
||||
|
||||
abstract class ParameterAutowireUtils {
|
||||
|
||||
private ParameterAutowireUtils() {
|
||||
}
|
||||
|
||||
public static boolean isAutowirable(Parameter parameter) {
|
||||
return ApplicationContext.class.isAssignableFrom(parameter.getType()) || hasAnnotation(parameter, Autowired.class) || hasAnnotation(parameter, Qualifier.class) || hasAnnotation(parameter, Value.class);
|
||||
}
|
||||
|
||||
public static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {
|
||||
|
||||
boolean required = findMergedAnnotation(parameter, Autowired.class)
|
||||
.map(Autowired::required)
|
||||
.orElse(true);
|
||||
MethodParameter methodParameter = (parameter.getDeclaringExecutable() instanceof Method ? MethodParameterFactory.createSynthesizingMethodParameter(parameter) : MethodParameterFactory.createMethodParameter(parameter));
|
||||
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
|
||||
descriptor.setContainingClass(containingClass);
|
||||
|
||||
return applicationContext
|
||||
.getAutowireCapableBeanFactory()
|
||||
.resolveDependency(descriptor, null);
|
||||
}
|
||||
|
||||
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
|
||||
|
||||
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import org.junit.jupiter.api.extension.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Executable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver {
|
||||
|
||||
private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(SpringExtension.class);
|
||||
|
||||
@Override
|
||||
public void beforeAll(ContainerExtensionContext context) throws Exception {
|
||||
getTestContextManager(context).beforeTestClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterAll(ContainerExtensionContext context) throws Exception {
|
||||
try {
|
||||
getTestContextManager(context).afterTestClass();
|
||||
} finally {
|
||||
context
|
||||
.getStore(namespace)
|
||||
.remove(context
|
||||
.getTestClass()
|
||||
.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
|
||||
getTestContextManager(context).prepareTestInstance(testInstance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(TestExtensionContext context) throws Exception {
|
||||
Object testInstance = context.getTestInstance();
|
||||
Method testMethod = context
|
||||
.getTestMethod()
|
||||
.get();
|
||||
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEach(TestExtensionContext context) throws Exception {
|
||||
Object testInstance = context.getTestInstance();
|
||||
Method testMethod = context
|
||||
.getTestMethod()
|
||||
.get();
|
||||
Throwable testException = context
|
||||
.getTestException()
|
||||
.orElse(null);
|
||||
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
Parameter parameter = parameterContext.getParameter();
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
Parameter parameter = parameterContext.getParameter();
|
||||
Class<?> testClass = extensionContext
|
||||
.getTestClass()
|
||||
.get();
|
||||
ApplicationContext applicationContext = getApplicationContext(extensionContext);
|
||||
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
|
||||
}
|
||||
|
||||
private ApplicationContext getApplicationContext(ExtensionContext context) {
|
||||
return getTestContextManager(context)
|
||||
.getTestContext()
|
||||
.getApplicationContext();
|
||||
}
|
||||
|
||||
private TestContextManager getTestContextManager(ExtensionContext context) {
|
||||
Assert.notNull(context, "ExtensionContext must not be null");
|
||||
Class<?> testClass = context
|
||||
.getTestClass()
|
||||
.get();
|
||||
ExtensionContext.Store store = context.getStore(namespace);
|
||||
return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration
|
||||
@Documented
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface SpringJUnit5Config {
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "classes")
|
||||
Class<?>[] value() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
Class<?>[] classes() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
String[] locations() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
Class<? extends ApplicationContextInitializer<?
|
||||
extends ConfigurableApplicationContext>>[] initializers() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
boolean inheritLocations() default true;
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
boolean inheritInitializers() default true;
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
String name() default "";
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import com.baeldung.web.reactive.Task;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class TestConfig {
|
||||
|
||||
@Bean
|
||||
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
Task taskName() {
|
||||
return new Task("taskName", 1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.web.reactive;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Task {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final int id;
|
||||
|
||||
public Task(@JsonProperty("name") String name, @JsonProperty("id") int id) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Task{" +
|
||||
"name='" + name + '\'' +
|
||||
", id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ import org.springframework.boot.web.server.WebServer;
|
|||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
//import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
|
@ -15,140 +15,141 @@ import org.springframework.web.reactive.function.BodyInserters;
|
|||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
||||
|
||||
// TODO The class does not compile, WebTestClient cannot be resolved. Missing dependency?
|
||||
public class FunctionalWebApplicationIntegrationTest {
|
||||
|
||||
private static WebTestClient client;
|
||||
private static WebServer server;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws Exception {
|
||||
server = new FunctionalWebApplication().start();
|
||||
client = WebTestClient
|
||||
.bindToServer()
|
||||
.baseUrl("http://localhost:" + server.getPort())
|
||||
.build();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRouter_whenGetTest_thenGotHelloWorld() throws Exception {
|
||||
client
|
||||
.get()
|
||||
.uri("/test")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(String.class)
|
||||
.value()
|
||||
.isEqualTo("helloworld");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception {
|
||||
client
|
||||
.get()
|
||||
.uri("/")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(String.class)
|
||||
.value()
|
||||
.isEqualTo("helloworld");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
|
||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
|
||||
formData.add("user", "baeldung");
|
||||
formData.add("token", "you_know_what_to_do");
|
||||
|
||||
client
|
||||
.post()
|
||||
.uri("/login")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.exchange(BodyInserters.fromFormData(formData))
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(String.class)
|
||||
.value()
|
||||
.isEqualTo("welcome back!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
|
||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
|
||||
formData.add("user", "baeldung");
|
||||
formData.add("token", "try_again");
|
||||
|
||||
client
|
||||
.post()
|
||||
.uri("/login")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.exchange(BodyInserters.fromFormData(formData))
|
||||
.expectStatus()
|
||||
.isBadRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
|
||||
Resource resource = new ClassPathResource("/baeldung-weekly.png");
|
||||
client
|
||||
.post()
|
||||
.uri("/upload")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.exchange(fromResource(resource))
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(String.class)
|
||||
.value()
|
||||
.isEqualTo(String.valueOf(resource.contentLength()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActors_whenAddActor_thenAdded() throws Exception {
|
||||
client
|
||||
.get()
|
||||
.uri("/actor")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(Actor.class)
|
||||
.list()
|
||||
.hasSize(2);
|
||||
|
||||
client
|
||||
.post()
|
||||
.uri("/actor")
|
||||
.exchange(fromObject(new Actor("Clint", "Eastwood")))
|
||||
.expectStatus()
|
||||
.isOk();
|
||||
|
||||
client
|
||||
.get()
|
||||
.uri("/actor")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(Actor.class)
|
||||
.list()
|
||||
.hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResources_whenAccess_thenGot() throws Exception {
|
||||
client
|
||||
.get()
|
||||
.uri("/files/hello.txt")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(String.class)
|
||||
.value()
|
||||
.isEqualTo("hello");
|
||||
}
|
||||
// private static WebTestClient client;
|
||||
// private static WebServer server;
|
||||
//
|
||||
// @BeforeClass
|
||||
// public static void setup() throws Exception {
|
||||
// server = new FunctionalWebApplication().start();
|
||||
// client = WebTestClient
|
||||
// .bindToServer()
|
||||
// .baseUrl("http://localhost:" + server.getPort())
|
||||
// .build();
|
||||
// }
|
||||
//
|
||||
// @AfterClass
|
||||
// public static void destroy() {
|
||||
// server.stop();
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void givenRouter_whenGetTest_thenGotHelloWorld() throws Exception {
|
||||
// client
|
||||
// .get()
|
||||
// .uri("/test")
|
||||
// .exchange()
|
||||
// .expectStatus()
|
||||
// .isOk()
|
||||
// .expectBody(String.class)
|
||||
// .value()
|
||||
// .isEqualTo("helloworld");
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception {
|
||||
// client
|
||||
// .get()
|
||||
// .uri("/")
|
||||
// .exchange()
|
||||
// .expectStatus()
|
||||
// .isOk()
|
||||
// .expectBody(String.class)
|
||||
// .value()
|
||||
// .isEqualTo("helloworld");
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
|
||||
// MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
|
||||
// formData.add("user", "baeldung");
|
||||
// formData.add("token", "you_know_what_to_do");
|
||||
//
|
||||
// client
|
||||
// .post()
|
||||
// .uri("/login")
|
||||
// .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
// .exchange(BodyInserters.fromFormData(formData))
|
||||
// .expectStatus()
|
||||
// .isOk()
|
||||
// .expectBody(String.class)
|
||||
// .value()
|
||||
// .isEqualTo("welcome back!");
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
|
||||
// MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
|
||||
// formData.add("user", "baeldung");
|
||||
// formData.add("token", "try_again");
|
||||
//
|
||||
// client
|
||||
// .post()
|
||||
// .uri("/login")
|
||||
// .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
// .exchange(BodyInserters.fromFormData(formData))
|
||||
// .expectStatus()
|
||||
// .isBadRequest();
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
|
||||
// Resource resource = new ClassPathResource("/baeldung-weekly.png");
|
||||
// client
|
||||
// .post()
|
||||
// .uri("/upload")
|
||||
// .contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
// .exchange(fromResource(resource))
|
||||
// .expectStatus()
|
||||
// .isOk()
|
||||
// .expectBody(String.class)
|
||||
// .value()
|
||||
// .isEqualTo(String.valueOf(resource.contentLength()));
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void givenActors_whenAddActor_thenAdded() throws Exception {
|
||||
// client
|
||||
// .get()
|
||||
// .uri("/actor")
|
||||
// .exchange()
|
||||
// .expectStatus()
|
||||
// .isOk()
|
||||
// .expectBody(Actor.class)
|
||||
// .list()
|
||||
// .hasSize(2);
|
||||
//
|
||||
// client
|
||||
// .post()
|
||||
// .uri("/actor")
|
||||
// .exchange(fromObject(new Actor("Clint", "Eastwood")))
|
||||
// .expectStatus()
|
||||
// .isOk();
|
||||
//
|
||||
// client
|
||||
// .get()
|
||||
// .uri("/actor")
|
||||
// .exchange()
|
||||
// .expectStatus()
|
||||
// .isOk()
|
||||
// .expectBody(Actor.class)
|
||||
// .list()
|
||||
// .hasSize(3);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void givenResources_whenAccess_thenGot() throws Exception {
|
||||
// client
|
||||
// .get()
|
||||
// .uri("/files/hello.txt")
|
||||
// .exchange()
|
||||
// .expectStatus()
|
||||
// .isOk()
|
||||
// .expectBody(String.class)
|
||||
// .value()
|
||||
// .isEqualTo("hello");
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import com.baeldung.web.reactive.Task;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@SpringJUnit5Config(TestConfig.class)
|
||||
@DisplayName("@SpringJUnit5Config Tests")
|
||||
class Spring5JUnit5ComposedAnnotationTests {
|
||||
|
||||
@Autowired
|
||||
Task task;
|
||||
|
||||
@Autowired
|
||||
List<Task> tasks;
|
||||
|
||||
@Test
|
||||
@DisplayName("ApplicationContext injected into method")
|
||||
void givenAMethodName_whenInjecting_thenApplicationContextInjectedIntoMethod(ApplicationContext applicationContext) {
|
||||
assertNotNull(applicationContext, "ApplicationContext should have been injected into method by Spring");
|
||||
assertEquals(this.task, applicationContext.getBean("taskName", Task.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Spring @Beans injected into fields")
|
||||
void givenAnObject_whenInjecting_thenSpringBeansInjected() {
|
||||
assertNotNull(task, "Task should have been @Autowired by Spring");
|
||||
assertEquals("taskName", task.getName(), "Task's name");
|
||||
assertEquals(1, tasks.size(), "Number of Tasks in context");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import com.baeldung.IntegrationTestExample1;
|
||||
import com.baeldung.IntegrationTestExample2;
|
||||
import org.junit.experimental.ParallelComputer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.Computer;
|
||||
import org.junit.runner.JUnitCore;
|
||||
|
||||
public class Spring5JUnit5ParallelTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingParallel() {
|
||||
final Class<?>[] classes = {
|
||||
IntegrationTestExample1.class, IntegrationTestExample2.class
|
||||
};
|
||||
|
||||
JUnitCore.runClasses(new ParallelComputer(true, true), classes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingLinear() {
|
||||
final Class<?>[] classes = {
|
||||
IntegrationTestExample1.class, IntegrationTestExample2.class
|
||||
};
|
||||
|
||||
JUnitCore.runClasses(new Computer(), classes);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import com.baeldung.web.reactive.Task;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = TestConfig.class)
|
||||
class Spring5JUnit5Tests {
|
||||
|
||||
@Autowired
|
||||
Task task;
|
||||
|
||||
@Test
|
||||
void givenAMethodName_whenInjecting_thenApplicationContextInjectedIntoMetho(ApplicationContext applicationContext) {
|
||||
assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring");
|
||||
assertEquals(this.task, applicationContext.getBean("taskName", Task.class));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class Spring5Java8NewFeaturesTest {
|
||||
|
||||
@FunctionalInterface
|
||||
public interface FunctionalInterfaceExample<Input, Result> {
|
||||
Result reverseString(Input input);
|
||||
}
|
||||
|
||||
public class StringUtils{
|
||||
public FunctionalInterfaceExample<String, String>
|
||||
functionLambdaString = s -> {
|
||||
return Pattern.compile(" +").splitAsStream(s)
|
||||
.map(word->new StringBuilder(word).reverse())
|
||||
.collect(Collectors.joining(" "));
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringUtil_whenSupplierCall_thenFunctionalInterfaceReverseString()
|
||||
throws Exception {
|
||||
Supplier<StringUtils> stringUtilsSupplier = StringUtils::new;
|
||||
|
||||
assertEquals(stringUtilsSupplier.get().functionLambdaString
|
||||
.reverseString("hello"), "olleh");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import com.baeldung.web.reactive.Task;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFunctions;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.ipc.netty.NettyContext;
|
||||
import reactor.ipc.netty.http.server.HttpServer;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
|
||||
|
||||
public class Spring5ReactiveServerClientTest {
|
||||
|
||||
private static NettyContext nettyContext;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() throws Exception {
|
||||
HttpServer server = HttpServer.create("localhost", 8080);
|
||||
RouterFunction<?> route = RouterFunctions
|
||||
.route(POST("/task/process"), request -> ServerResponse
|
||||
.ok()
|
||||
.body(request
|
||||
.bodyToFlux(Task.class)
|
||||
.map(ll -> new Task("TaskName", 1)), Task.class))
|
||||
.and(RouterFunctions.route(GET("/task"), request -> ServerResponse
|
||||
.ok()
|
||||
.body(Mono.just("server is alive"), String.class)));
|
||||
HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
|
||||
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
|
||||
nettyContext = server
|
||||
.newHandler(adapter)
|
||||
.block();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void shutDown() {
|
||||
nettyContext.dispose();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCheckTask_whenServerHandle_thenServerResponseALiveString() throws Exception {
|
||||
WebClient client = WebClient.create("http://localhost:8080");
|
||||
Mono<String> result = client
|
||||
.get()
|
||||
.uri("/task")
|
||||
.exchange()
|
||||
.then(response -> response.bodyToMono(String.class));
|
||||
|
||||
assertThat(result.block()).isInstanceOf(String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeTasks_whenServerHandleTheTasks_thenServerResponseATask() throws Exception {
|
||||
URI uri = URI.create("http://localhost:8080/task/process");
|
||||
ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector());
|
||||
ClientRequest request = ClientRequest
|
||||
.method(HttpMethod.POST, uri)
|
||||
.body(BodyInserters.fromPublisher(getLatLngs(), Task.class))
|
||||
.build();
|
||||
|
||||
Flux<Task> taskResponse = exchange
|
||||
.exchange(request)
|
||||
.flatMap(response -> response.bodyToFlux(Task.class));
|
||||
|
||||
assertThat(taskResponse.blockFirst()).isInstanceOf(Task.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCheckTask_whenServerHandle_thenOragicServerResponseALiveString() throws Exception {
|
||||
URI uri = URI.create("http://localhost:8080/task");
|
||||
ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector());
|
||||
ClientRequest request = ClientRequest
|
||||
.method(HttpMethod.GET, uri)
|
||||
.body(BodyInserters.fromPublisher(getLatLngs(), Task.class))
|
||||
.build();
|
||||
|
||||
Flux<String> taskResponse = exchange
|
||||
.exchange(request)
|
||||
.flatMap(response -> response.bodyToFlux(String.class));
|
||||
|
||||
assertThat(taskResponse.blockFirst()).isInstanceOf(String.class);
|
||||
}
|
||||
|
||||
private static Flux<Task> getLatLngs() {
|
||||
return Flux
|
||||
.range(0, 3)
|
||||
.zipWith(Flux.interval(Duration.ofSeconds(1)))
|
||||
.map(x -> new Task("taskname", 1))
|
||||
.doOnNext(ll -> System.out.println("Produced: {}" + ll));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue