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可以很簡單的構建請求發送給服務端。
因爲這是初識篇,所以簡單的寫兩段代碼就可以了,單元測試很多時候比代碼還要重要,是程序員必須掌握的技能,因此後續的學習中,會深入學習這些知識。