|
| 1 | +package com.baeldung.displayallbeans; |
| 2 | + |
| 3 | +import static org.assertj.core.api.BDDAssertions.then; |
| 4 | +import static org.hamcrest.CoreMatchers.hasItem; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.beans.factory.annotation.Value; |
| 17 | +import org.springframework.boot.context.embedded.LocalServerPort; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 20 | +import org.springframework.http.HttpStatus; |
| 21 | +import org.springframework.http.ResponseEntity; |
| 22 | +import org.springframework.test.context.TestPropertySource; |
| 23 | +import org.springframework.test.context.junit4.SpringRunner; |
| 24 | +import org.springframework.web.context.WebApplicationContext; |
| 25 | + |
| 26 | +import com.baeldung.displayallbeans.Application; |
| 27 | + |
| 28 | +@RunWith(SpringRunner.class) |
| 29 | +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 30 | +@TestPropertySource(properties = {"management.port=0", "endpoints.beans.id=springbeans", "endpoints.beans.sensitive=false"}) |
| 31 | +public class DisplayBeanIntegrationTest { |
| 32 | + |
| 33 | + @LocalServerPort |
| 34 | + private int port; |
| 35 | + |
| 36 | + @Value("${local.management.port}") |
| 37 | + private int mgt; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private TestRestTemplate testRestTemplate; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private WebApplicationContext context; |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception { |
| 47 | + @SuppressWarnings("rawtypes") |
| 48 | + ResponseEntity<Map> entity = this.testRestTemplate.getForEntity( |
| 49 | + "http://localhost:" + this.port + "/getPerson", Map.class); |
| 50 | + |
| 51 | + then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenRestTemplate_whenAccessEndpointUrl_thenHttpStatusOK() throws Exception { |
| 56 | + @SuppressWarnings("rawtypes") |
| 57 | + ResponseEntity<List> entity = this.testRestTemplate.getForEntity( |
| 58 | + "http://localhost:" + this.mgt + "/springbeans", List.class); |
| 59 | + |
| 60 | + then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenRestTemplate_whenAccessEndpointUrl_thenReturnsBeanNames() throws Exception { |
| 65 | + @SuppressWarnings("rawtypes") |
| 66 | + ResponseEntity<List> entity = this.testRestTemplate.getForEntity( |
| 67 | + "http://localhost:" + this.mgt + "/springbeans", List.class); |
| 68 | + |
| 69 | + List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans"); |
| 70 | + List<String> beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList()); |
| 71 | + |
| 72 | + assertThat( beanNamesList, hasItem("personController")); |
| 73 | + assertThat( beanNamesList, hasItem("person")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenWebApplicationContext_whenAccessGetBeanDefinitionNames_thenReturnsBeanNames() throws Exception { |
| 78 | + String[] beanNames = context.getBeanDefinitionNames(); |
| 79 | + |
| 80 | + List<String> beanNamesList = Arrays.asList(beanNames); |
| 81 | + assertTrue(beanNamesList.contains("personController")); |
| 82 | + assertTrue(beanNamesList.contains("person")); |
| 83 | + } |
| 84 | +} |
0 commit comments