Hi
Is it possible to have a junit test method with the @Async annotation?
I am trying to generate concurrent access to a service.
In order to that I was thinking off calling an @Async method that calls this service.
I am using 3.0.3.RELEASE.
I have this declaration in my xml configuration: <task:annotation-driven />
The ouput of that test shows that there is no AsyncTask at all.Code:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) @Transactional @TransactionConfiguration public class CampaignServiceTest { @Autowired private xxService xxService; @Test public void testConcurrency() throws InterruptedException, ExecutionException { List<Boolean> results = new ArrayList<Boolean>(); Parameter parameter = new parameter(); for (int i = 0; i < 200; i++) { System.out.println("start "+i); results.add(consume(parameter, i).get().booleanValue()); } for (Boolean value : results) { assertTrue("the consumer finished with an error", value); } } @Async public Future<Boolean> consume(Parameter parameter, int number) { try { xxService.handleOperation(parameter); Thread.sleep((int) (Math.random() * 1000)); System.out.println("end " + number); return new AsyncResult<Boolean>(true); } catch (Exception e) { System.out.println("end " + number); return new AsyncResult<Boolean>(false); } } }
Aach call is done one after the others.
Any help or suggestion will be appreciated.
Regards


Reply With Quote
