From 1b38f7c5ccb035d0adc5de104e5f23d9177d81cb Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Tue, 19 Nov 2024 13:34:31 +0800 Subject: [PATCH] Move default params function to static --- .../SpringSimpleBackendApplicationTests.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/leungcheng/spring_simple_backend/SpringSimpleBackendApplicationTests.java b/src/test/java/com/leungcheng/spring_simple_backend/SpringSimpleBackendApplicationTests.java index 5671222..746baec 100644 --- a/src/test/java/com/leungcheng/spring_simple_backend/SpringSimpleBackendApplicationTests.java +++ b/src/test/java/com/leungcheng/spring_simple_backend/SpringSimpleBackendApplicationTests.java @@ -50,7 +50,7 @@ private void clearAccessToken() { } private String useNewUserAccessToken() throws Exception { - UserCredentials userCredentials = sampleUserCredentials(); + UserCredentials userCredentials = UserCredentials.sample(); signup(userCredentials).andExpect(status().isCreated()); User user = userRepository.findByUsername(userCredentials.username).orElseThrow(); @@ -71,7 +71,7 @@ private String useNewUserAccessToken() throws Exception { public void shouldCreateAndGetProduct() throws Exception { useNewUserAccessToken(); - CreateProductParams params = validParams(); + CreateProductParams params = CreateProductParams.sample(); MvcResult mvcResult = createProduct(params) .andExpect(status().isCreated()) @@ -107,11 +107,11 @@ public void shouldIgnoreIdWhenCreateProduct() throws Exception { public void shouldRejectCreateProductWithInvalidData() throws Exception { useNewUserAccessToken(); - CreateProductParams params = validParams(); + CreateProductParams params = CreateProductParams.sample(); params.name = ""; createProduct(params).andExpect(status().isBadRequest()); - params = validParams(); + params = CreateProductParams.sample(); params.price = -1; createProduct(params).andExpect(status().isBadRequest()); } @@ -150,7 +150,7 @@ public void shouldRejectLoginWithNonexistentUsername() throws Exception { @Test public void shouldRejectNonAuthApiCallWithoutToken() throws Exception { clearAccessToken(); - createProduct(validParams()).andExpect(status().isForbidden()); + createProduct(CreateProductParams.sample()).andExpect(status().isForbidden()); } @Test @@ -162,7 +162,7 @@ public void shouldRejectIfAuthHeaderIsNotSetCorrectly() throws Exception { post("/products") .contentType("application/json") .header("Authorization", "NotBearer " + accessToken.orElseThrow()) - .content(validParams().toContent())) + .content(CreateProductParams.sample().toContent())) .andExpect(status().isForbidden()); mockMvc @@ -170,7 +170,7 @@ public void shouldRejectIfAuthHeaderIsNotSetCorrectly() throws Exception { post("/products") .contentType("application/json") .header("Authorization", "Bearer ") - .content(validParams().toContent())) + .content(CreateProductParams.sample().toContent())) .andExpect(status().isForbidden()); } @@ -178,7 +178,7 @@ public void shouldRejectIfAuthHeaderIsNotSetCorrectly() throws Exception { public void shouldCreateProductWithUserIdSameAsCreator() throws Exception { String userId = useNewUserAccessToken(); - CreateProductParams params = validParams(); + CreateProductParams params = CreateProductParams.sample(); createProduct(params) .andExpect(status().isCreated()) .andExpect(jsonPath("$.userId").value(userId)); @@ -189,6 +189,10 @@ private static class CreateProductParams { double price; int quantity; + private static CreateProductParams sample() { + return new CreateProductParams("Product 1", 1.0, 50); + } + private CreateProductParams(String name, double price, int quantity) { this.name = name; this.price = price; @@ -206,10 +210,6 @@ public String toContent() { } } - private CreateProductParams validParams() { - return new CreateProductParams("Product 1", 1.0, 50); - } - private ResultActions createProduct(CreateProductParams params) throws Exception { MockHttpServletRequestBuilder builder = post("/products").contentType("application/json").content(params.toContent()); @@ -227,6 +227,10 @@ private static class UserCredentials { String username; String password; + private static UserCredentials sample() { + return new UserCredentials("sample-user", "sample-password"); + } + private UserCredentials(String username, String password) { this.username = username; this.password = password; @@ -237,10 +241,6 @@ public String toContent() { } } - private UserCredentials sampleUserCredentials() { - return new UserCredentials("sample-user", "sample-password"); - } - private ResultActions signup(UserCredentials userCredentials) throws Exception { return mockMvc.perform( post("/signup").contentType("application/json").content(userCredentials.toContent()));