I am writing a test (groovy language\spock framework if relevant)
Initially it makes our application send an email to a specific exchange (office 365 if relevant) mailbox. After that I need to log into that mailbox and verify the email that the application has sent.
I use EWS Java API to communicate with the EWS (com.microsoft.ews-java-api 2.0)
After connecting to the exchange service, i do the following:
SearchFilter searchFilter = new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "search string")
Awaitility.await()
.atMost(180, TimeUnit.SECONDS)
.until({
exchangeService.findItems(WellKnownFolderName.Inbox, searchFilter, new ItemView(1)).totalCount > 0
})
Awaitility.await() in here is essentially a dynamic wait - it retries the code until conditions are met. It can be replaced with any kind of loop with a thread sleep and break condition.
On the first attempt the findItems call never finds the email - it usually takes around a minute for the email to be sent\arrive.
The issue is that on any subsequent attempt the findItems does not find the item successfully. However if I hold a breakpoint on it until I see the email in the mailbox - it works successfully.
So for me it looks like once the search fails (does not find anything) executing the method again does not do anything. What am I doing wrong?