对springboot的service层方法进行单元测试

对springboot得service层方法进行测试,由于我的启动类与java代码不在一个模块里,启动类在web模块,service在普通模块内,测试得时候遇到了一些问题。如下图所示。

在这里插入图片描述

下图,测试类的路径与启动类的路径要一致,切记,切记
在这里插入图片描述

启动类代码

package com.springboot.controller;

import org.mybatis.spring.annotation.MapperScan;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan(basePackages = "com.springboot.*.mapper")
@ComponentScan(basePackages={"com.springboot"})
public class Application {
    public static void main(String args[]){
        System.out.println("垃圾springboot");
        SpringApplication.run(Application.class, args);
    }
}

service层


@Service
public class IMybatisUserServiceImpl implements IMybatisUserService {

	    @Autowired
	    public MyBatisDao myBatisDao;
	
	    @Override
	    public List<MybatisUser> selectAll() {
	        List<MybatisUser> mybatisUsers = myBatisDao.pageByFilterRows();
	        return mybatisUsers;
	    }
    }

dao层


import com.springboot.mybatistest.entity.MybatisUser;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MyBatisDao {
    public List<MybatisUser> pageByFilterRows();
}

测试类

package com.springboot.controller;


import com.springboot.mybatistest.entity.MybatisUser;
import com.springboot.mybatistest.service.IMybatisUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Test1 {


    @Autowired
    public IMybatisUserService mybatisUserService;

    @Test
    public void test(){
       mybatisUserService.select();
        List<MybatisUser> mybatisUsers = mybatisUserService.selectAll();
        System.out.println(mybatisUsers.size());
    }
}

注:所有的方法都写完后,启动测试类,报错,说找不到mapper文件中的pageByFilterRows方法。最后在sb-core模块的pom文件中添加以下配置。因为如果不添加一下配置,程序只会默认去找resources下的配置文件,所以找不到我们自己写的mapper文件。添加以下配置后,解决问题

 <build>
        <resources>
            <!--将java代码目录中的xml输出,默认不输出除resources目录外的xml文件-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
假设我们有两个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中调用它们,测试代码也可以根据实际情况进行编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值