|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2016 the original author or authors. |
| 2 | + * Copyright 2012-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.endpoint.mvc;
|
18 | 18 |
|
19 |
| -import java.util.Set; |
20 |
| - |
21 | 19 | import org.junit.Before;
|
22 | 20 | import org.junit.Test;
|
23 |
| -import org.junit.runner.RunWith; |
24 | 21 |
|
25 |
| -import org.springframework.beans.factory.annotation.Autowired; |
26 |
| -import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration; |
27 |
| -import org.springframework.boot.actuate.autoconfigure.JolokiaAutoConfiguration; |
28 |
| -import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration; |
29 |
| -import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
30 |
| -import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; |
31 |
| -import org.springframework.boot.context.properties.EnableConfigurationProperties; |
32 |
| -import org.springframework.boot.test.context.SpringBootTest; |
33 |
| -import org.springframework.boot.test.util.EnvironmentTestUtils; |
34 |
| -import org.springframework.context.ConfigurableApplicationContext; |
35 |
| -import org.springframework.context.annotation.Configuration; |
36 |
| -import org.springframework.context.annotation.Import; |
37 |
| -import org.springframework.test.annotation.DirtiesContext; |
38 |
| -import org.springframework.test.context.junit4.SpringRunner; |
39 |
| -import org.springframework.test.web.servlet.MockMvc; |
40 |
| -import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 22 | +import org.springframework.beans.factory.DisposableBean; |
| 23 | +import org.springframework.mock.web.MockServletContext; |
| 24 | +import org.springframework.test.util.ReflectionTestUtils; |
41 | 25 | import org.springframework.web.context.WebApplicationContext;
|
42 |
| -import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 26 | +import org.springframework.web.servlet.mvc.ServletWrappingController; |
43 | 27 |
|
44 | 28 | import static org.assertj.core.api.Assertions.assertThat;
|
45 |
| -import static org.hamcrest.Matchers.containsString; |
46 |
| -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
47 |
| -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
48 |
| -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.spy; |
| 31 | +import static org.mockito.Mockito.verify; |
49 | 32 |
|
50 | 33 | /**
|
51 |
| - * Tests for {@link JolokiaMvcEndpoint} |
| 34 | + * Tests for {@link JolokiaMvcEndpoint}. |
52 | 35 | *
|
53 |
| - * @author Christian Dupuis |
54 |
| - * @author Dave Syer |
| 36 | + * @author Andy Wilkinson |
55 | 37 | */
|
56 |
| -@RunWith(SpringRunner.class) |
57 |
| -@DirtiesContext |
58 |
| -@SpringBootTest |
59 | 38 | public class JolokiaMvcEndpointTests {
|
60 | 39 |
|
61 |
| - @Autowired |
62 |
| - private MvcEndpoints endpoints; |
63 |
| - |
64 |
| - @Autowired |
65 |
| - private WebApplicationContext context; |
| 40 | + private final JolokiaMvcEndpoint endpoint = new JolokiaMvcEndpoint(); |
66 | 41 |
|
67 |
| - private MockMvc mvc; |
| 42 | + private final ServletWrappingController controller = (ServletWrappingController) spy( |
| 43 | + ReflectionTestUtils.getField(this.endpoint, "controller")); |
68 | 44 |
|
69 | 45 | @Before
|
70 |
| - public void setUp() { |
71 |
| - this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); |
72 |
| - EnvironmentTestUtils.addEnvironment((ConfigurableApplicationContext) this.context, |
73 |
| - "foo:bar"); |
74 |
| - } |
75 |
| - |
76 |
| - @Test |
77 |
| - public void endpointRegistered() throws Exception { |
78 |
| - Set<? extends MvcEndpoint> values = this.endpoints.getEndpoints(); |
79 |
| - assertThat(values).hasAtLeastOneElementOfType(JolokiaMvcEndpoint.class); |
80 |
| - } |
81 |
| - |
82 |
| - @Test |
83 |
| - public void search() throws Exception { |
84 |
| - this.mvc.perform(get("/jolokia/search/java.lang:*")).andExpect(status().isOk()) |
85 |
| - .andExpect(content().string(containsString("GarbageCollector"))); |
86 |
| - } |
87 |
| - |
88 |
| - @Test |
89 |
| - public void read() throws Exception { |
90 |
| - this.mvc.perform(get("/jolokia/read/java.lang:type=Memory")) |
91 |
| - .andExpect(status().isOk()) |
92 |
| - .andExpect(content().string(containsString("NonHeapMemoryUsage"))); |
| 46 | + public void before() { |
| 47 | + ReflectionTestUtils.setField(this.endpoint, "controller", this.controller); |
93 | 48 | }
|
94 | 49 |
|
95 | 50 | @Test
|
96 |
| - public void list() throws Exception { |
97 |
| - this.mvc.perform(get("/jolokia/list/java.lang/type=Memory/attr")) |
98 |
| - .andExpect(status().isOk()) |
99 |
| - .andExpect(content().string(containsString("NonHeapMemoryUsage"))); |
100 |
| - } |
101 |
| - |
102 |
| - @Configuration |
103 |
| - @EnableConfigurationProperties |
104 |
| - @EnableWebMvc |
105 |
| - @Import({ JacksonAutoConfiguration.class, |
106 |
| - HttpMessageConvertersAutoConfiguration.class, |
107 |
| - EndpointWebMvcAutoConfiguration.class, JolokiaAutoConfiguration.class, |
108 |
| - ManagementServerPropertiesAutoConfiguration.class }) |
109 |
| - public static class Config { |
110 |
| - |
| 51 | + public void controllerIsDestroyed() throws Exception { |
| 52 | + this.endpoint.setApplicationContext(mock(WebApplicationContext.class)); |
| 53 | + this.endpoint.setServletContext(new MockServletContext()); |
| 54 | + this.endpoint.afterPropertiesSet(); |
| 55 | + this.endpoint.destroy(); |
| 56 | + assertThat(this.endpoint).isInstanceOf(DisposableBean.class); |
| 57 | + verify(this.controller).destroy(); |
111 | 58 | }
|
112 | 59 |
|
113 | 60 | }
|
0 commit comments