From 884fd0125adac223837a88e0dd69e3c1db8618e1 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 4 Dec 2013 14:56:53 +0000 Subject: [PATCH] Add some handler adapter tests --- .../endpoint/mvc/EndpointHandlerAdapter.java | 1 + .../mvc/EndpointHandlerAdapterTests.java | 38 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapter.java index 2b7e2bb33c..debd78158b 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapter.java @@ -50,6 +50,7 @@ import com.fasterxml.jackson.databind.SerializationFeature; * {@link AbstractMessageConverterMethodProcessor} but not tied to annotated methods. * * @author Phillip Webb + * * @see EndpointHandlerMapping */ public final class EndpointHandlerAdapter implements HandlerAdapter { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapterTests.java index bb0251d9cc..5877f63a7d 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapterTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerAdapterTests.java @@ -16,10 +16,16 @@ package org.springframework.boot.actuate.endpoint.mvc; -import org.junit.Test; -import org.springframework.boot.actuate.endpoint.Endpoint; -import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerAdapter; +import java.util.Collections; +import java.util.Map; +import org.junit.Test; +import org.springframework.boot.actuate.endpoint.AbstractEndpoint; +import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -32,6 +38,8 @@ import static org.mockito.Mockito.mock; public class EndpointHandlerAdapterTests { private EndpointHandlerAdapter adapter = new EndpointHandlerAdapter(); + private MockHttpServletRequest request = new MockHttpServletRequest(); + private MockHttpServletResponse response = new MockHttpServletResponse(); @Test public void onlySupportsEndpoints() throws Exception { @@ -39,6 +47,28 @@ public class EndpointHandlerAdapterTests { assertFalse(this.adapter.supports(mock(Object.class))); } - // FIXME tests + @Test + public void rendersJson() throws Exception { + this.adapter.handle(this.request, this.response, + new AbstractEndpoint>("/foo") { + @Override + protected Map doInvoke() { + return Collections.singletonMap("hello", "world"); + } + }); + assertEquals("{\"hello\":\"world\"}", this.response.getContentAsString()); + } + @Test + public void rendersString() throws Exception { + this.request.addHeader("Accept", "text/plain"); + this.adapter.handle(this.request, this.response, new AbstractEndpoint( + "/foo") { + @Override + protected String doInvoke() { + return "hello world"; + } + }); + assertEquals("hello world", this.response.getContentAsString()); + } }