From 9d9d0cc4276bab8a29c32b6bb11f8b85ed762576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Ju=C3=A1rez?= Date: Thu, 4 May 2017 22:27:05 -0500 Subject: [PATCH 1/5] BAEL-870 How to call a method during runtime using reflection? (#1769) * BAEL 870 How to call method during runtime using reflection? * BAEL-870 How to call a method during runtime using reflection? --- .../baeldung/java/reflection/Operations.java | 17 ++++++ .../java/reflection/OperationsUnitTest.java | 53 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/java/reflection/Operations.java create mode 100644 core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/java/reflection/Operations.java b/core-java/src/main/java/com/baeldung/java/reflection/Operations.java new file mode 100644 index 0000000000..754a93122f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/reflection/Operations.java @@ -0,0 +1,17 @@ +package com.baeldung.java.reflection; + +public class Operations { + + public double sum(int a, double b) { + return a + b; + } + + public static double multiply(float a, long b){ + return a * b; + } + + private boolean and(boolean a, boolean b) { + return a && b; + } + +} diff --git a/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java b/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java new file mode 100644 index 0000000000..da0b436275 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.java.reflection; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class OperationsUnitTest { + + public OperationsUnitTest() { + } + + @Test(expectedExceptions = IllegalAccessException.class) + public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class); + + Operations operationsInstance = new Operations(); + Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false); + + Assert.assertFalse(result); + } + + @Test + public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception { + Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class); + andInstanceMethod.setAccessible(true); + + Operations operationsInstance = new Operations(); + Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false); + + Assert.assertFalse(result); + } + + @Test + public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception { + Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class); + + Operations operationsInstance = new Operations(); + Double result = (Double)sumInstanceMethod.invoke(operationsInstance, 1, 3); + + Assert.assertTrue(4 == result); + } + + @Test + public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception { + Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply",float.class, long.class); + + Double result = (Double)multiplyStaticMethod.invoke(null, 3.5f, 2); + + Assert.assertTrue(7 == result); + } + +} From 86a893d454067572e17cbeac1dd917720b8cbd69 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Fri, 5 May 2017 07:06:09 +0200 Subject: [PATCH 2/5] Refactor SpringBootProfileIntegrationTest --- .../org/baeldung/SpringBootProfileIntegrationTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootProfileIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootProfileIntegrationTest.java index 806b38a8ce..9dd473f321 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootProfileIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/SpringBootProfileIntegrationTest.java @@ -1,8 +1,5 @@ package org.baeldung; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import org.baeldung.config.H2TestProfileJPAConfig; import org.baeldung.domain.GenericEntity; import org.baeldung.repository.GenericEntityRepository; @@ -11,9 +8,12 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringRunner.class) @SpringBootTest(classes = { Application.class, H2TestProfileJPAConfig.class }) @ActiveProfiles("test") public class SpringBootProfileIntegrationTest { From e2555c2210a015bb3c89e4470890a6bf0454bf3a Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Fri, 5 May 2017 07:40:24 -0500 Subject: [PATCH 3/5] BAEL-870: Switch from TestNG to JUnit to fix broken build (#1787) * Add files via upload * Update pom.xml * Update RunGuice.java * Update Communication.java * Update CommunicationMode.java * Update DefaultCommunicator.java * Update EmailCommunicationMode.java * Update IMCommunicationMode.java * Update SMSCommunicationMode.java * Update MessageLogger.java * Update MessageSentLoggable.java * Update AOPModule.java * Update BasicModule.java * Update CommunicationModel.java * Update Communicator.java * Update BasicModule.java * Update RunGuice.java * Update MessageLogger.java * Update Communicator.java * Update pom.xml * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * Update pom.xml * Update pom.xml * Update pom.xml * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom * BAEL-393: Adding guide-intro module to root pom * BAEL-9: Updated README.md * BAEL-157: README.md updated * Changed project name * Update RunGuice.java Removed references to message logging and output * Update Communication.java Removed message logging-related code * BAEL-566: Updated README.md * New project name * BAEL-393: removing guice-intro directory * BAEL-393: renamed module guice-intro to guice in root pom.xml * BAEL-393 and BAEL-541 README.md files * BAEL-731: Updated README.md * BAEL-680: renamed test methods * BAEL-714: Updated README.md * BAEL-737: Updated README.md * BAEL-680 and BAEL-756 README.md updates * BAEL-666: Updated README * BAEL-415: Custom Scope * BAEL-415: Custom Scope - renamed classes to reflect TenantScope * README file updates for BAEL-723, BAEL-763, and BAEL-415 * BAEL-735: README * BAEL-567: README * BAEL-736: README * BAEL-766: Update README * BAEL-555: README update * BAEL-761: README update * BAEL-742: Stripe API for Java README file * BAEL-86: Correction to README file * BAEL-828: Updated README.md * BAEL-830: Updated README * BAEL-870: Switched from TestNG to JUnit due to build errors --- core-java/pom.xml | 1 - .../java/reflection/OperationsUnitTest.java | 18 +++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core-java/pom.xml b/core-java/pom.xml index 1f5897ee2d..633687bde0 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -395,7 +395,6 @@ 1.3 4.12 1.10.19 - 6.10 3.6.1 1.7.0 diff --git a/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java b/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java index da0b436275..4bd12047c6 100644 --- a/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java @@ -1,23 +1,27 @@ package com.baeldung.java.reflection; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import org.testng.Assert; -import org.testng.annotations.Test; + +import org.junit.Test; public class OperationsUnitTest { public OperationsUnitTest() { } - @Test(expectedExceptions = IllegalAccessException.class) + @Test(expected=IllegalAccessException.class) public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class); Operations operationsInstance = new Operations(); Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false); - Assert.assertFalse(result); + assertFalse(result); } @Test @@ -28,7 +32,7 @@ public class OperationsUnitTest { Operations operationsInstance = new Operations(); Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false); - Assert.assertFalse(result); + assertFalse(result); } @Test @@ -38,7 +42,7 @@ public class OperationsUnitTest { Operations operationsInstance = new Operations(); Double result = (Double)sumInstanceMethod.invoke(operationsInstance, 1, 3); - Assert.assertTrue(4 == result); + assertThat(result, equalTo(4.0)); } @Test @@ -47,7 +51,7 @@ public class OperationsUnitTest { Double result = (Double)multiplyStaticMethod.invoke(null, 3.5f, 2); - Assert.assertTrue(7 == result); + assertThat(result, equalTo(7.0)); } } From 67968089a07ca5950d586fb7c942cf3f6fe138a1 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Fri, 5 May 2017 16:11:00 +0200 Subject: [PATCH 4/5] Use newer runners --- .../test/java/org/baeldung/SpringBootJPAIntegrationTest.java | 4 ++-- .../java/org/baeldung/boot/ApplicationIntegrationTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java index 202d24ffc7..7c90622cdc 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java @@ -6,12 +6,12 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -@RunWith(SpringJUnit4ClassRunner.class) +@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class SpringBootJPAIntegrationTest { @Autowired diff --git a/spring-boot/src/test/java/org/baeldung/boot/ApplicationIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/boot/ApplicationIntegrationTest.java index 57a8abc1ee..5e351157c8 100644 --- a/spring-boot/src/test/java/org/baeldung/boot/ApplicationIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/boot/ApplicationIntegrationTest.java @@ -5,9 +5,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) +@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) @TestPropertySource("classpath:exception.properties") public class ApplicationIntegrationTest { From 30df1541a1ae26ffdb29770fa933d23ec7187d10 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Fri, 5 May 2017 16:26:25 +0200 Subject: [PATCH 5/5] Spring 5 reff (#1786) * Fix Spring5 snippets * Refactor Spring 5 samples * Refactor Spring 5 samples * Enable Spring5 --- pom.xml | 2 +- spring-5/.gitignore | 3 +- .../com/baeldung/functional/FormHandler.java | 23 +- .../java/com/baeldung/web/FooController.java | 34 +-- ...ple1.java => Example1IntegrationTest.java} | 2 +- ...ple2.java => Example2IntegrationTest.java} | 2 +- ...mple.java => ParallelIntegrationTest.java} | 6 +- ...=> Spring5ApplicationIntegrationTest.java} | 2 +- ...nctionalWebApplicationIntegrationTest.java | 275 +++++++++--------- .../jupiter/Spring5JUnit5ParallelTest.java | 8 +- 10 files changed, 161 insertions(+), 196 deletions(-) rename spring-5/src/test/java/com/baeldung/{IntegrationTestExample1.java => Example1IntegrationTest.java} (93%) rename spring-5/src/test/java/com/baeldung/{IntegrationTestExample2.java => Example2IntegrationTest.java} (93%) rename spring-5/src/test/java/com/baeldung/{ParallelTestExample.java => ParallelIntegrationTest.java} (62%) rename spring-5/src/test/java/com/baeldung/{Spring5ApplicationTests.java => Spring5ApplicationIntegrationTest.java} (85%) diff --git a/pom.xml b/pom.xml index 3f8eeb6264..91af48f79f 100644 --- a/pom.xml +++ b/pom.xml @@ -111,7 +111,7 @@ selenium-junit-testng solr spark-java - + spring-5 spring-akka spring-amqp spring-all diff --git a/spring-5/.gitignore b/spring-5/.gitignore index 83c05e60c8..dec013dfa4 100644 --- a/spring-5/.gitignore +++ b/spring-5/.gitignore @@ -1,6 +1,5 @@ -*.class - #folders# +.idea /target /neoDb* /data diff --git a/spring-5/src/main/java/com/baeldung/functional/FormHandler.java b/spring-5/src/main/java/com/baeldung/functional/FormHandler.java index c44db76f56..5c3890d412 100644 --- a/spring-5/src/main/java/com/baeldung/functional/FormHandler.java +++ b/spring-5/src/main/java/com/baeldung/functional/FormHandler.java @@ -18,33 +18,24 @@ public class FormHandler { return request .body(toFormData()) .map(MultiValueMap::toSingleValueMap) - .map(formData -> { - System.out.println("form data: " + formData.toString()); - if ("baeldung".equals(formData.get("user")) && "you_know_what_to_do".equals(formData.get("token"))) { - return ok() - .body(Mono.just("welcome back!"), String.class) - .block(); - } - return ServerResponse - .badRequest() - .build() - .block(); - }); + .filter(formData -> "baeldung".equals(formData.get("user"))) + .filter(formData -> "you_know_what_to_do".equals(formData.get("token"))) + .flatMap(formData -> ok().body(Mono.just("welcome back!"), String.class)) + .or(ServerResponse.badRequest().build()); } Mono handleUpload(ServerRequest request) { return request .body(toDataBuffers()) .collectList() - .map(dataBuffers -> { + .flatMap(dataBuffers -> { AtomicLong atomicLong = new AtomicLong(0); dataBuffers.forEach(d -> atomicLong.addAndGet(d .asByteBuffer() .array().length)); - System.out.println("data length:" + atomicLong.get()); + return ok() - .body(fromObject(atomicLong.toString())) - .block(); + .body(fromObject(atomicLong.toString())); }); } } diff --git a/spring-5/src/main/java/com/baeldung/web/FooController.java b/spring-5/src/main/java/com/baeldung/web/FooController.java index d0b69e707e..0e7c94bd8f 100644 --- a/spring-5/src/main/java/com/baeldung/web/FooController.java +++ b/spring-5/src/main/java/com/baeldung/web/FooController.java @@ -1,26 +1,15 @@ package com.baeldung.web; -import java.util.List; -import java.util.Optional; - -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; - +import com.baeldung.persistence.FooRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.http.HttpStatus; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import com.baeldung.persistence.FooRepository; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import java.util.List; @RestController("/foos") public class FooController { @@ -30,30 +19,29 @@ public class FooController { // API - read - @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") + @GetMapping("/foos/{id}") @ResponseBody @Validated public Foo findById(@PathVariable @Min(0) final long id) { - return repo.findOne(id).orElse(null); + return repo.findById(id).orElse(null); } - @RequestMapping(method = RequestMethod.GET) + @GetMapping @ResponseBody public List findAll() { return repo.findAll(); } - @RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) + @GetMapping(params = { "page", "size" }) @ResponseBody @Validated public List findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) { - final Page resultPage = repo.findAll(new PageRequest(page, size)); - return resultPage.getContent(); + return repo.findAll(PageRequest.of(page, size)).getContent(); } // API - write - @RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") + @PutMapping("/foos/{id}") @ResponseStatus(HttpStatus.OK) @ResponseBody public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) { diff --git a/spring-5/src/test/java/com/baeldung/IntegrationTestExample1.java b/spring-5/src/test/java/com/baeldung/Example1IntegrationTest.java similarity index 93% rename from spring-5/src/test/java/com/baeldung/IntegrationTestExample1.java rename to spring-5/src/test/java/com/baeldung/Example1IntegrationTest.java index 0a27be4a95..8b9e66213f 100644 --- a/spring-5/src/test/java/com/baeldung/IntegrationTestExample1.java +++ b/spring-5/src/test/java/com/baeldung/Example1IntegrationTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class IntegrationTestExample1 { +public class Example1IntegrationTest { @Test public void test1a() { diff --git a/spring-5/src/test/java/com/baeldung/IntegrationTestExample2.java b/spring-5/src/test/java/com/baeldung/Example2IntegrationTest.java similarity index 93% rename from spring-5/src/test/java/com/baeldung/IntegrationTestExample2.java rename to spring-5/src/test/java/com/baeldung/Example2IntegrationTest.java index 0bb2d47ef5..6ed53ca4e9 100644 --- a/spring-5/src/test/java/com/baeldung/IntegrationTestExample2.java +++ b/spring-5/src/test/java/com/baeldung/Example2IntegrationTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class IntegrationTestExample2 { +public class Example2IntegrationTest { @Test public void test1a() { diff --git a/spring-5/src/test/java/com/baeldung/ParallelTestExample.java b/spring-5/src/test/java/com/baeldung/ParallelIntegrationTest.java similarity index 62% rename from spring-5/src/test/java/com/baeldung/ParallelTestExample.java rename to spring-5/src/test/java/com/baeldung/ParallelIntegrationTest.java index e73b8d4649..1ce96de4ef 100644 --- a/spring-5/src/test/java/com/baeldung/ParallelTestExample.java +++ b/spring-5/src/test/java/com/baeldung/ParallelIntegrationTest.java @@ -5,18 +5,18 @@ import org.junit.experimental.ParallelComputer; import org.junit.runner.Computer; import org.junit.runner.JUnitCore; -public class ParallelTestExample { +public class ParallelIntegrationTest { @Test public void runTests() { - final Class[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class }; + final Class[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class }; JUnitCore.runClasses(new Computer(), classes); } @Test public void runTestsInParallel() { - final Class[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class }; + final Class[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class }; JUnitCore.runClasses(new ParallelComputer(true, true), classes); } diff --git a/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java b/spring-5/src/test/java/com/baeldung/Spring5ApplicationIntegrationTest.java similarity index 85% rename from spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java rename to spring-5/src/test/java/com/baeldung/Spring5ApplicationIntegrationTest.java index 537ec56a89..af288c3c2d 100644 --- a/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java +++ b/spring-5/src/test/java/com/baeldung/Spring5ApplicationIntegrationTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class Spring5ApplicationTests { +public class Spring5ApplicationIntegrationTest { @Test public void contextLoads() { diff --git a/spring-5/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java b/spring-5/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java index dda25f46f7..fe7f4b06ff 100644 --- a/spring-5/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java +++ b/spring-5/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java @@ -4,152 +4,139 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; 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.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.test.web.reactive.server.WebTestClient; -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 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 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) + .isEqualTo("helloworld"); + } + + @Test + public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception { + client + .get() + .uri("/") + .exchange() + .expectStatus() + .isOk() + .expectBody(String.class) + .isEqualTo("helloworld"); + } + + /* @Test + public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception { + MultiValueMap 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 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) + .isEqualTo("hello"); + } } diff --git a/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelTest.java b/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelTest.java index a060b78c93..92d69b2d91 100644 --- a/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelTest.java +++ b/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelTest.java @@ -1,7 +1,7 @@ package com.baeldung.jupiter; -import com.baeldung.IntegrationTestExample1; -import com.baeldung.IntegrationTestExample2; +import com.baeldung.Example1IntegrationTest; +import com.baeldung.Example2IntegrationTest; import org.junit.experimental.ParallelComputer; import org.junit.jupiter.api.Test; import org.junit.runner.Computer; @@ -12,7 +12,7 @@ public class Spring5JUnit5ParallelTest { @Test public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingParallel() { final Class[] classes = { - IntegrationTestExample1.class, IntegrationTestExample2.class + Example1IntegrationTest.class, Example2IntegrationTest.class }; JUnitCore.runClasses(new ParallelComputer(true, true), classes); @@ -21,7 +21,7 @@ public class Spring5JUnit5ParallelTest { @Test public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingLinear() { final Class[] classes = { - IntegrationTestExample1.class, IntegrationTestExample2.class + Example1IntegrationTest.class, Example2IntegrationTest.class }; JUnitCore.runClasses(new Computer(), classes);