Results 1 to 3 of 3

Thread: [JUNIT] ContextLoader

  1. #1
    Join Date
    Feb 2010
    Posts
    13

    Default [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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2010
    Posts
    13

    Default

    Quote 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •