import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
+import reactor.core.publisher.Mono;
/**
@GetMapping("/")
public String fetch(Model model, @RequestParam String path)
{
- model.addAttribute("text", service.getRemoteText(path));
+ model.addAttribute(
+ "text",
+ service
+ .getRemoteText(path)
+ .onErrorResume(t -> Mono.just(t.getMessage())));
return "home";
}
}
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.ui.Model;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
.expectComplete()
.verify();
}
+
+ @Test
+ @DisplayName("Data not found on remote-server")
+ void testNotFoud()
+ {
+ Mono<String> mono = Mono.error(WebClientResponseException.create(404, "", null, null, null));
+ when(service.getRemoteText("foo")).thenReturn(mono);
+
+ String result = controller.fetch(model, "foo");
+
+ assertThat(result).isEqualTo("home");
+ ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
+ verify(model).addAttribute(eq("text"), captor.capture());
+ StepVerifier
+ .create(captor.getValue())
+ .expectNext("404 ")
+ .expectComplete()
+ .verify();
+ }
}