Ensure /error view is available

This commit is contained in:
Dave Syer
2013-11-28 14:20:45 +00:00
parent 8c9b7bd406
commit 3e5e058b02
3 changed files with 156 additions and 11 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.boot.actuate.autoconfigure;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -30,11 +32,13 @@ import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigurationReport;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link EndpointAutoConfiguration}.
@@ -73,6 +77,21 @@ public class EndpointAutoConfigurationTests {
assertNotNull(this.context.getBean(TraceEndpoint.class));
}
@Test
public void healthEndpoint() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(EndpointAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class);
this.context.refresh();
HealthEndpoint<?> bean = this.context.getBean(HealthEndpoint.class);
assertNotNull(bean);
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) bean.invoke();
assertNotNull(result);
assertTrue("Wrong result: " + result, result.containsKey("status"));
assertTrue("Wrong result: " + result, result.containsKey("database"));
}
@Test
public void autoconfigurationAuditEndpoints() {
this.context = new AnnotationConfigApplicationContext();

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
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.context.ConfigurableWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Dave Syer
*/
public class BasicErrorControllerSpecialIntegrationTests {
private ConfigurableWebApplicationContext wac;
private MockMvc mockMvc;
@After
public void close() {
if (this.wac != null) {
this.wac.close();
}
}
public void setup(ConfigurableWebApplicationContext context) {
this.wac = context;
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void errorPageAvailableWithParentContext() throws Exception {
setup((ConfigurableWebApplicationContext) new SpringApplicationBuilder(
ParentConfiguration.class).child(ChildConfiguration.class).run());
MvcResult response = this.mockMvc
.perform(get("/error").accept(MediaType.TEXT_HTML))
.andExpect(status().isOk()).andReturn();
String content = response.getResponse().getContentAsString();
assertTrue("Wrong content: " + content, content.contains("status=999"));
}
@Test
public void errorPageAvailableWithMvcIncluded() throws Exception {
setup((ConfigurableWebApplicationContext) SpringApplication
.run(WebMvcIncludedConfiguration.class));
MvcResult response = this.mockMvc
.perform(get("/error").accept(MediaType.TEXT_HTML))
.andExpect(status().isOk()).andReturn();
String content = response.getResponse().getContentAsString();
assertTrue("Wrong content: " + content, content.contains("status=999"));
}
@Configuration
// TODO: fix this so it doesn't need to be excluded
@EnableAutoConfiguration(exclude = ServerPropertiesAutoConfiguration.class)
protected static class ParentConfiguration {
}
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
protected static class WebMvcIncludedConfiguration {
// For manual testing
public static void main(String[] args) {
SpringApplication.run(WebMvcIncludedConfiguration.class, args);
}
}
@Configuration
@EnableAutoConfiguration
protected static class ChildConfiguration {
// For manual testing
public static void main(String[] args) {
new SpringApplicationBuilder(ParentConfiguration.class).child(
ChildConfiguration.class).run(args);
}
}
}