SpringBoot(八):測試組件-junit

一、概述

  • junit集成流程
  • 單元測試類實例

二、junit集成流程

1)pom.xml引入java

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

2)基類編寫:web

package com.demo.quickStart;

import com.ss.quickStart.QuickStartApplication;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = QuickStartApplication.class)
@Transactional
@Rollback
public class BaseJunitTests {
}

 

三、單元測試類實例

package com.demo.quickStart.dao;

import com.demo.quickStart.BaseJunitTests;
import com.ss.quickStart.dao.UserMapper;
import com.ss.quickStart.domain.User;
import com.ss.quickStart.domain.dto.UserOrdersDTO;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class UserMapperTest extends BaseJunitTests {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void testQryUserOrders3(){
        UserOrdersDTO userOrdersDTO = userMapper.qryUserOrders3(1L);
        Assert.assertTrue(userOrdersDTO != null && userOrdersDTO.getOrderList().size() > 0);
    }

    @Test
    public void testAdd(){
        User user = new User();
        user.setName("小王");
        user.setSex(1);
        int count = userMapper.insert(user);
        Assert.assertTrue(count == 1);
    }
}

測試結果以下圖:spring

 

四、controller層。

package com.szhtxx.etcloud.ovat.controller;

import com.alibaba.fastjson.JSON;
import com.szhtxx.etcloud.ovat.api.dto.OvatBillRuleDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class OvatBillRuleControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void testAdd() throws Exception{
        OvatBillRuleDTO ruleDTO = new OvatBillRuleDTO();
        ruleDTO.setRuleSchemaCode("rule_001");
        ruleDTO.setRuleSchemaName("wsy拆合規則001");
        ruleDTO.setDetailSplitRule("0");
        ruleDTO.setDetailMergeRule("0");
        ruleDTO.setListRule("0");
        ruleDTO.setListLimitSpecial(9999);
        ruleDTO.setListLimitGeneral(9999);
        ruleDTO.setListLimitElectron(9999);
        ruleDTO.setNegativeDetailRule("1");
        ruleDTO.setDeviationRule("0");

        mvc.perform(post("/ovatBillRule/add")
                    .contentType(MediaType.APPLICATION_JSON_UTF8)
                    .content(JSON.toJSONString(ruleDTO)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true));
    }

    @Test
    public void testQryAllRule() throws Exception{
        mvc.perform(get("/ovatBillRule/qryAllRule")
                    .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true));
    }

    @Test
    public void testQryRuleInfo() throws Exception{
        mvc.perform(get("/ovatBillRule/qryRuleInfo")
                    .contentType(MediaType.APPLICATION_JSON_UTF8)
                    .param("ruleCode","rule_001"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true));
    }

    @Test
    public void testDelRule() throws Exception{
        mvc.perform(get("/ovatBillRule/delRule")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .param("ruleSchemaId","5aa5f4d63415ae521cecdf8d"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true));
    }
}
相關文章
相關標籤/搜索