使用Intellij IDEA右击要测试的方法,选择如下图。
Service
package cn.notemi.service;
import cn.notemi.Util.ResultUtil;
import cn.notemi.po.Result;
import cn.notemi.repository.UserRepository;
import cn.notemi.enums.ResultEnum;
import cn.notemi.exception.UserException;
import cn.notemi.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
/**
* Title:UserService
* Description:
*
* @author Flicker
* @create 2017-08-03 下午 6:24
**/
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
/**
* 通过id查询一个用户的信息
* @param id
* @return
*/
public User findOne(Integer id){
return userRepository.findOne(id);
}
}
测试
package cn.notemi;
import cn.notemi.service.UserService;
import cn.notemi.po.User;
import org.junit.Assert;
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;
/**
* Title:UserServiceTest
* Description:
*
* @author Flicker
* @create 2017-08-05 下午 4:23
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void findOneTest(){
User user = userService.findOne(1);
//这里用断言来比对两个值
Assert.assertEquals(new Integer(18),user.getAge());
}
}
运行测试,如果失败则会报错,需要post提交的controller方法测试需要模拟http请求来测试。
Comments (暂无评论)