Testing
Fakes, Parameterized tests and others crazy tools
First of all, we have to say that this scraper test is duplicated in both projects (yeah we do copy paste it, do not judge us) because the tests don't depend on any framework restriction. These are UVAScraperTests.java and UVAScraperTests.java ( same name but the links are different).
All the tests are integration ones because we wanted to test that http interaction.
So why is it special? We don't include nothing in the core framework in these tests but we want to remark as an example because we use two important testing concepts.
A fake implementation, in this case, a static method that returns the UVAExchange objects. It's just hardcoded but it's a valid implementation for theses tests but not for production:
private static List<UVAExchange> fakeSomeUVAExchanges() {
List<UVAExchange> exchanges = new ArrayList<UVAExchange>();
exchanges.add(new UVAExchange(LocalDate.parse("16/05/2019", DateTimeFormatter.ofPattern("dd/MM/yyyy")),
BigDecimal.valueOf(36.3)));
exchanges.add(new UVAExchange(LocalDate.parse("17/05/2019", DateTimeFormatter.ofPattern("dd/MM/yyyy")),
BigDecimal.valueOf(36.34)));
...
If you want to know more about fakes and its differences with mocks and stubs we strongly advise you to read this post of Martin Fowler
@ParameterizedTest: you could note that there is only one test method with this annotation when you run it two execution would run. This cool junit feature allows you to repeat a test with different data sources. The better resources to learn about this functionality is the official junit documentation.
One little last thing to note is the use of Java 8 Stream API and lamdas to make tests easier to read:
private static List<UVAExchange> filterExchanges(List<UVAExchange> fakedExchanges, Integer year, Integer month){
return fakedExchanges.stream().filter(u->{
LocalDate date = u.getDate();
LocalDate startDate = LocalDate.of(year, month, 15);
LocalDate endDate = LocalDate.of(year, month+1, 16);
return date.isAfter(startDate) && date.isBefore(endDate);
}).collect(Collectors.toList());
}
Last updated
Was this helpful?