You have two components:
@Component
public class BookService {
public int getBooksCountByAuthor(String author){
// should access db and return a desired result
// but in our case it will be a hard-coded number
return 20;
}
public int getAllBooksCount(){
// should access db and return a desired result
// but in our case it will be a hard-coded number
return 100;
}
}
@Component
public class BookStatisticsService {
private final BookService bookService;
public BookStatisticsService(BookService bookService) {
this.bookService = bookService;
}
public double getPercentageOfBooksByAuthor(String author) {
int allBooksCount = bookService.getAllBooksCount();
if (allBooksCount == 0) return 0;
return (double)
bookService.getBooksCountByAuthor(author) /
allBooksCount * 100;
}
}
The following test checks the getPercentageOfBooksByAuthor method:
@SpringBootTest
public class BookStatisticsServiceTest {
@MockBean
private BookService bookService;
@Autowired
private BookStatisticsService bookStatisticsService;
@Test
void getPercentageOfBooksByAuthor() {
double actual = bookStatisticsService.getPercentageOfBooksByAuthor("Jane Austen");
double expected = ??
Assertions.assertThat(actual).isEqualTo(expected, withPrecision(0.0000001));
}
}
What should the expected variable be equal to in order to pass the test?