I am trying to write test cases for below class which is giving me various exceptions on various different attempts. Can anyone suggest me on how to write a Test Class which can provide me a good coverage
My Class under test is-
@RestController
@RefreshScope
public class BookResource {
private final AtomicLong counter = new AtomicLong();
@Autowired
Environment environment;
private Logger logger = MyLoggerFactory.getLogger(getClass());
@Autowired
private BookRepository bookRepository;
@Value("${magazine.costPerBook:100}")
private String costPerTon;
@PostMapping( value= "v1/books",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
public Book createBook(@RequestBody Book book){
logger.info("received request to create a book. Current Market rate is {}",costPerBook);
bookRepository.save(book);
return book;
}
@GetMapping(value= "v1/books/{id}" , produces = MediaType.APPLICATION_JSON_VALUE)
public Book getBook(@PathVariable long id){
logger.info("Received request to read book {}", id);
Book book = bookRepository.findById(id);
return book;
}
@GetMapping(value= "v1/books" , produces = MediaType.APPLICATION_JSON_VALUE)
public Books getBooks(){
logger.info("Received request to read all books {}",costPerBook);
if (bookRepository.count() == 0 ){
logger.info("No objects found , adding sample objects for demonstration");
List<Book> books = new ArrayList<Book>();
books.add(new Book("atlantis"));
bookRepository.saveAll(books);
}
// Retrieve from repository
Iterable<Book> booksIterator = bookRepository.findAll();
Books books = new Books();
books.setBooks(booksIterator);
return books;
}
}
I am trying to write test cases for below class which is giving me various exceptions on various different attempts. Can anyone suggest me on how to write a Test Class which can provide me a good coverage
My Class under test is-
@RestController
@RefreshScope
public class BookResource {
private final AtomicLong counter = new AtomicLong();
@Autowired
Environment environment;
private Logger logger = MyLoggerFactory.getLogger(getClass());
@Autowired
private BookRepository bookRepository;
@Value("${magazine.costPerBook:100}")
private String costPerTon;
@PostMapping( value= "v1/books",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
public Book createBook(@RequestBody Book book){
logger.info("received request to create a book. Current Market rate is {}",costPerBook);
bookRepository.save(book);
return book;
}
@GetMapping(value= "v1/books/{id}" , produces = MediaType.APPLICATION_JSON_VALUE)
public Book getBook(@PathVariable long id){
logger.info("Received request to read book {}", id);
Book book = bookRepository.findById(id);
return book;
}
@GetMapping(value= "v1/books" , produces = MediaType.APPLICATION_JSON_VALUE)
public Books getBooks(){
logger.info("Received request to read all books {}",costPerBook);
if (bookRepository.count() == 0 ){
logger.info("No objects found , adding sample objects for demonstration");
List<Book> books = new ArrayList<Book>();
books.add(new Book("atlantis"));
bookRepository.saveAll(books);
}
// Retrieve from repository
Iterable<Book> booksIterator = bookRepository.findAll();
Books books = new Books();
books.setBooks(booksIterator);
return books;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.