Skip to content

Commit c238d4a

Browse files
committed
#23 Clean Test Code
- Simplify by removing unnecessary setup method. - Use distinct values in fixture to make it easier spotting bugs. Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 158ba74 commit c238d4a

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/test/java/io/securecodebox/persistence/defectdojo/service/UserProfileServiceTest.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,37 @@
88
import io.securecodebox.persistence.defectdojo.config.Config;
99
import io.securecodebox.persistence.defectdojo.model.User;
1010
import io.securecodebox.persistence.defectdojo.model.UserProfile;
11-
import org.junit.jupiter.api.BeforeEach;
1211
import org.junit.jupiter.api.Test;
1312
import org.springframework.http.MediaType;
1413
import org.springframework.test.web.client.MockRestServiceServer;
1514

1615
import java.net.URISyntaxException;
1716
import java.util.Arrays;
17+
import java.util.List;
1818

1919
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
2020
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
2121
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
2222

2323

24-
// This test is special because the defectdojo api does not return a list,
25-
// but the generic code assumes every endpoint returns a list
24+
/**
25+
* Tests for UserProfileService
26+
* <p>
27+
* This test is special because the defectdojo api does not return a list, but the generic code assumes every endpoint
28+
* returns a list.
29+
* </p>
30+
*/
2631
class UserProfileServiceTest {
27-
28-
private Config config;
29-
private UserProfileService underTest;
30-
private MockRestServiceServer mockServer;
31-
32-
// This string does not contain every field of the api response as those are not implemented
33-
private String apiResponse = """
32+
/**
33+
* This string does not contain every field of the api response as those are not implemented
34+
*/
35+
private static final String API_RESPONSE = """
3436
{
3537
"user": {
3638
"id": 0,
37-
"username": "GdqmXprK.j7R+OYE49SzL3mM2U6I0DyLRHnDg87i9It0AfP-kxvswW3qOI2i+31-@0",
38-
"first_name": "string",
39-
"last_name": "string",
39+
"username": "username",
40+
"first_name": "first_name",
41+
"last_name": "last_name",
4042
"email": "user@example.com",
4143
"last_login": "2022-11-01T16:20:19.373Z",
4244
"is_active": true,
@@ -46,24 +48,18 @@ class UserProfileServiceTest {
4648
}
4749
""";
4850

49-
@BeforeEach
50-
void setup() {
51-
config = new Config("https://defectdojo.example.com", "abc", 42);
52-
underTest = new UserProfileService(config);
53-
mockServer = MockRestServiceServer.createServer(underTest.getRestTemplate());
54-
}
51+
private final Config config = new Config("https://defectdojo.example.com", "abc", 42);
52+
private final UserProfileService sut = new UserProfileService(config);
53+
private final MockRestServiceServer server = MockRestServiceServer.createServer(sut.getRestTemplate());
5554

5655
@Test
57-
void testSearch() throws JsonProcessingException, URISyntaxException {
58-
var url = config.getUrl() + "/api/v2/" + underTest.getUrlPath() + "/?offset=0&limit=100";
59-
mockServer.expect(requestTo(url)).andRespond(withSuccess(apiResponse, MediaType.APPLICATION_JSON));
56+
void search() throws JsonProcessingException, URISyntaxException {
57+
final var url = String.format("%s/api/v2/%s/?offset=0&limit=100", config.getUrl(), sut.getUrlPath());
58+
server.expect(requestTo(url)).andRespond(withSuccess(API_RESPONSE, MediaType.APPLICATION_JSON));
6059

61-
var user = new User(0L, "GdqmXprK.j7R+OYE49SzL3mM2U6I0DyLRHnDg87i9It0AfP-kxvswW3qOI2i+31-@0", "string", "string");
62-
var userProfile = new UserProfile(user);
63-
var expected = Arrays.asList(userProfile);
64-
var actual = underTest.search();
60+
final var expected = new UserProfile(new User(0L, "username", "first_name", "last_name"));
6561

66-
mockServer.verify();
67-
assertIterableEquals(expected, actual);
62+
assertIterableEquals(List.of(expected), sut.search());
63+
server.verify();
6864
}
6965
}

0 commit comments

Comments
 (0)