Search in sources :

Example 1 with BookDto

use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.

the class BookTests method testPost.

@Test
void testPost() throws Exception {
    BookDto dto = new BookDto(null, "Book 10", "Author 10");
    MvcResult res = mvc.perform(MockMvcRequestBuilders.post("/books").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isCreated()).andReturn();
    BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
    assertNotNull(resDto);
}
Also used : BookDto(com.example.demo.dto.BookDto) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with BookDto

use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.

the class BookTests method testGet.

@Test
void testGet() throws Exception {
    for (BookDto dto : bookDtos) {
        MvcResult res = mvc.perform(MockMvcRequestBuilders.get("/books/{id}", dto.getId()).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
        assertEquals(dto, resDto);
    }
}
Also used : BookDto(com.example.demo.dto.BookDto) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with BookDto

use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.

the class BookTests method testGetByTitle.

@Test
void testGetByTitle() throws Exception {
    for (BookDto dto : bookDtos) {
        MvcResult res = mvc.perform(MockMvcRequestBuilders.get("/books").param("title", dto.getTitle()).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        List<BookDto> resDtos = objectMapper.readValue(res.getResponse().getContentAsString(), new TypeReference<ArrayList<BookDto>>() {
        });
        assertEquals(1, resDtos.size());
        assertEquals(dto, resDtos.get(0));
    }
}
Also used : BookDto(com.example.demo.dto.BookDto) ArrayList(java.util.ArrayList) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with BookDto

use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.

the class BookTests method init.

@BeforeEach
void init() throws Exception {
    for (BookDto dto : bookDtos) {
        MvcResult res = mvc.perform(MockMvcRequestBuilders.post("/books").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isCreated()).andReturn();
        BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
        dto.setId(resDto.getId());
    }
}
Also used : BookDto(com.example.demo.dto.BookDto) MvcResult(org.springframework.test.web.servlet.MvcResult) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with BookDto

use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.

the class BookTests method testPut.

@Test
void testPut() throws Exception {
    List<BookDto> modifiedDtos = bookDtos.stream().map(dto -> new BookDto(dto.getId(), dto.getTitle().toUpperCase(), dto.getAuthor().toLowerCase())).collect(Collectors.toList());
    for (BookDto dto : modifiedDtos) {
        MvcResult res = mvc.perform(MockMvcRequestBuilders.put("/books/{id}", dto.getId()).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
        assertNotNull(resDto);
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) BookDto(com.example.demo.dto.BookDto) MockMvcResultMatchers(org.springframework.test.web.servlet.result.MockMvcResultMatchers) Collectors(java.util.stream.Collectors) MockMvcRequestBuilders(org.springframework.test.web.servlet.request.MockMvcRequestBuilders) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) MockMvc(org.springframework.test.web.servlet.MockMvc) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) AutoConfigureMockMvc(org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) BeforeAll(org.junit.jupiter.api.BeforeAll) MvcResult(org.springframework.test.web.servlet.MvcResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Transactional(org.springframework.transaction.annotation.Transactional) BookDto(com.example.demo.dto.BookDto) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

BookDto (com.example.demo.dto.BookDto)6 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 MvcResult (org.springframework.test.web.servlet.MvcResult)5 ArrayList (java.util.ArrayList)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Book (com.example.demo.entity.Book)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 AfterAll (org.junit.jupiter.api.AfterAll)1 AfterEach (org.junit.jupiter.api.AfterEach)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 AutoConfigureMockMvc (org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc)1 MediaType (org.springframework.http.MediaType)1