0

I am using below code to test my asynchronous implementation. (reference from another SO post https://stackoverflow.com/a/20807233/1989988)

@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    //it has param1 and param2 set as null
                        //if @EnableAsync is removed, then param1 and param2 are set but async call is not working
    }

Edit: "Here MyAsyncClass is a class having a few methods annotated with Spring's @Async annotation."
As mentioned in the code comments, I am getting the class level fields set as null in my test method when using @EnableAsync annotation and there are so many cglib$Callback fields are shown at debug time.

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";
    }
}  
Community
  • 1
  • 1
iAmLearning
  • 1,153
  • 3
  • 15
  • 28
  • Have you tried actually using the object, i.e. calling one of its methods, and see what happens? What you're seeing is a dynamic proxy. That's what allows the asynchnous magic to happen. – JB Nizet Jan 13 '16 at 06:55
  • yes, MyAsyncClass has multiple methods which are called asynchronously as expected but as the param1 and param2 are set to null, I am not able to test my method'd logic and getting null pointer. – iAmLearning Jan 13 '16 at 06:58
  • Post the code of MyAsyncClass, post the code of your test, and post the stack trace of the exception. – JB Nizet Jan 13 '16 at 07:00
  • The issue is that I am getting the param1 and param2 set as null when accessed from test method when EnableAsync annotation is applied. I want the param1 and param2 variable to be what I set in Bean annotated method. – iAmLearning Jan 13 '16 at 08:25
  • We can't help find the problem in your code if you don't post it. There is 99.999% chance that the problem is in your code, and 0.001% chance that it's a Spring problem. – JB Nizet Jan 13 '16 at 08:31
  • Added the classes above. Please take a look. – iAmLearning Jan 13 '16 at 09:35
  • After fixing all the compilation errors in your code, and doing `assertEquals("Param1Param2", myAsyncClass.performAnotherAction().get());` in the test method, everything works fine: the test passes. Now, if you posted REAL code that actually compiles, contains a real test, and reproduces the problem, we might provide more help. – JB Nizet Jan 13 '16 at 17:54

0 Answers0