I am using the code below to test for an asynchronous implementation. (link from another SO post https://stackoverflow.com/a/166779/ )
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class MyAsyncClassTest { @Configuration @EnableAsync static class Config { @Bean public MyAsyncClass myAsyncClass() { Parameter1 param1 = new Parameter1(); Parameter2 param2 = new Parameter2(); return new MyAsyncClass(param1, param2); } } @Autowired private MyAsyncClass myAsyncClass; @Test public void test() throws Exception { myAsyncClass
Edit: "Here, MyAsyncClass is a class that has several methods annotated using the Spring @Async annotation."
As mentioned in the code comments, I get class-level fields set to null in my test method when using the @EnableAsync annotation, and so many cglib$Callback fields are displayed during debugging.
Edit:
Main class:
@Component public class MyAsyncClass { private final Param1 param1; private final Param2 param2; @Autowired public MyAsyncClass(final Param1 param1, final Param2 param2) { this.param1 = param1; this.param2 = param2; } @Async public Future<T> performAction() { String result = param1.getDefaultName() + param2.getDefaultName(); return new AsyncResult<String>(result); } @Async public Future<String> performAnotherAction() { String result = param1.getDefaultName() + param2.getDefaultName(); return new AsyncResult<String>(result); } }
Classes Param1 and Param2:
@Component public class Param1 { public String getDefaultName() { return "Param1"; } } @Component public class Param2 { public String getDefaultName() { return "Param2"; } }
source share