G2A Many GEOs
SHARE
TWEET

Untitled

a guest Apr 6th, 2020 186 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @WebMvcTest(CategoryController.class)
  2. public class CategoryControllerTest {
  3.  
  4.     @MockBean
  5.     CategoryService categoryService;
  6.  
  7.     ObjectMapper mapper = new ObjectMapper();
  8.  
  9.     @Autowired
  10.     private MockMvc mockMvc;
  11.  
  12.     Category category;
  13.     CategoryDto categoryDto;
  14.  
  15.     @BeforeEach
  16.     public void init() {
  17.         categoryDto = new CategoryDto();
  18.         categoryDto.setId(15L);
  19.         categoryDto.setName("name");
  20.         categoryDto.setParentId(2L);
  21.  
  22.         category = new Category();
  23.         category.setId(categoryDto.getId());
  24.         category.setName(categoryDto.getName());
  25.         category.setParentId(categoryDto.getParentId());
  26.     }
  27.  
  28.     @Test
  29.     public void is_should_return_created_category() throws Exception {
  30.         when(categoryService.createCategory(any(CategoryDto.class))).thenReturn(categoryDto);
  31.  
  32.         mockMvc.perform(post("/api/categories/create")
  33.                 .content(mapper.writeValueAsString(categoryDto))
  34.                 .contentType(MediaType.APPLICATION_JSON))
  35.                 .andExpect(status().isOk())
  36.                 .andExpect(jsonPath("$.id").value(categoryDto.getId()))
  37.                 .andExpect(jsonPath("$.name").value(categoryDto.getName()))
  38.                 .andExpect(jsonPath("$.parentId").value(categoryDto.getParentId()));
  39.  
  40.     }
  41. }
  42.  
  43.  
  44. @Override
  45.     @Transactional
  46.     public CategoryDto createCategory(CategoryDto categoryDto) {
  47.         Category category = categoryMapper.toEntity(categoryDto);
  48.         categoryRepository.save(category);
  49.         categoryDto.setId(category.getId());
  50.         return categoryDto;
  51.     }
  52.  
  53.  
  54. @Data
  55. @JsonInclude(JsonInclude.Include.NON_NULL)
  56. public class CategoryDto {
  57.     private Long id;
  58.     @NotEmpty
  59.     private String name;
  60.     private Long parentId;
  61. }
  62.  
  63. @Entity
  64. @Table(name = "categories")
  65. @Getter
  66. @Setter
  67. public class Category extends BaseEntity {
  68.  
  69.     @Column(name = "name")
  70.     private String name;
  71.  
  72.     @Column(name = "parent_id")
  73.     private Long parentId;
  74.  
  75. }
  76.  
  77.   @PostMapping("/create")
  78.     public ResponseEntity<CategoryDto> createCategory(@RequestBody @Valid CategoryDto categoryDto) {
  79.         return ResponseEntity.ok(categoryService.createCategory(categoryDto));
  80.     }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top