如何在所有映射都返回ResponseEntity的情况下测试@RestController?

huangapple go评论77阅读模式
英文:

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(&quot;/events&quot;)
public class EventController {
    @Autowired
    private EventRepository eventRepository;

    @GetMapping(&quot;&quot;)
    public ResponseEntity&lt;Iterable&lt;Event&gt;&gt; getAll() {
        return ResponseEntity.ok(eventRepository.findAll());
    }

    @GetMapping(&quot;/{id}&quot;)
    public ResponseEntity&lt;Event&gt; get(@PathVariable Integer id) {
        Optional&lt;Event&gt; event= eventRepository.findById(id);
        if (event.isPresent()) {
            return ResponseEntity.ok(event.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping(&quot;&quot;)
    public ResponseEntity&lt;Event&gt; 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(&quot;dev&quot;)
public class EventControllerTests {

    @Autowired
	private TestRestTemplate testRestTemplate;

        @Test
    	public void testCreateEvent() {
    		Event event = new Event(); // Your entity
    		event.setEventName(&quot;Test&quot;); // Your entity attributes
    
    		URI location = testRestTemplate.postForLocation(&quot;http://localhost:8080/events&quot;, event);
    
    		Event event2 = testRestTemplate.getForObject(location, Event.class);
    
    		MatcherAssert.assertThat(event2.getEventName(), Matchers.equalTo(event.getEventName()));
    		
    
    	}

}

huangapple
  • 本文由 发表于 2020年4月10日 23:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61143558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定