springboot junit test

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>


<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

 

 

1.注解列表

  • @RunWith:标识为JUnit的运行环境;
  • @SpringBootTest:获取启动类、加载配置,确定装载Spring Boot;
  • @Test:声明需要测试的方法;
  • @BeforeClass:针对所有测试,只执行一次,且必须为static void;
  • @AfterClass:针对所有测试,只执行一次,且必须为static void;
  • @Before:每个测试方法前都会执行的方法;
  • @After:每个测试方法前都会执行的方法;
  • @Ignore:忽略方法;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //不必显示指明启动类等
@ActiveProfiles("xx")
@Transactional
@Slf4j  // 日志

 

 

 创建测试类方法:https://www.w3cschool.cn/intellij_idea_doc/intellij_idea_doc-mrn42f9j.html

右键 >>>GO TO  >>> TEST

2.Assert断言

  • Assert.assertEquals 对比两个值相等
  • Assert.assertNotEquals 对比两个值不相等
  • Assert.assertSame 对比两个对象的引用相等
  • Assert.assertArrayEquals 对比两个数组相等
  • Assert.assertTrue 验证返回是否为真
  • Assert.assertFlase 验证返回是否为假
  • Assert.assertNull 验证null
  • Assert.assertNotNull 验证非null

 

3.Web模拟测试

 spring boot

// junit4 如果需要测试容器,需要加上@RunWith,junit5之后可以不要
@RunWith(SpringRunner.class)
//SpringBootTest 可指定启动类或者测试环境等,这里直接默认。在实际开发过程中,可指定其测试启动时为随机端口,避免不必要的端口冲突。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//测试单一接口时 ,也可利用注解@WebMvcTest 进行单一测试
//@WebMvcTest(DemoController.class)
public class TestServiceImplTest {
//使用 WebMvcTest 时使用
    //@autowired mockMvc 是可自动注入的。
    //当直接使用SpringBootTest 会提示 注入失败  这里直接示例利用 MockMvcBuilders工具创建
    //@Autowired
    MockMvc mockMvc;


    @Autowired
    WebApplicationContext wc;

    @BeforeEach
    public void beforeSetUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wc).build();
    }

    @DisplayName("接口描述,展示测试方法")
    @Test
    public void test() {
        String id = "xxx";
        MvcResult result = this.mockMvc
                .perform(MockMvcRequestBuilders.get("/api/test").param("id", id))
                .andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        //断言 是否和预期相等
        System.out.println(result.getResponse().getContentAsString());
    }

} 

 spring mvc

// SpringJUnit 引入Spring-Test框架
@RunWith(SpringJUnit4ClassRunner.class)
@Configuration
@ContextConfiguration({ "classpath:spring/spring-beans.xml" })
// Web项目,Junit需要模拟ServletContext。指定项目web资源的路径 默认值 src/main/webapp
// @WebAppConfiguration
public class ExampleTest {

    private ApplicationContext ac; 
  // 方式2
    @Before 
    public void setUp() throws Exception{ 
        ac = new ClassPathXmlApplicationContext("config/*.xml"); 

  }

    @Autowired
    private Example example;

    @Test
    public void example {

    }
}
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml"); 
new ClassPathXmlApplicationContext("applicationContext.xml");// 从classpath中加载 
new FileSystemXmlApplicationContext("classpath:地址");// 没有classpath表示当前目录,如和web-inf

 

 

4.数据库测试

给测试类上添加“@Transactional”,这样既可以测试数据操作方法,又不会污染数据库了。

 

5.cron表达式

* 第一位,表示秒,取值0-59
* 第二位,表示分,取值0-59
* 第三位,表示小时,取值0-23
* 第四位,日期天/日,取值1-31
* 第五位,日期月份,取值1-12
* 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思
          另外:1表示星期天,2表示星期一。
* 第7为,年份,可以留空,取值1970-2099


(*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...
(?)问号:问号只能出现在日期和星期这两个位置。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
(/)斜杠:如:x/y,x是开始值,y是步长,比如在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60    另:*/y,等同于0/y

 

posted @ 2020-01-02 10:51  毁乐乖狂,自有诪张  阅读(964)  评论(0编辑  收藏  举报