spirng + junit + suit 测试

@RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境 


@ContextConfiguration(Locations="../applicationContext.xml") 用于指定配置文件所在的位置 


@Test标注在方法前,表示其是一个测试的方法 无需在其配置文件中额外设置属性. 



多个配置文件时{"/applic","/asas"} 可以导入多个配置文件 


package com.augmentum.ec.test.api;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import com.augmentum.ec.core.filter.LogAndVerfySigFilter;
import com.augmentum.ec.core.security.filter.CustomUsernamePasswordAuthenticationFilter;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:/META-INF/application-context.xml")
@WebAppConfiguration
public class UserTest {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    WebApplicationContext wac;

    MockMvc mockMvc;

    @BeforeClass
    public void login() {

    }

    @Test
    public void testGetUserDetail() throws Exception {
        CustomUsernamePasswordAuthenticationFilter loginFilter = (CustomUsernamePasswordAuthenticationFilter) applicationContext
                .getBean("loginFilter");
        LogAndVerfySigFilter logAndVerfySigFilter = (LogAndVerfySigFilter) applicationContext
                .getBean("LogAndVerfySigFilter");
        mockMvc = webAppContextSetup(wac).addFilter(loginFilter, "/*").addFilter(logAndVerfySigFilter, "/*").build();
        
        mockMvc.perform(get("/userDetail/1")).andExpect(jsonPath("$.status").value("success"));
    }

    @AfterClass
    public void signOut() {

    }
}

使用suit进行多个测试用例的执行:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@SuiteClasses({ TestA.class, TestB.class, TestC.class, UserTest.class })
@RunWith(Suite.class)
public class SuiteUser {
    // empty
}


版权声明:本文为lll350203041原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>