From: Kai Moritz Date: Sat, 11 Jan 2020 13:52:09 +0000 (+0100) Subject: WIP: Auseinanderfusseln WIP:Service / Schlechter Unit-Test X-Git-Tag: tmp~3 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=fb61d2b01aa88b19963f949e619d0340e412b4bd;p=demos%2Ftesting WIP: Auseinanderfusseln WIP:Service / Schlechter Unit-Test --- diff --git a/src/main/java/de/juplo/demo/RemoteContentController.java b/src/main/java/de/juplo/demo/RemoteContentController.java deleted file mode 100644 index 00b0b96..0000000 --- a/src/main/java/de/juplo/demo/RemoteContentController.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.juplo.demo; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Mono; - - -/** - * Fetches data from remote-webserver and renders them as HTML. - * @author Kai Moritz - */ -@RestController -public class RemoteContentController -{ - @Autowired - RemoteContentService service; - - - @GetMapping("/") - public Mono renderRemoteText(@RequestParam String path) - { - return service.getRemoteText(path); - } -} diff --git a/src/test/java/de/juplo/demo/RemoteContentServiceTest.java b/src/test/java/de/juplo/demo/RemoteContentServiceTest.java new file mode 100644 index 0000000..40a85b5 --- /dev/null +++ b/src/test/java/de/juplo/demo/RemoteContentServiceTest.java @@ -0,0 +1,50 @@ +package de.juplo.demo; + + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec; +import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec; +import org.springframework.web.reactive.function.client.WebClient.ResponseSpec; +import reactor.core.publisher.Mono; + + +/** + * Unit-Test for class {@link RemoteContentService}. + * @author Kai Moritz + */ +@ExtendWith(MockitoExtension.class) +public class RemoteContentServiceTest +{ + RemoteContentService service; + + @MockBean + WebClient webClient; + @MockBean + RequestHeadersUriSpec uriSpec; + @MockBean + RequestHeadersSpec headersSpec; + @MockBean + ResponseSpec responseSpec; + + + @Test + void test() + { + when(webClient.get()).thenReturn(uriSpec); + when(uriSpec.uri(eq("/foo"))).thenReturn(headersSpec); + when(headersSpec.retrieve()).thenReturn(responseSpec); + when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("bar")); + + Mono result = service.getRemoteText("/foo"); + + assertThat(result).isNotNull(); + assertThat(result.block()).isEqualTo("bar"); + } +}