隐藏

Idea中Springboot项目使用JUnit4进行单元测试的方式

发布:2022/3/24 11:50:03作者:管理员 来源:本站 浏览次数:1376

Idea中Springboot项目使用JUnit4进行单元测试的方式


依赖注入不可少:


<dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <scope>test</scope>

</dependency>


1、在业务代码中完成编码操作,如完成service编码实现:


package com.example.demo.service;


import com.example.demo.entity.User;

import com.example.demo.mapper.UserMapper;

import com.example.demo.result.ResultDTO;

import com.example.demo.result.ResultError;

import com.example.demo.result.UserError;

import com.example.demo.util.JwtUtil;

import com.example.demo.util.RedisUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.util.StringUtils;


import java.util.HashMap;

import java.util.List;

import java.util.Map;


@Service

public class UserService {

   @Autowired(required=false)

   public UserMapper userMapper;

   @Autowired

   RedisUtils redisUtils;


   public List<User> findAllUser(){

       return userMapper.findAllUser();

   }


   public List<User> findUserByUserId(int userid){

       return userMapper.findUserByUserId(userid);

   }


   public int addUser(User user){

       return userMapper.addUser(user);

   }


   public int deleteUser(User user){

       return userMapper.deleteUser(user);

   }


   public User userLogin(User user){

       return userMapper.userLogin(user);

   }


   /**

    * 用户登录

    * @param name

    * @param password

    * @return

    */

   public ResultDTO login(String name, String password) {

       Map<String, Object> tokenMap = new HashMap<>();

       //密码需要客户端加密后传递

       String token = null;

       try {

           User user = userMapper.findByUsername(name);

           if(user == null){

               return ResultDTO.failure(new ResultError(UserError.EMP_IS_NULL_EXIT));

           }else{

               if(!user.getPassword().equals(password)){

                   return ResultDTO.failure(new ResultError(UserError.PASSWORD_OR_NAME_IS_ERROR));

               }else {

                   // 将 user id 、userName保存到 token 里面

                   token = JwtUtil.sign(user.getUsername(),String.valueOf(user.getUserid()),user.getPassword());

                   redisUtils.set("userToken-" + user.getUserid(), token, 2L * 60);

               }

           }


           if(StringUtils.isEmpty(token)){

               return ResultDTO.failure(new ResultError(UserError.PASSWORD_OR_NAME_IS_ERROR));

           }

           tokenMap.put("token", token);

           tokenMap.put("user",user);

       } catch (Exception e) {

           e.printStackTrace();

       }

       return ResultDTO.success(tokenMap);

   }


   public User findByUserId(int userid){

       return userMapper.findByUserId(userid);

   }

   public List<User> getUserArticle(){

       return userMapper.getUserArticle();

   }


}





2、把鼠标放到public class UserService类名上,右键选择Go To>Test,或者直接Ctrl+Shift+T,效果图如下:

Idea中Springboot项目使用JUnit4进行单元测试的方式接着跳转到:

Idea中Springboot项目使用JUnit4进行单元测试的方式

3、在生成的Test类中增加注解:@RunWith和@SpringBootTest,然后在Test类中注入需要测试的service,调用测试的接口即可!

package com.example.demo.service;


import com.example.demo.entity.User;

import org.junit.jupiter.api.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import java.util.List;


@RunWith(SpringRunner.class)

@SpringBootTest

class UserServiceTest {


   @Resource

   public UserService userService;

   @Test

   void findAllUser() {

       System.out.println("Before");

       List<User> user= userService.findAllUser();

       System.out.println(user);

   }


   @Test

   void findUserByUserId() {

   }


   @Test

   void addUser() {

   }


   @Test

   void deleteUser() {

   }


   @Test

   void userLogin() {

   }


   @Test

   void login() {

   }


   @Test

   void findByUserId() {

   }


   @Test

   void getUserArticle() {

   }

}



4、运行测试代码:建议右键>Debug 方法名,可打断点

Idea中Springboot项目使用JUnit4进行单元测试的方式5、如果springboot启动没有错误,则可以输出测试代码的内容