How to run JUnit SpringJUnit4ClassRunner with Parametrized?

后端 未结 4 1236
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 16:16

The following code is invalid due to duplicate @RunWith annotation:

@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(Parameterized.class)
@Sprin         


        
4条回答
  •  借酒劲吻你
    2020-12-12 16:49

    Handle application context by yourself

    What worked for me was having a @RunWith(Parameterized.class) test class that managed the application context "by hand".

    To do that I created an application context with the same string collection that would be in the @ContextConfiguration. So instead of having

    @ContextConfiguration(locations = { "classpath:spring-config-file1.xml",
        "classpath:spring-config-file2.xml" })
    

    I had

    ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {
                "classpath:spring-config-file1.xml", "classpath:spring-config-file2.xml"  });
    

    And for each @Autowired I needed I fetched it by hand from the created context:

    SomeClass someBean = ctx.getBean("someClassAutowiredBean", SomeClass.class);
    

    Do not forget to close the context at the end:

    ((ClassPathXmlApplicationContext) ctx).close();
    

提交回复
热议问题