3

I want to write a test against a search with date. I am thinking of a test code something like

assertThat(repository.findByBookingDateAfter(LocalDate.of(2016, 1, 1))).extracting("bookingDate").are(...));

where the class structure is something like the followings:

public class Booking{
  ...
  LocalDate bookingDate;
  ...
}

Because the query is on any bookings after a given date, my test shall check the field indeed later than the date. After some online search, I don't see any useful information. I am wondering whether I am on the right track or not.

Any suggestions?

Update:

Later, I change the dependency setup is the followings in the build.gradle file:

testCompile('org.springframework.boot:spring-boot-starter-test'){
    exclude group: 'org.assertj'
}
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'

to have the latest version of assertj.

Update 2:

The following is a screenshot of any methods starting with "is". The "isAfter" isn't on the list.

enter image description here

vic
  • 2,548
  • 9
  • 44
  • 74
  • 1
    Could you please extend your example with more **code** and a description of what specifically you want to *test*, what you **expect** and what you already **tried**? Depending on what **date api** you use (I see a `LocalDate` in your example) you might want to have a look at the documentation for [Joda-Time Assertion](https://joel-costigliola.github.io/assertj/assertj-joda-time.html) or the [code examples for Java 8 Date and Time API](https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/Java8DateTimeAssertionsExamples.java). – Thomas Traude Feb 17 '17 at 20:18
  • Thanks. I add more details in my question. – vic Feb 17 '17 at 21:41

1 Answers1

5

I would use allMatch or allSatisfy assertions to check that every extracted date is after the one used in the query.

// set the extracted type to chain LocalDate assertion 
assertThat(result).extracting("bookingDate", LocalDate.class)
                  .allMatch(localDate -> localDate.isAfter(queryDate)));

or

// use a lambda to extract the LocalDate to chain LocalDate assertion
assertThat(result).extracting(Booking::getBookingDate)
                  .allSatisfy(localDate -> assertThat(localDate).isAfter(queryDate));

you get a better error message with the second assertion, it will show the faulty date.

Joel Costigliola
  • 6,308
  • 27
  • 35
  • Are allMatch and allSatisfy a part of assertj? I can't find them in any dependencies in my project. Also the localDate -> assertThat(localDate) code can't pass the compiler either. – vic Feb 18 '17 at 23:50
  • they are in 3.6.2, see http://joel-costigliola.github.io/assertj/core-8/api/org/assertj/core/api/AbstractIterableAssert.html#method.summary – Joel Costigliola Feb 19 '17 at 04:20
  • After replacing to the version, I can see allMatch and allSatisfy, but not isAfter. I scan over all available methods, but can't find a suitable one. – vic Feb 19 '17 at 18:51
  • I can see it is in your code sample https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/DateAssertionsExamples.java , but I don't know why I can't see it. – vic Feb 19 '17 at 18:58
  • you are looking at `Date` assertion examples instead of `LocalDate` that are shown in https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/Java8DateTimeAssertionsExamples.java. Can you double check the AssertJ version you are using ? – Joel Costigliola Feb 19 '17 at 22:46
  • I add the updated dependency setting of my project in the question. I find that I used Java6Assertions.assertThat instead of Assertions.assertThat. After making the correction, the problem still exists: can't find isAfter. I also try to something in the same fashion as allSatisfy(title -> assertThat(title).contains("the string")). The "contains" can't be found either. – vic Feb 20 '17 at 05:32
  • The following example proves that `allSatisfy` works nicely with `LocalDate` `isAfter` assertion in assertj core 3.6.1: https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/Java8DateTimeAssertionsExamples.java#L143 Check again the version that you are using. Show us how you get the assertj-core dependency. – Joel Costigliola Feb 21 '17 at 00:50
  • you need to specify the the type of what you are extracting otherwise AssertJ, ex : https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/IterableAssertionsExamples.java#L215 Using Java8 I would recommend using a lambda to express the extraction operation as it would inform AssertJ of the extracted type,ex : https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/IterableAssertionsExamples.java#L455. hopefully this time it will work ! – Joel Costigliola Feb 21 '17 at 22:26
  • Yes, the change works for both cases. Thanks a lot for your help. – vic Feb 22 '17 at 00:53