Asynchronous unit testing in Java/Junit - a very simple yet unsuccessful example
When testing CompletableFuture
you need to wait until all its steps are completed.
Instead of a Thread.sleep
, add a final fut.join()
JUnit-testing a Spring @Async void service method
For @Async
semantics to be adhered, some active @Configuration
class will have the @EnableAsync
annotation, e.g.
@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {
//
}
To resolve my issue, I introduced a new Spring profile non-async
.
If the non-async
profile is not active, the AsyncConfiguration
is used:
@Configuration
@EnableAsync
@EnableScheduling
@Profile("!non-async")
public class AsyncConfiguration implements AsyncConfigurer {
// this configuration will be active as long as profile "non-async" is not (!) active
}
If the non-async profile is active, the NonAsyncConfiguration
is used:
@Configuration
// notice the missing @EnableAsync annotation
@EnableScheduling
@Profile("non-async")
public class NonAsyncConfiguration {
// this configuration will be active as long as profile "non-async" is active
}
Now in the problematic JUnit test class, I explicitly activate the "non-async" profile in order to mutually exclude the async behavior:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@Transactional
@ActiveProfiles(profiles = "non-async")
public class SomeServiceIntTest {
@Inject
private SomeService someService;
@Test
public void testAsyncMethod() {
Foo testData = prepareTestData();
someService.asyncMethod(testData);
verifyResults();
}
// verifyResult() with assertions, etc.
}
JUnit testing an asynchronous method with Mockito
The solution was to add in a Mockito verification step with a timeout and a check to ensure that the mocked component's method had been called the appropriate number of times:
Mockito.verify(myComponentAsAMock, Mockito.timeout(1000).times(1)).doOtherStuff(ArgumentMatchers.any(MyClass.class));
Related Topics
Springboot 401 Unauthorized Even With Out Security
Check If a Given Time Lies Between Two Times Regardless of Date
How to Take Input as String With Spaces in Java Using Scanner
Java Spring Boot Test: How to Exclude Java Configuration Class from Test Context
Parameter 0 of Constructor in Required a Bean of Type 'Java.Lang.String' That Could Not Be Found
How to Check If a Record Exists Using Jpa
Resttemplate: How to Send Url and Query Parameters Together
How to Remove the Decimal Part If It Is Only 0
Can Not Deserialize Instance of Java.Util.Arraylist Out of Start_Object Token
Java Fast Food Menu (Using Methods)
How to Set a Default Entity Property Value With Hibernate
Java How to Retrieve More Than 1 Million Rows in a Resultset
How to Pass List in Postman in Get Request and Get in Getmapping