软件测试

初识SpringBoot单元测试

2019-08-27  本文已影响0人  有财君

1. 测试Service

首先我们写出service层的代码:

package com.example.demo.service;

import com.example.demo.dao.OrderDao;
import com.example.demo.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;

    public String getOrderName(Long id) {
        Optional<Order> optionalOrder = this.orderDao.findById(id);
        return optionalOrder.map(Order::getName).orElse(null);
    }
}

为了做这个类的单元测试,我们编写这样一段test代码:

package com.example.demo.service;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderServiceTests {
    @Autowired
    private OrderService orderService;

    @Value("${test.id}")
    private long id;

    @Value("${test.name}")
    private String name;

    @Test
    public void getOrderNameTest() {
        Assert.assertEquals(this.name, this.orderService.getOrderName(this.id));
    }
}

我这里的配置都在test下的resources目录下。下面是工程结构截图:

工程结构

测试Service类本身没有什么,用断言式即可简单完成。

2. 测试Controller

为了测试,每次都需要启动服务,然后用Postman发送请求,这种方式也不错,但是都是手动的,还是用代码定制比较方便。

下面是controller代码:

package com.example.demo.controller;

import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/getName")
    public String getOrderName(@RequestParam("id") Long id) {
        return this.orderService.getOrderName(id);
    }
}

对应的test代码:

package com.example.demo.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class OrderControllerTests {
    @Autowired
    private MockMvc mockMvc;

    @Value("${test.id}")
    private String id;

    @Test
    public void getOrderNameTest() throws Exception {
        MvcResult mvcResult = mockMvc
                .perform(MockMvcRequestBuilders.get("/getName")
                        .param("id", id))
                .andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
    }
}

MockMvc可以很简单的构建请求发送给服务端。

因为这是初识篇,所以简单的写两段代码就可以了,单元测试很多时候比代码还要重要,是程序员必须掌握的技能,因此后续的学习中,会深入学习这些知识。

上一篇下一篇

猜你喜欢

热点阅读