I'm currently implementing a few repository methods as @Async
with spring-boot, and it's working like it should when i actually boot up the application, but when running integration tests, things starts to act weird.
I have setup a little project that showcases the problem.
Repository:
interface MyObjectRepository : CrudRepository<MyObject, Long> {
@Async
fun findBySomething(something: String): ListenableFuture<MyObject?>
}
Application:
@SpringBootApplication
@EnableAsync
class Application {
private val log = LoggerFactory.getLogger(Application::class.java)
}
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
And my test case:
@RunWith(SpringRunner::class)
@DataJpaTest
class MyObjectRepositoryTest {
@Autowired
lateinit var target: MyObjectRepository
@Before
fun `init`() {
target.deleteAll()
}
@Test
fun `should be able to find MyObject`() {
val m = MyObject("something")
val expected = target.save(m)
val futureResult = target.findBySomething("something")
val result = futureResult.get()
assert.that(result, equalTo(expected))
}
}
My results:
expected == the object (good)
result == null (bad)
If i enable @Async
on class-level on the repository, the result gets flipped around.
And if i further make the test sleep after calling target.save(m)
both expected
and result
are null
Anyone have any suggestions as to what could be wrong? Am i missing something (like an annotation for testing purposes?).
Again, everything is working fine when i boot up the application, and make rest-calls to my controller.