Results 1 to 2 of 2

Thread: In a @Test, I'd like to add extra locations to the ones from @ContextConfiguration

  1. #1
    Join Date
    May 2006
    Location
    Madrid
    Posts
    382

    Default In a @Test, I'd like to add extra locations to the ones from @ContextConfiguration

    Hello,

    I have an application whose context load the services, repositories and so on.

    Additionally, I have an application context for loading one task executor. It queries a database and uses the obtained records to perform its job (actually, it's a spring integration context that starts an entire integration flow).

    I'd like to first load the application context, then create some records in the database, and then load the application context with the location of files that contain the beans for executing the tasks.

    I tried my best, but without success.

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath*:META-INF/spring/applicationContext*.xml")
    @ActiveProfiles("dev")
    public class MyTest {
    
    	@Autowired
    	private ApplicationContext ctx;//It's a GenericApplicationContext
    
    
    	@Test
        public void tests() {
    
        	//GenericXmlApplicationContext allows me to load locations and refresh
        	GenericXmlApplicationContext gctx = new GenericXmlApplicationContext();//I don't know how to create it from the previous ctx
    
        	//Manage the active profiles in the Environment
    
        	gctx.load("classpath*:META-INF/spring/integration/applicationContext*.xml");
        	gctx.refresh();
        }
    
    }
    I only succeed when I managed to load of the application context without the Spring runner and the Spring annotations

    Is it possible to do what I want to?

  2. #2
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

    Replace your code
    Code:
        @Test     public void tests() {     	//GenericXmlApplicationContext allows me to load locations and refresh     	GenericXmlApplicationContext gctx = new GenericXmlApplicationContext();//I don't know how to create it from the previous ctx     	//Manage the active profiles in the Environment     	gctx.load("classpath*:META-INF/spring/integration/applicationContext*.xml");     	gctx.refresh();     }
    with this
    Code:
    @Test     public void tests() {      	ClassPathXmlApplicationContext gctx = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/integration/applicationContext*.xml", ctx);     	gctx.refresh();     }
    This should create a new context with the parent context predefined from ctx instance.

Posting Permissions

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