Добавил параметризованные тесты имён и проверку получения счёта по Location
This commit is contained in:
1 parent
6dec1fe840
commit
0af9111103
3 files changed
+112
-12
No files matched your search
@@ -0,0 +1,30 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class AccountTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullAndEmptySource
|
||||||
|
@ValueSource(strings = {" ", "\t", "\n"})
|
||||||
|
void creatingAccountWithInvalidNameThrowsException(String name) {
|
||||||
|
InvalidAccountNameException exception = assertThrows(InvalidAccountNameException.class,
|
||||||
|
() -> new Account(1L, name));
|
||||||
|
assertEquals("Не указано имя счёта", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullAndEmptySource
|
||||||
|
@ValueSource(strings = {" ", "\t", "\n"})
|
||||||
|
void renamingAccountWithInvalidNameThrowsExceptionAndPreservesName(String name) {
|
||||||
|
Account account = new Account(1L, "Карта");
|
||||||
|
InvalidAccountNameException exception = assertThrows(InvalidAccountNameException.class,
|
||||||
|
() -> account.setName(name));
|
||||||
|
assertEquals("Не указано имя счёта", exception.getMessage());
|
||||||
|
assertEquals("Карта", account.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class CategoryTest {
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullAndEmptySource
|
||||||
|
@ValueSource(strings = {" ", "\t", "\n"})
|
||||||
|
void creatingCategoryWithInvalidNameThrowsException(String name) {
|
||||||
|
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> new Category(1L, name));
|
||||||
|
assertEquals("Не указано имя категории", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullAndEmptySource
|
||||||
|
@ValueSource(strings = {" ", "\t", "\n"})
|
||||||
|
void renamingCategoryWithInvalidNameThrowsExceptionAndPreservesName(String name) {
|
||||||
|
Category category = new Category(1L, "Продукты");
|
||||||
|
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> category.setName(name));
|
||||||
|
assertEquals("Не указано имя категории", exception.getMessage());
|
||||||
|
assertEquals("Продукты", category.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,33 +2,42 @@ package net.sytes.kashey.easymoney.api;
|
|||||||
|
|
||||||
import net.sytes.kashey.easymoney.config.AccountingConfig;
|
import net.sytes.kashey.easymoney.config.AccountingConfig;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
|
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
|
import tools.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@WebMvcTest(AccountController.class)
|
@WebMvcTest(AccountController.class)
|
||||||
@Import(AccountingConfig.class)
|
@Import(AccountingConfig.class)
|
||||||
class AccountControllerTest {
|
class AccountControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
@Autowired
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void creatingAccountReturnsCreatedAccountAndLocation() throws Exception {
|
void creatingAccountReturnsCreatedAccountAndLocation() throws Exception {
|
||||||
|
// Given
|
||||||
|
String body = objectMapper.writeValueAsString(new CreateAccountRequest("Карта"));
|
||||||
|
|
||||||
// When / Then
|
// When / Then
|
||||||
mockMvc.perform(post("/api/accounts")
|
mockMvc.perform(post("/api/accounts")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content("""
|
|
||||||
{
|
.content(body))
|
||||||
"name":"Карта"
|
|
||||||
}
|
|
||||||
"""))
|
|
||||||
.andExpect(status().isCreated())
|
.andExpect(status().isCreated())
|
||||||
.andExpect(header().exists("Location"))
|
.andExpect(header().exists("Location"))
|
||||||
.andExpect(jsonPath("$.id").isNumber())
|
.andExpect(jsonPath("$.id").isNumber())
|
||||||
@@ -44,17 +53,49 @@ class AccountControllerTest {
|
|||||||
.andExpect(jsonPath("$.detail").value("Счет не найден: 999"));
|
.andExpect(jsonPath("$.detail").value("Счет не найден: 999"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@ParameterizedTest
|
||||||
void creatingAccountWithInvalidNameReturnsBadRequestAnswer() throws Exception {
|
@NullAndEmptySource
|
||||||
|
@ValueSource(strings = {" ", "\t", "\n"})
|
||||||
|
void creatingAccountWithInvalidNameReturnsBadRequestAnswer(String name) throws Exception {
|
||||||
|
String body = objectMapper.writeValueAsString(new CreateAccountRequest(name));
|
||||||
|
|
||||||
mockMvc.perform(post("/api/accounts")
|
mockMvc.perform(post("/api/accounts")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content("""
|
.content(body))
|
||||||
{
|
|
||||||
"name":""
|
|
||||||
}
|
|
||||||
"""))
|
|
||||||
.andExpect(status().isBadRequest())
|
.andExpect(status().isBadRequest())
|
||||||
.andExpect(jsonPath("$.status").value(400))
|
.andExpect(jsonPath("$.status").value(400))
|
||||||
.andExpect(jsonPath("$.detail").value("Не указано имя счёта"));
|
.andExpect(jsonPath("$.detail").value("Не указано имя счёта"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void creatingAccountWithAbsentNameReturnsBadRequestAnswer() throws Exception {
|
||||||
|
mockMvc.perform(post("/api/accounts")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{}"))
|
||||||
|
.andExpect(status().isBadRequest())
|
||||||
|
.andExpect(jsonPath("$.status").value(400))
|
||||||
|
.andExpect(jsonPath("$.detail").value("Не указано имя счёта"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createdAccountCanBeRetrievedByLocation() throws Exception {
|
||||||
|
// Given
|
||||||
|
String body = objectMapper.writeValueAsString(new CreateAccountRequest("Карта"));
|
||||||
|
|
||||||
|
MvcResult result = mockMvc.perform(post("/api/accounts")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(body))
|
||||||
|
.andExpect(status().isCreated())
|
||||||
|
.andReturn();
|
||||||
|
|
||||||
|
String location = result.getResponse().getHeader("Location");
|
||||||
|
assertNotNull(location, "POST должен возвращать заголовок Location");
|
||||||
|
AccountResponse createdAccount = objectMapper.readValue(result.getResponse().getContentAsString(), AccountResponse.class);
|
||||||
|
|
||||||
|
// When / Then
|
||||||
|
mockMvc.perform(get(location))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.id", Long.class).value(createdAccount.id()))
|
||||||
|
.andExpect(jsonPath("$.name").value("Карта"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user