service层这么调用多个mapper层的数据并测试
时间: 2024-04-06 11:07:36 浏览: 30
假设我们有两个mapper层的接口:UserMapper和OrderMapper,它们分别用于查询用户数据和订单数据。我们需要在service层中调用这两个接口获取数据。
1. 创建UserService和OrderService类,注入UserMapper和OrderMapper接口:
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getUsers() {
return userMapper.getUsers();
}
}
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
public List<Order> getOrders() {
return orderMapper.getOrders();
}
}
```
2. 创建一个Controller类,注入UserService和OrderService接口,并调用它们的方法:
```java
@RestController
public class MyController {
@Autowired
private UserService userService;
@Autowired
private OrderService orderService;
@GetMapping("/data")
public List<Object> getData() {
List<Object> data = new ArrayList<>();
data.addAll(userService.getUsers());
data.addAll(orderService.getOrders());
return data;
}
}
```
3. 编写单元测试代码,测试UserService和OrderService的方法是否能够正确获取数据:
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyControllerTests {
@Autowired
private UserService userService;
@Autowired
private OrderService orderService;
@Test
public void testGetUsers() {
List<User> users = userService.getUsers();
Assert.assertNotNull(users);
Assert.assertTrue(users.size() > 0);
}
@Test
public void testGetOrders() {
List<Order> orders = orderService.getOrders();
Assert.assertNotNull(orders);
Assert.assertTrue(orders.size() > 0);
}
}
```
4. 测试Controller的getData方法是否能够正确获取数据:
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyControllerTests {
@Autowired
private MockMvc mockMvc;
@Test
public void testGetData() throws Exception {
MvcResult result = mockMvc.perform(get("/data"))
.andExpect(status().isOk())
.andReturn();
String content = result.getResponse().getContentAsString();
Assert.assertNotNull(content);
Assert.assertTrue(content.contains("user"));
Assert.assertTrue(content.contains("order"));
}
}
```
以上是一个示例,实际上,我们可以根据自己的业务需求,创建多个service层和mapper层的接口,然后在Controller层中调用它们,测试代码也可以根据实际情况进行编写。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)