Skip to content

Commit 8e50a08

Browse files
feature: reconcile tests with the records, 404 and RestClient changes
The test suite was written in parallel against the pre-modernization API: - Topic accessors are record components now (getId -> id) - getTopicWithId returns Optional and an unknown id is a 404, not a 500, so the placeholder assertions and their @disabled twins collapse into one - the RestTemplate bean is a RestClient bean Co-Authored-By: alex.vyshetsky <alex.vyshetsky@cognition.ai>
1 parent 2c8283c commit 8e50a08

3 files changed

Lines changed: 12 additions & 33 deletions

File tree

src/test/java/hello/ApplicationContextLoadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.springframework.boot.test.context.SpringBootTest;
66
import org.springframework.context.ApplicationContext;
77
import org.springframework.jdbc.core.JdbcTemplate;
8-
import org.springframework.web.client.RestTemplate;
8+
import org.springframework.web.client.RestClient;
99

1010
import hello.controller.GreetingController;
1111
import hello.controller.HelloController;
@@ -36,7 +36,7 @@ void exposesTheExpectedBeans() {
3636
assertThat(context.getBean(TopicController.class)).isNotNull();
3737
assertThat(context.getBean(HelloController.class)).isNotNull();
3838
assertThat(context.getBean(GreetingController.class)).isNotNull();
39-
assertThat(context.getBean(RestTemplate.class)).isNotNull();
39+
assertThat(context.getBean(RestClient.class)).isNotNull();
4040
assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
4141
}
4242

src/test/java/hello/controller/TopicNotFoundStatusTest.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package hello.controller;
22

3-
import org.junit.jupiter.api.Disabled;
43
import org.junit.jupiter.api.Test;
54
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.boot.test.context.SpringBootTest;
@@ -15,10 +14,8 @@
1514
* MockMvc rethrows unhandled exceptions instead of running the error dispatch, so this one case is
1615
* driven over a real connection.
1716
*
18-
* <p>{@code TopicService.getTopicWithId} calls {@code Optional.get()} on a miss, which throws
19-
* {@link java.util.NoSuchElementException} and surfaces as HTTP 500. That is asserted here as the
20-
* <em>current</em> behaviour; the disabled test below is the replacement for when a parallel change
21-
* turns the miss into a proper 404.
17+
* <p>{@code TopicService.getTopicWithId} returns an empty {@link java.util.Optional} on a miss, which
18+
* the controller translates into a 404.
2219
*/
2320
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2421
class TopicNotFoundStatusTest {
@@ -27,15 +24,7 @@ class TopicNotFoundStatusTest {
2724
private TestRestTemplate restTemplate;
2825

2926
@Test
30-
void unknownTopicIdCurrentlyReturnsInternalServerError() {
31-
ResponseEntity<String> response = restTemplate.getForEntity("/topic/does-not-exist", String.class);
32-
33-
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
34-
}
35-
36-
@Test
37-
@Disabled("Enable, and delete unknownTopicIdCurrentlyReturnsInternalServerError, once an unknown id is a 404")
38-
void unknownTopicIdShouldReturnNotFound() {
27+
void unknownTopicIdReturnsNotFound() {
3928
ResponseEntity<String> response = restTemplate.getForEntity("/topic/does-not-exist", String.class);
4029

4130
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);

src/test/java/hello/service/TopicServiceTest.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void createFreshService() {
3737
* records only has to touch one line in this class.
3838
*/
3939
private static List<String> idsOf(List<Topic> topics) {
40-
return topics.stream().map(Topic::getId).collect(Collectors.toList());
40+
return topics.stream().map(Topic::id).collect(Collectors.toList());
4141
}
4242

4343
@Test
@@ -48,26 +48,16 @@ void seedsThreeTopics() {
4848

4949
@Test
5050
void getTopicWithIdReturnsTheMatchingTopic() {
51-
Topic topic = topicService.getTopicWithId("java");
51+
Topic topic = topicService.getTopicWithId("java").orElseThrow();
5252

5353
assertThat(idsOf(List.of(topic))).containsExactly("java");
54-
assertThat(topic.getSubjectName()).isEqualTo("Core Java");
54+
assertThat(topic.subjectName()).isEqualTo("Core Java");
5555
}
5656

5757
@Test
58-
void getTopicWithIdThrowsForAnUnknownId() {
59-
// Current behaviour: Optional.get() on an empty Optional. Over HTTP this surfaces as a 500.
60-
assertThatThrownBy(() -> topicService.getTopicWithId("does-not-exist"))
61-
.isInstanceOf(NoSuchElementException.class);
62-
}
63-
64-
@Test
65-
@Disabled("Enable together with the parallel change that makes an unknown id a proper 404")
66-
void getTopicWithIdShouldReportAMissingTopicWithoutThrowing() {
67-
// Replacement for getTopicWithIdThrowsForAnUnknownId once the service returns an empty
68-
// Optional (or the controller translates the miss into HTTP 404).
69-
assertThatCode(() -> topicService.getTopicWithId("does-not-exist"))
70-
.doesNotThrowAnyException();
58+
void getTopicWithIdReportsAMissingTopicWithoutThrowing() {
59+
assertThatCode(() -> topicService.getTopicWithId("does-not-exist")).doesNotThrowAnyException();
60+
assertThat(topicService.getTopicWithId("does-not-exist")).isEmpty();
7161
}
7262

7363
@Test
@@ -83,7 +73,7 @@ void updateTopicReplacesTheTopicInPlace() {
8373

8474
List<Topic> topics = topicService.getAllTopics();
8575
assertThat(idsOf(topics)).containsExactly("spring", "java", "javascript");
86-
assertThat(topics.get(1).getSubjectName()).isEqualTo("Updated Java");
76+
assertThat(topics.get(1).subjectName()).isEqualTo("Updated Java");
8777
}
8878

8979
@Test

0 commit comments

Comments
 (0)