Skip to content

Commit b86b3a9

Browse files
Merge pull request #18406 from programenth/spring-web-test1
Adding "EurekaServer and EurekaClient changes" | BAEL-5269
2 parents 357193a + ea82c9d commit b86b3a9

File tree

9 files changed

+228
-0
lines changed

9 files changed

+228
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.0.0</version>
9+
<relativePath/>
10+
</parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>eurekaClient</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>eurekaClient</name>
15+
<description>Demo project for Eureka Client</description>
16+
<url/>
17+
<properties>
18+
<java.version>17</java.version>
19+
<spring-cloud.version>2022.0.0</spring-cloud.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.cloud</groupId>
24+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
<dependencyManagement>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.springframework.cloud</groupId>
40+
<artifactId>spring-cloud-dependencies</artifactId>
41+
<version>${spring-cloud.version}</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
45+
</dependencies>
46+
</dependencyManagement>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.eurekaclient;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class Controller {
9+
10+
@Value("${spring.application.name}")
11+
private String appName;
12+
13+
@GetMapping("/")
14+
public String greeting() {
15+
return String.format("Hello from '%s'!", appName);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.eurekaclient;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class EurekaClientApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(EurekaClientApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.application.name=eurekaClient
2+
server.port=8081
3+
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
4+
eureka.instance.prefer-ip-address=true
5+
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.eurekaclient;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.web.client.TestRestTemplate;
8+
import org.springframework.boot.test.web.server.LocalServerPort;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
11+
12+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
13+
class EurekaClientIntegrationTest {
14+
15+
@LocalServerPort
16+
private int port;
17+
18+
@Autowired
19+
private TestRestTemplate restTemplate;
20+
21+
@Test
22+
void whenServerStarts_thenEurekaClientHomePageIsUp() {
23+
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/", String.class);
24+
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
25+
Assertions.assertTrue(response.getBody().contains("Hello from"));
26+
}
27+
28+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.4.3</version>
9+
<relativePath/>
10+
</parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>eurekaServer</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>eurekaServer</name>
15+
<description>Demo project for Eureka Server</description>
16+
<url/>
17+
<properties>
18+
<java.version>17</java.version>
19+
<spring-cloud.version>2024.0.0</spring-cloud.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.cloud</groupId>
28+
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
<dependencyManagement>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.springframework.cloud</groupId>
40+
<artifactId>spring-cloud-dependencies</artifactId>
41+
<version>${spring-cloud.version}</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
45+
</dependencies>
46+
</dependencyManagement>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.eurekaserver;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6+
7+
@SpringBootApplication
8+
@EnableEurekaServer
9+
public class EurekaServerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(EurekaServerApplication.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring.application.name=eurekaServer
2+
server.port=8761
3+
eureka.instance.hostname=localhost
4+
eureka.client.register-with-eureka=false
5+
eureka.client.fetch-registry=false
6+
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.eurekaserver;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.web.client.TestRestTemplate;
8+
import org.springframework.boot.test.web.server.LocalServerPort;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
11+
12+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
13+
class EurekaServerIntegrationTest {
14+
15+
@LocalServerPort
16+
private int port;
17+
18+
@Autowired
19+
private TestRestTemplate restTemplate;
20+
21+
@Test
22+
void whenServerStars_thenEurekaServerHomePageHasStatusUp() {
23+
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/", String.class);
24+
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
25+
Assertions.assertTrue(response.getBody().contains("<td>status</td><td>UP</td>"));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)