|
1 |
| -Write unit test for following code. |
2 |
| -You MUST return code only, not explain. |
3 |
| -You MUST use given-when-then style. |
4 |
| -You MUST use should_xx style for test method name. |
5 |
| -When testing controller, you MUST use MockMvc and test API only. |
| 1 | +Write unit test for following code. You MUST Use English to reply me! |
6 | 2 | You are working on a project that uses Spring MVC,Spring WebFlux,JDBC to build RESTful APIs.
|
7 |
| -// class name: BookMeetingRoomRequest |
8 |
| -// class fields: meetingRoomId |
9 |
| -// class methods: |
10 |
| -// super classes: [Object] |
11 |
| -// |
12 |
| -// class name: BookMeetingRoomResponse |
13 |
| -// class fields: bookingId meetingRoomId userId startTime endTime |
14 |
| -// class methods: |
15 |
| -// super classes: [Object] |
16 |
| -// |
| 3 | +You MUST use should_xx_xx style for test method name. |
| 4 | +You MUST use given-when-then style. |
| 5 | +- Test file should be complete and compilable, without need for further actions. |
| 6 | +- Ensure that each test focuses on a single use case to maintain clarity and readability. |
| 7 | +- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parameterized tests. |
| 8 | + | This project uses JUnit 5, you should import `org.junit.jupiter.api.Test` and use `@Test` annotation.- You MUST use MockMvc and test API only. |
| 9 | +- Use appropriate Spring test annotations such as `@MockBean`, `@Autowired`, `@WebMvcTest`, `@DataJpaTest`, `@AutoConfigureTestDatabase`, `@AutoConfigureMockMvc`, `@SpringBootTest` etc. |
| 10 | + |
| 11 | + |
| 12 | +// here are related classes: |
| 13 | +// 'package: cc.unitmesh.untitled.demo.service.BlogService |
| 14 | +// class BlogService { |
| 15 | +// blogRepository |
| 16 | +// + public BlogPost createBlog(BlogPost blogDto) |
| 17 | +// + public BlogPost getBlogById(Long id) |
| 18 | +// + public BlogPost updateBlog(Long id, BlogPost blogDto) |
| 19 | +// + public void deleteBlog(Long id) |
| 20 | +// } |
| 21 | +// 'package: cc.unitmesh.untitled.demo.service.BlogService |
| 22 | +// class BlogService { |
| 23 | +// blogRepository |
| 24 | +// + public BlogPost createBlog(BlogPost blogDto) |
| 25 | +// + public BlogPost getBlogById(Long id) |
| 26 | +// + public BlogPost updateBlog(Long id, BlogPost blogDto) |
| 27 | +// + public void deleteBlog(Long id) |
| 28 | +// } |
| 29 | +// 'package: cc.unitmesh.untitled.demo.entity.BlogPost |
| 30 | +// class BlogPost { |
| 31 | +// id |
| 32 | +// title |
| 33 | +// content |
| 34 | +// author |
| 35 | +// + public BlogPost(String title, String content, String author) |
| 36 | +// + public BlogPost() |
| 37 | +// + public void setId(Long id) |
| 38 | +// + public Long getId() |
| 39 | +// + public String getTitle() |
| 40 | +// + public void setTitle(String title) |
| 41 | +// + public String getContent() |
| 42 | +// + public void setContent(String content) |
| 43 | +// + public String getAuthor() |
| 44 | +// + public void setAuthor(String author) |
| 45 | +// } |
| 46 | +// 'package: cc.unitmesh.untitled.demo.dto.CreateBlogRequest |
| 47 | +// class CreateBlogRequest { |
| 48 | +// title |
| 49 | +// content |
| 50 | +// user |
| 51 | +// |
| 52 | +// } |
| 53 | +Code: |
17 | 54 | ```java
|
18 |
| -@PostMapping("/{meetingRoomId}/book") |
19 |
| - public ResponseEntity<BookMeetingRoomResponse> bookMeetingRoom(@PathVariable String meetingRoomId, @RequestBody BookMeetingRoomRequest request) { |
20 |
| - // 业务逻辑 |
21 |
| - BookMeetingRoomResponse response = new BookMeetingRoomResponse(); |
22 |
| - // 设置 response 的属性 |
23 |
| - return new ResponseEntity<>(response, HttpStatus.CREATED); |
| 55 | +@RestController |
| 56 | +@RequestMapping("/blog") |
| 57 | +public class BlogController { |
| 58 | + BlogService blogService; |
| 59 | + |
| 60 | + public BlogController(BlogService blogService) { |
| 61 | + this.blogService = blogService; |
| 62 | + } |
| 63 | + |
| 64 | + @ApiOperation(value = "Get Blog by id") |
| 65 | + @GetMapping("/{id}") |
| 66 | + public BlogPost getBlog(@PathVariable Long id) { |
| 67 | + return blogService.getBlogById(id); |
| 68 | + } |
| 69 | + |
| 70 | + @ApiOperation(value = "Create a new blog") |
| 71 | + @PostMapping("/") |
| 72 | + public BlogPost createBlog(@RequestBody CreateBlogRequest request) { |
| 73 | + BlogPost blogPost = new BlogPost(); |
| 74 | + BeanUtils.copyProperties(request, blogPost); |
| 75 | + return blogService.createBlog(blogPost); |
24 | 76 | }
|
| 77 | +} |
25 | 78 | ```
|
26 |
| -Start with `import` syntax here: |
| 79 | +Start with `import` syntax here: |
0 commit comments