zoukankan      html  css  js  c++  java
  • 转:how-to-run-junit-springjunit4classrunner-with-parametrized(spring-test如何与junit的Parameterized结合)

    original: https://stackoverflow.com/questions/28560734/how-to-run-junit-springjunit4classrunner-with-parametrized/28561473

    @RunWith(Parameterized.class)
    @WebAppConfiguration
    @ContextConfiguration({"classpath:springmvc.xml"})
    public class SpringTestWithParametersBase {
        private MockMvc mockMvc;
    
        @Autowired
        private WebApplicationContext wac;
    
        @Autowired
        @Qualifier("xx")
        private Filter filter;
    
        private TestContextManager testContextManager;
    
        @Before
        public void setup() throws Exception {
            //https://stackoverflow.com/questions/28560734/how-to-run-junit-springjunit4classrunner-with-parametrized/28561473
            //this is where the magic happens, we actually do "by hand" what the spring runner would do for us,
            // read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though
            this.testContextManager = new TestContextManager(getClass());
            this.testContextManager.prepareTestInstance(this);
    
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
                    .addFilter(filter, "/api/v2/*")
                    .build();
    
            doSetup();
        }
    
        protected void doSetup() {
        }
    
        protected MockMvc getMockMvc() {
            return mockMvc;
        }
        
        @Parameterized.Parameters(name = "{index}:{0}")
        public static Collection addedNumbers() {
            return Arrays.asList(new Object[][]{
                    {"/xxxx", "", status().isNotFound()}
            });
        }
    }
    

    There is a github project https://github.com/mmichaelis/spring-aware-rule, which builds on previous blog, but adds support in a generalized way

    @SuppressWarnings("InstanceMethodNamingConvention")
    @ContextConfiguration(classes = {ServiceTest.class})
    public class SpringAwareTest {
    
        @ClassRule
        public static final SpringAware SPRING_AWARE = SpringAware.forClass(SpringAwareTest.class);
    
        @Rule
        public TestRule springAwareMethod = SPRING_AWARE.forInstance(this);
    
        @Rule
        public TestName testName = new TestName();
    
        ...
    }
    

    如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
  • 相关阅读:
    MySQL 触发器
    MySQL视图
    MySQL中的存储过程和函数
    Mysql数据库连接查询
    数据库(MySQL)表基本操作
    Spring MVC---数据绑定和表单标签
    Spring MVC---基于注解的控制器
    Spring基于AOP的事务管理
    Ubuntu下通过wine安装HeidiSQL
    chmod命令详解
  • 原文地址:https://www.cnblogs.com/liqipeng/p/14730781.html
Copyright © 2011-2022 走看看