Announcement Announcement Module
Collapse
No announcement yet.
In a @Test, I'd like to add extra locations to the ones from @ContextConfiguration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X