Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
self injecting spring test
import fr.pmu.commons.spring.SpringBuilder;
import org.junit.*;
import org.junit.Test;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
public class LearAsyncTest {
@Service
public static class AsyncService {
@Async
public void foo(int i) {
System.out.println(i + ". Zombie");
}
}
@org.springframework.context.annotation.Configuration
@EnableAsync
@EnableScheduling
public static class Configuration {
}
@Inject
private AsyncService asyncService;
@Before
public void setUp() throws Exception {
AnnotationConfigApplicationContext applicationContext = new SpringBuilder()//
.scanningClasses(Configuration.class, AsyncService.class) //
.build();
AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.autowireBean(this);
}
@Test
public void testAsync() throws Exception {
for (int i = 0; i < 100_009; i++) {
asyncService.foo(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment