英文:
How to test @RestController when all of the mapping return with ResponseEntity?
问题
@RestController
@RequestMapping("/events")
public class EventController {
@Autowired
private EventRepository eventRepository;
@GetMapping("")
public ResponseEntity<Iterable<Event>> getAll() {
return ResponseEntity.ok(eventRepository.findAll());
}
@GetMapping("/{id}")
public ResponseEntity<Event> get(@PathVariable Integer id) {
Optional<Event> event = eventRepository.findById(id);
if (event.isPresent()) {
return ResponseEntity.ok(event.get());
} else {
return ResponseEntity.notFound().build();
}
}
@PostMapping("")
public ResponseEntity<Event> post(@RequestBody Event event) {
Event savedEvent = eventRepository.save(event);
return ResponseEntity.ok(savedEvent);
}
.
.
.
英文:
I need to do unit testing on a @RestController where every method returns with a ResponseEntity.
I have a CRUD repository to use but I don't know how can I test it with the ResponseEntities.
@RestController
@RequestMapping("/events")
public class EventController {
@Autowired
private EventRepository eventRepository;
@GetMapping("")
public ResponseEntity<Iterable<Event>> getAll() {
return ResponseEntity.ok(eventRepository.findAll());
}
@GetMapping("/{id}")
public ResponseEntity<Event> get(@PathVariable Integer id) {
Optional<Event> event= eventRepository.findById(id);
if (event.isPresent()) {
return ResponseEntity.ok(event.get());
} else {
return ResponseEntity.notFound().build();
}
}
@PostMapping("")
public ResponseEntity<Event> post(@RequestBody Event event) {
EventsavedEvent = eventRepository.save(event);
return ResponseEntity.ok(savedEvent);
}
.
.
.
答案1
得分: 1
迄今为止都还不错,我可以帮助你。
首先,你必须添加单元测试依赖。
然后你必须检查下面的代码。
下面的代码只包括创建部分。
祝你好运。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@ActiveProfiles("dev")
public class EventControllerTests {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void testCreateEvent() {
Event event = new Event(); // 你的实体
event.setEventName("Test"); // 你的实体属性
URI location = testRestTemplate.postForLocation("http://localhost:8080/events", event);
Event event2 = testRestTemplate.getForObject(location, Event.class);
MatcherAssert.assertThat(event2.getEventName(), Matchers.equalTo(event.getEventName()));
}
}
英文:
So far so good , I can help you .
First of all, you must add unit test dependency.
After that you must examine below code.
Below code only consist for create.
Good luck.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@ActiveProfiles("dev")
public class EventControllerTests {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void testCreateEvent() {
Event event = new Event(); // Your entity
event.setEventName("Test"); // Your entity attributes
URI location = testRestTemplate.postForLocation("http://localhost:8080/events", event);
Event event2 = testRestTemplate.getForObject(location, Event.class);
MatcherAssert.assertThat(event2.getEventName(), Matchers.equalTo(event.getEventName()));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论