We Recommend

Java How to Program Java How to Program
Takes a new tools-based approach to Web application development that uses Netbeans 5.5 and Java Studio Creator 2 to create and consume Web Services. Features new AJAX-enabled, Web applications built with JavaServer Faces (JSF), Java Studio Creator 2 and the Java Blueprints AJAX Components. Includes new topics throughout, such as JDBC 4, SwingWorker for multithreaded GUIs, GroupLayout, Java Desktop Integration Components (JDIC), and much more.


Posted By

narkisr on 07/27/08


Tagged

testing java spring


Versions (?)


Custom loader for Junit Spring Runner


Published in: Java 


This Loader enables the loading of a custom context when using Spring JUnit runner, this enables programmatic registration of beans of commonly used beans in tests. This loader receive TestedService annotation in the processLocations method and uses its values to register beans in the loadContext method. This snippets completes the following entry: http://javadevelopmentforthemasses.blogspot.com/2008/07/mocking-spring-tests.html

  1. public class Loader implements ContextLoader {
  2.  
  3. private Class testedService;
  4. private Class[] beans;
  5.  
  6.  
  7. @Override
  8. public String[] processLocations(Class<?> testClass, String... locs) {
  9. TestedService annotation = testClass.getAnnotation(TestedService.class);
  10. testedService = annotation.service();
  11. beans = annotation.beans();
  12. return locs;
  13. }
  14.  
  15. @Override
  16. public ApplicationContext loadContext(String... locs) throws Exception {
  17. GenericApplicationContext ctx = new GenericApplicationContext();
  18. final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
  19. xmlReader.loadBeanDefinitions(new ClassPathResource("spring/context.xml"));
  20. final Map<String, Object> declaredValues = new HashMap<String, Object>(1);
  21. final String serviceName = StringUtils.uncapitalize(testedService.getSimpleName().replace("Impl", ""));
  22. register(serviceName, testedService, declaredValues,ctx);
  23. declaredValues.put("mocksHolder", testedService);
  24. register("autoBeanDeclarer", AutoBeanDeclarer.class, declaredValues,ctx);
  25. for (final Class bean : beans) {// registering all the other beans
  26. final String beanName=StringUtils.uncapitalize(bean.getSimpleName().replace("Impl", ""));
  27. register(beanName, bean, declaredValues,ctx);
  28. }
  29.  
  30. AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
  31. ctx.refresh();
  32. return ctx;
  33. }
  34.  
  35. private void register(final String name, final Class clazz, Map<String, Object> values,GenericApplicationContext ctx) {
  36. final BeanDefinitionBuilder serviceBuilder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
  37. for (Map.Entry<String, Object> entry : values.entrySet()) {
  38. serviceBuilder.addPropertyValue(entry.getKey(), entry.getValue());
  39. }
  40. values.clear();
  41. ctx.registerBeanDefinition(name, serviceBuilder.getRawBeanDefinition());
  42.  
  43. }
  44. }
  45.  
  46. // Usage, note that the location String has to be unique in order to make spring load the context separately for each test.
  47.  
  48. @RunWith(SpringJUnit4ClassRunner.class)
  49. @ContextConfiguration(loader = Loader.class,locations="SomeUniqueString")
  50. @TestedService(service = SomeServiceImpl.class)
  51. public class SomeServiceCases {
  52.  
  53. // tests and autowiring..
  54.  
  55. }

Report this snippet 

You need to login to post a comment.