Skip to content

Commit afc7862

Browse files
committed
unit tests added for controllers
1 parent 6ad64ff commit afc7862

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed
Binary file not shown.

microservices-self-registration/contextservice/pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
<groupId>org.springframework.cloud</groupId>
4848
<artifactId>spring-cloud-starter-openfeign</artifactId>
4949
</dependency>
50-
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
<scope>test</scope>
54+
</dependency>
5155
<dependency>
5256
<groupId>org.springframework.boot</groupId>
5357
<artifactId>spring-boot-starter-test</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.learning.contextservice;
2+
3+
import com.learning.contextservice.client.GreetingServiceClient;
4+
import com.learning.contextservice.controller.ContextController;
5+
import com.netflix.discovery.converters.Auto;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.Mock;
8+
import org.mockito.Mockito;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
12+
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
13+
import org.springframework.context.annotation.Import;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
18+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
19+
20+
@WebMvcTest(ContextController.class)
21+
public class ContextControllerTest {
22+
23+
@Autowired
24+
private MockMvc mockMvc;
25+
26+
@MockitoBean
27+
private GreetingServiceClient greetingServiceClient;
28+
29+
@Value("${user.region}")
30+
private String userRegion;
31+
32+
@Test
33+
void shouldReturnContextGreeting() throws Exception{
34+
Mockito.when(greetingServiceClient.getGreeting()).thenReturn("Mocked Hello");
35+
36+
mockMvc.perform(MockMvcRequestBuilders.get("/context")
37+
.accept(MediaType.TEXT_PLAIN))
38+
.andExpect(MockMvcResultMatchers.status().isOk())
39+
.andExpect(MockMvcResultMatchers.content().string("The Greeting Service says: Mocked Hello from Chennai, Tamil Nadu, India"));
40+
}
41+
}
Binary file not shown.

microservices-self-registration/greetingservice/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<groupId>org.springframework.cloud</groupId>
4040
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
4247
<dependency>
4348
<groupId>org.springframework.boot</groupId>
4449
<artifactId>spring-boot-starter-actuator</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.learning.greetingservice;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.test.web.servlet.MockMvc;
8+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
9+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
10+
11+
@WebMvcTest(GreetingsController.class)
12+
public class GreetingControllerTest {
13+
14+
@Autowired
15+
private MockMvc mockMvc;
16+
17+
@Test
18+
void shouldReturnGreeting() throws Exception{
19+
mockMvc.perform(MockMvcRequestBuilders.get("/greeting")
20+
.accept(MediaType.TEXT_PLAIN))
21+
.andExpect(MockMvcResultMatchers.status().isOk())
22+
.andExpect(MockMvcResultMatchers.content().string("Hello"));
23+
}
24+
}

0 commit comments

Comments
 (0)