Skip to content

Commit ab2c8ac

Browse files
committed
sonarqube comments on coverage addressed
1 parent 7d0152b commit ab2c8ac

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed
Binary file not shown.

microservices-self-registration/contextservice/src/test/java/com/learning/contextservice/ContextControllerTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
import com.learning.contextservice.client.GreetingServiceClient;
44
import com.learning.contextservice.controller.ContextController;
5+
import org.hamcrest.Matchers;
56
import org.junit.jupiter.api.Test;
67
import org.mockito.Mockito;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
911
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.context.annotation.Import;
1014
import org.springframework.http.MediaType;
1115
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1216
import org.springframework.test.web.servlet.MockMvc;
1317
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
1418
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1519

16-
@WebMvcTest(ContextController.class)
20+
@SpringBootTest(classes = ContextserviceApplication.class)
21+
@AutoConfigureMockMvc
22+
@Import(TestConfig.class)
1723
class ContextControllerTest {
1824

1925
@Autowired
@@ -34,4 +40,11 @@ void shouldReturnContextGreeting() throws Exception{
3440
.andExpect(MockMvcResultMatchers.status().isOk())
3541
.andExpect(MockMvcResultMatchers.content().string("The Greeting Service says: Mocked Hello from Chennai, Tamil Nadu, India"));
3642
}
43+
44+
@Test
45+
void shouldReturnContextServiceHealthStatusUp() throws Exception {
46+
mockMvc.perform(MockMvcRequestBuilders.get("/actuator/health"))
47+
.andExpect(MockMvcResultMatchers.status().isOk())
48+
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"status\":\"UP\"")));
49+
}
3750
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.learning.contextservice;
2+
3+
import com.learning.contextservice.client.GreetingServiceClient;
4+
import org.mockito.Mockito;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class TestConfig {
10+
11+
@Bean
12+
public GreetingServiceClient greetingServiceClient() {
13+
GreetingServiceClient mockClient = Mockito.mock(GreetingServiceClient.class);
14+
Mockito.when(mockClient.getGreeting()).thenReturn("Mocked Hello");
15+
return mockClient;
16+
}
17+
}

microservices-self-registration/greetingservice/src/test/java/com/learning/greetingservice/GreetingControllerTest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import org.junit.jupiter.api.Test;
44
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
56
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
7+
import org.springframework.boot.test.context.SpringBootTest;
68
import org.springframework.http.MediaType;
79
import org.springframework.test.web.servlet.MockMvc;
810
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
911
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1012

11-
@WebMvcTest(GreetingsController.class)
13+
@SpringBootTest(classes = GreetingserviceApplication.class)
14+
@AutoConfigureMockMvc
1215
class GreetingControllerTest {
1316

1417
@Autowired
@@ -21,4 +24,11 @@ void shouldReturnGreeting() throws Exception{
2124
.andExpect(MockMvcResultMatchers.status().isOk())
2225
.andExpect(MockMvcResultMatchers.content().string("Hello"));
2326
}
27+
28+
@Test
29+
void shouldReturnHealthStatusUp() throws Exception{
30+
mockMvc.perform(MockMvcRequestBuilders.get("/actuator/health"))
31+
.andExpect(MockMvcResultMatchers.status().isOk())
32+
.andExpect(MockMvcResultMatchers.content().string(org.hamcrest.Matchers.containsString("\"status\":\"UP\"")));
33+
}
2434
}

0 commit comments

Comments
 (0)