Announcement Announcement Module
Collapse
No announcement yet.
[JUNIT] ContextLoader Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [JUNIT] ContextLoader

    Hello guys !!

    I have a slight issue, easy to fix I am sure but I do not see it

    I am using JUNIT to test some classes :

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:/applicationContext.xml", "classpath:/BapplicationContext.xml", "classpath:/applicationBusinessContext.xml" })
    @TransactionConfiguration(transactionManager = "hibernateTransactionManager",defaultRollback = true)
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public class ABServicesTest extends
    				AbstractTransactionalJUnit4SpringContextTests {
    
    	@Autowired
    	private ABServices	ABServices;
    
    	@Test
    	public void test123() {
                testFake();
    	}
    In testFake() I have a calling like that :


    Code:
    public final class StaticUtils {
    	
    private  static final IAService  A_SERVICE  =  (IAService)ContextLoader.getCurrentWebApplicationContext().getBean("iAService");
    But when it tries to retreive the bean , I have ContextLoader.getCurrentWebApplicationContext() = null.

    I need to inject the ContextLoader from the test context or instantiate a new ContextLoader ?

    What do I have to do to initialize ContextLoader.getCurrentWebApplicationContext() ?


    It seems trivial but I do not see it


    Thanks

  • #2
    You could use some reflection tricks to add the context to the map however that probably would still fail as you aren't loading a WebApplicationContext but an ApplicationContext (WebApplicationContext == AplicationContext but ApplicationContext != WebApplicationContext).

    I also wouldn't use that utility class to obtain references and if you really must I would make the utility class BeanFactoryAware(or ApplicationContextAware) and add it to the context to get access the the ApplicationContext. And it works regardless of the environment.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      You could use some reflection tricks to add the context to the map however that probably would still fail as you aren't loading a WebApplicationContext but an ApplicationContext (WebApplicationContext == AplicationContext but ApplicationContext != WebApplicationContext).

      I also wouldn't use that utility class to obtain references and if you really must I would make the utility class BeanFactoryAware(or ApplicationContextAware) and add it to the context to get access the the ApplicationContext. And it works regardless of the environment.
      Thank you for the guidance

      I ended up creating a bean extending ApplicationContextAware, and then initialized the new bean in the applicationContext.xml.
      Then just calling the static method, and voila

      Comment

      Working...
      X