From 55d3274fed757dac56a42fbffb3221940df2bf08 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 1 Jan 2017 17:01:16 +0100 Subject: [PATCH] Making work integration tests --- .../service/impl/AwesomeBusinessLogicImpl.java | 1 - .../rest/controller/adapters/RxJavaAdapter.java | 4 +++ .../spring-configuration/mvc/rest/rest-config.xml | 27 ++++++++---------- .../controller/CarControllerIntegrationTest.java | 2 -- ...pletableFutureCarControllerIntegrationTest.java | 2 -- .../RxJavaCarControllerIntegrationTest.java | 33 +++++++++++++++++----- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/impl/AwesomeBusinessLogicImpl.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/impl/AwesomeBusinessLogicImpl.java index f81e8f6..5c960fe 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/impl/AwesomeBusinessLogicImpl.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/impl/AwesomeBusinessLogicImpl.java @@ -12,7 +12,6 @@ import org.springframework.stereotype.Service; import de.spring.webservices.domain.Car; import de.spring.webservices.rest.business.service.AwesomeBusinessLogic; -import rx.exceptions.Exceptions; @Service("awesomeBusinessLogic") public class AwesomeBusinessLogicImpl implements AwesomeBusinessLogic { diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java index f0d4985..3bd32db 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java @@ -15,6 +15,10 @@ import rx.Single; * * spring netflix is already doing this stuff for me. * + * I DO NOT THINK THIS IS THE RIGHT WAY FOR DOING IT. I MEAN I AM USING subscribe() + * AND spring netflix DOES NOT. SO, I GUESS, CALLING subscribe() IS NOT THE RIGHT WAY OF DOING THIS STUFF :( + * + * YOU'D BETTER USE THE spring netflix IMPLEMENTATION INSTEAD OF THIS ONE :( */ public class RxJavaAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(RxJavaAdapter.class); diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml index 5bf22fe..d5fc193 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml @@ -15,22 +15,14 @@ See: org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser + + With XML annotation-driven works with org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser + With Java annotations @EnableWebMvc does the same with org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport + + So annotation-driven and @EnableWebMvc are the same. The first one using XML files and the second one using Java annotations :/ + + All this stuff could be done by means of annotation-driven or @EnableWebMvc but in this way I have more control :/ --> - - - - - - - - @@ -67,8 +59,9 @@ + + class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> @@ -80,6 +73,8 @@ + + diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java index 4a19dbc..053f7b9 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java @@ -15,7 +15,6 @@ import java.util.ArrayList; import java.util.List; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.domain.PageImpl; @@ -36,7 +35,6 @@ import de.spring.webservices.rest.business.service.AwesomeBusinessLogic; // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/ -@Ignore @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"}) public class CarControllerIntegrationTest { diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CompletableFutureCarControllerIntegrationTest.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CompletableFutureCarControllerIntegrationTest.java index 1d9afd5..8c41608 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CompletableFutureCarControllerIntegrationTest.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/CompletableFutureCarControllerIntegrationTest.java @@ -19,7 +19,6 @@ import java.util.List; import java.util.concurrent.CompletableFuture; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.domain.Page; @@ -43,7 +42,6 @@ import de.spring.webservices.rest.business.service.CompletableFutureBusinessLogi // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/ -@Ignore @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"}) public class CompletableFutureCarControllerIntegrationTest { diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java index efffd5d..52f56fe 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java @@ -17,8 +17,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.util.ArrayList; import java.util.List; +import javax.inject.Inject; + import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.domain.Page; @@ -32,6 +33,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -39,11 +41,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import de.spring.webservices.domain.Car; import de.spring.webservices.rest.business.service.RxJavaBusinessLogic; import rx.Observable; +import rx.Subscriber; +import rx.schedulers.Schedulers; // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/ -@Ignore @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"}) public class RxJavaCarControllerIntegrationTest { @@ -51,6 +54,9 @@ public class RxJavaCarControllerIntegrationTest { private static final int PAGE_SIZE = 10; private static final String TEMPLATE = "Car: %s"; + @Inject + private AsyncHandlerMethodReturnValueHandler singleReturnValueHandler; + private RxJavaBusinessLogic rxJavaBusinessLogic; private RxJavaCarController controller; private MockMvc mockMvc; @@ -59,20 +65,27 @@ public class RxJavaCarControllerIntegrationTest { public void setup() { rxJavaBusinessLogic = mock(RxJavaBusinessLogic.class); controller = new RxJavaCarController(rxJavaBusinessLogic); - mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); + mockMvc = MockMvcBuilders + .standaloneSetup(controller) + .setCustomReturnValueHandlers(singleReturnValueHandler) + .build(); } @Test public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception { final List cars = new ArrayList<>(); cars.add(new Car(1L, String.format(TEMPLATE, 1))); - Observable> observable = Observable.create(observer -> observer.onNext( new PageImpl<>(cars))); + Observable> observable = Observable + .create((Subscriber> observer) -> { + observer.onNext( new PageImpl<>(cars)); + observer.onCompleted(); + }).subscribeOn(Schedulers.io()); given(rxJavaBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(observable); MvcResult result = mockMvc.perform(get("/api/rxjava/cars/") .accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(request().asyncStarted()) - .andExpect(request().asyncResult(instanceOf(Page.class))) + .andExpect(request().asyncResult(instanceOf(ResponseEntity.class))) .andReturn(); mockMvc.perform(asyncDispatch(result)) @@ -85,13 +98,17 @@ public class RxJavaCarControllerIntegrationTest { @Test public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception { - Observable observable = Observable.create(observer -> observer.onNext( new Car(1L, String.format(TEMPLATE, 1)))); + Observable observable = Observable + .create((Subscriber observer) -> { + observer.onNext( new Car(1L, String.format(TEMPLATE, 1))); + observer.onCompleted(); + }).subscribeOn(Schedulers.io()); given(rxJavaBusinessLogic.findById(1L)).willReturn(observable); MvcResult result = mockMvc.perform(get("/api/rxjava/cars/{id}", 1L) .accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(request().asyncStarted()) - .andExpect(request().asyncResult(instanceOf(Car.class))) + .andExpect(request().asyncResult(instanceOf(ResponseEntity.class))) .andReturn(); mockMvc.perform(asyncDispatch(result)) @@ -101,6 +118,8 @@ public class RxJavaCarControllerIntegrationTest { .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)); } + // THIS GUY IS USING MY de.spring.webservices.rest.controller.adapters.RxJavaAdapter AND IT DOES NOT NEED to call onCompleted() + // I DO NOT THINK MY de.spring.webservices.rest.controller.adapters.RxJavaAdapter SHOULD BE USED. YOU'D BETTER USE THE spring netflix IMPLEMENTATION :/ @Test public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception { Car car = new Car(null, "nothing"); -- 2.1.4