Skip to content

Commit d825de9

Browse files
AMP-135 - fixed test issues.
1 parent 1a65be8 commit d825de9

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

src/test/java/uk/gov/hmcts/cp/integration/ExampleControllerIntegrationTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.gov.hmcts.cp.integration;
22

3+
import org.junit.jupiter.api.BeforeEach;
34
import org.junit.jupiter.api.Test;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.transaction.annotation.Transactional;
@@ -14,13 +15,24 @@ class ExampleControllerIntegrationTest extends IntegrationTestBase {
1415
@Autowired
1516
ExampleRepository exampleRepository;
1617

18+
private ExampleEntity entity;
19+
20+
@BeforeEach
21+
void setup() {
22+
entity = exampleRepository.save(
23+
ExampleEntity.builder()
24+
.exampleText("Welcome to service-hmcts-springboot-template")
25+
.build()
26+
);
27+
}
28+
1729
@Transactional
1830
@Test
1931
void endpoint_should_return_ok() throws Exception {
20-
ExampleEntity exampleEntity = insertExample("Some random text");
21-
mockMvc.perform(get("/example/{id}", exampleEntity.getId()))
32+
mockMvc.perform(get("/example/{id}", entity.getId()))
2233
.andDo(print())
2334
.andExpect(status().isOk());
2435
}
2536

37+
2638
}
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
package uk.gov.hmcts.cp.integration;
22

3+
import org.junit.jupiter.api.BeforeEach;
34
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import uk.gov.hmcts.cp.entities.ExampleEntity;
7+
import uk.gov.hmcts.cp.repositories.ExampleRepository;
48

59
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
610
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
711

812
class RootControllerIntegrationTest extends IntegrationTestBase {
913

14+
@Autowired
15+
ExampleRepository exampleRepository;
16+
17+
private ExampleEntity entity;
18+
19+
@BeforeEach
20+
void setup() {
21+
entity = exampleRepository.save(
22+
ExampleEntity.builder()
23+
.exampleText("Welcome to service-hmcts-springboot-template")
24+
.build()
25+
);
26+
}
27+
1028
@Test
1129
void root_endpoint_should_be_ok() throws Exception {
12-
mockMvc.perform(get("/example/{example_id}", 1))
30+
mockMvc.perform(get("/example/{example_id}", entity.getId()))
1331
.andExpect(status().isOk());
1432
}
1533
}

src/test/java/uk/gov/hmcts/cp/integration/TracingIntegrationTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;
88
import org.slf4j.MDC;
9+
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.beans.factory.annotation.Value;
1011
import org.springframework.test.web.servlet.MvcResult;
1112
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
1213
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
import uk.gov.hmcts.cp.entities.ExampleEntity;
15+
import uk.gov.hmcts.cp.repositories.ExampleRepository;
1316

1417
import java.io.ByteArrayOutputStream;
1518
import java.io.PrintStream;
@@ -37,12 +40,21 @@ class TracingIntegrationTest extends IntegrationTestBase {
3740

3841
private final PrintStream originalStdOut = System.out;
3942

43+
@Autowired
44+
ExampleRepository exampleRepository;
45+
46+
private ExampleEntity entity;
47+
4048
@BeforeEach
41-
public void setUp() {
42-
// Manually populate MDC with trace information similar to TracingFilter
49+
void setup() {
4350
MDC.put(TRACE_ID_FIELD, TEST_TRACE_ID_1);
4451
MDC.put(SPAN_ID_FIELD, TEST_SPAN_ID_1);
4552
MDC.put("applicationName", springApplicationName);
53+
entity = exampleRepository.save(
54+
ExampleEntity.builder()
55+
.exampleText("Welcome to service-hmcts-springboot-template")
56+
.build()
57+
);
4658
}
4759

4860
@AfterEach
@@ -86,7 +98,7 @@ private ByteArrayOutputStream captureStdOut() {
8698
private MvcResultHelper performRequestAndCaptureLogs(final String path, final String traceId, final String spanId) throws Exception {
8799
final ByteArrayOutputStream capturedStdOut = captureStdOut();
88100

89-
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path, 1);
101+
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path, entity.getId());
90102
if (traceId != null) {
91103
requestBuilder = requestBuilder.header(TRACE_ID_FIELD, traceId);
92104
}
@@ -130,7 +142,7 @@ private void assertTracingFields(final Map<String, Object> log, final String exp
130142

131143
private void assertCommonLogFields(final Map<String, Object> log) {
132144
assertThat(log.get("logger_name")).isEqualTo("uk.gov.hmcts.cp.controllers.ExampleController");
133-
assertThat(log.get("message")).isEqualTo("getExampleByExampleId example for 1");
145+
assertThat(log.get("message")).isEqualTo("getExampleByExampleId example for "+entity.getId());
134146
}
135147

136148
private void assertResponseHeaders(final MvcResult result, final String expectedTraceId, final String expectedSpanId) {

0 commit comments

Comments
 (0)