最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Testing RestController class with HATEOAS links - Stack Overflow

programmeradmin0浏览0评论

I am working through the Amazon Junior Dev Certificate on Coursera and finishing up the course on Spring Boot APIs. The current lab I am working on is requiring me to write unit tests for a RestController that makes use of HATEOAS links and I am at my wits end trying to find a way to test these methods without entirely reconstructing the original methods.

I have actually tried to reconstruct the original method within the test and that almost worked except that the tests failed because they were comparing object references, and since the expected and actual methods ran separately it generated separate object references.

Code:

@Test
    void getBookByIdTest(){
        long nullId = 100L;
        assertEquals(ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error: Book with ID 100 not found."),bookController.getBookById(nullId));
        String baseURL = testRestTemplate.getRootUri();
      //   Available
        long availableId = 1L;
        Book bookAvailable = bookService.findBookById(availableId);
        EntityModel<Book> availEntity = EntityModel.of(bookAvailable);     

availEntity.add(linkTo(methodOn(BookController.class).getBookById(availableId)).withSelfRel());   

availEntity.add(linkTo(methodOn(BookController.class).borrowBook(availableId)).withRel("borrow"));

assertEquals(ResponseEntity.ok(availEntity),bookController.getBookById(1));
        
       // Unavailable

        long unavailableId = 2L;
        Book bookUnavailable = bookService.findBookById(unavailableId);
        EntityModel<Book> unavailEntity = EntityModel.of(bookUnavailable);
       unavailEntity.add(linkTo(methodOn(BookController.class).getBookById(unavailableId)).withSelfRel());
        unavailEntity.add(linkTo(methodOn(BookController.class).returnBook(unavailableId)).withRel("return"));
        assertEquals(ResponseEntity.ok(unavailEntity),bookController.getBookById(2));

I am currently trying to just compare the links with assertTrue(entity.hasLink("self") etc. but that is also failing.

The exercise does not mention using Mock MVC or Mockito so I am assuming they want me to write a simple unit test.

What approach would you all recommend I take here?

Thank you!

PS: I am including the code for the first controller method I am trying to test:

 @GetMapping("/{id}")
    public ResponseEntity<?> getBookById(@PathVariable long id) {
        Book foundBook = bookService.findBookById(id);
        if (foundBook == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error: Book with ID " + id + " not found.");
        }
        EntityModel<Book> resource = EntityModel.of(foundBook);
        resource.add(
                    linkTo(
                            methodOn(BookController.class).getBookById(id)).
                                withSelfRel());

        if (foundBook.isAvailable()) {
            resource.add(
                        linkTo(
                                methodOn(BookController.class).borrowBook(id)).
                                    withRel("borrow"));
        } else {
            resource.add(
                        linkTo(
                                methodOn(BookController.class).returnBook(id)).
                                    withRel("return"));
        }

        return ResponseEntity.ok(resource);
    }
发布评论

评论列表(0)

  1. 暂无评论