Explorar el Código

Issue #25 fix- replacement of deprecated methods in test classes (#26)

Marceli Baczewski hace 8 años
padre
commit
15a2878701

+ 17
- 22
complete/src/test/java/hello/HelloControllerIT.java Ver fichero

@@ -8,36 +8,31 @@ import java.net.URL;
8 8
 import org.junit.Before;
9 9
 import org.junit.Test;
10 10
 import org.junit.runner.RunWith;
11
-import org.springframework.beans.factory.annotation.Value;
12
-import org.springframework.boot.test.IntegrationTest;
13
-import org.springframework.boot.test.SpringApplicationConfiguration;
14
-import org.springframework.boot.test.TestRestTemplate;
11
+import org.springframework.boot.context.embedded.LocalServerPort;
12
+import org.springframework.boot.test.context.SpringBootTest;
13
+import org.springframework.boot.test.web.client.TestRestTemplate;
15 14
 import org.springframework.http.ResponseEntity;
16 15
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17
-import org.springframework.test.context.web.WebAppConfiguration;
18
-import org.springframework.web.client.RestTemplate;
19 16
 
20 17
 @RunWith(SpringJUnit4ClassRunner.class)
21
-@SpringApplicationConfiguration(classes = Application.class)
22
-@WebAppConfiguration
23
-@IntegrationTest({"server.port=0"})
18
+@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
24 19
 public class HelloControllerIT {
25 20
 
26
-    @Value("${local.server.port}")
21
+    @LocalServerPort
27 22
     private int port;
28 23
 
29
-	private URL base;
30
-	private RestTemplate template;
24
+    private URL base;
25
+    private TestRestTemplate template;
31 26
 
32
-	@Before
33
-	public void setUp() throws Exception {
34
-		this.base = new URL("http://localhost:" + port + "/");
35
-		template = new TestRestTemplate();
36
-	}
27
+    @Before
28
+    public void setUp() throws Exception {
29
+        this.base = new URL("http://localhost:" + port + "/");
30
+        template = new TestRestTemplate();
31
+    }
37 32
 
38
-	@Test
39
-	public void getHello() throws Exception {
40
-		ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
41
-		assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
42
-	}
33
+    @Test
34
+    public void getHello() throws Exception {
35
+        ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
36
+        assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
37
+    }
43 38
 }

+ 2
- 5
complete/src/test/java/hello/HelloControllerTest.java Ver fichero

@@ -7,18 +7,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
7 7
 import org.junit.Before;
8 8
 import org.junit.Test;
9 9
 import org.junit.runner.RunWith;
10
-import org.springframework.boot.test.SpringApplicationConfiguration;
10
+import org.springframework.boot.test.context.SpringBootTest;
11 11
 import org.springframework.http.MediaType;
12
-import org.springframework.mock.web.MockServletContext;
13 12
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14
-import org.springframework.test.context.web.WebAppConfiguration;
15 13
 import org.springframework.test.web.servlet.MockMvc;
16 14
 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
17 15
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18 16
 
19 17
 @RunWith(SpringJUnit4ClassRunner.class)
20
-@SpringApplicationConfiguration(classes = MockServletContext.class)
21
-@WebAppConfiguration
18
+@SpringBootTest
22 19
 public class HelloControllerTest {
23 20
 
24 21
 	private MockMvc mvc;