一 代码位置
https://gitee.com/cakin24/code/tree/master/07/UnitTestDemo
二 关键代码
package com.example.demo.service;
import com.example.demo.entity.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;
import static org.hamcrest.CoreMatchers.is;
//表明要在测试环境运行,底层使用的junit测试工具
@RunWith(SpringRunner.class)
// SpringJUnit支持,由此引入Spring-Test框架支持!
//启动整个spring的工程
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void getUserInfo() {
User user = userService.getUserInfo();
//比较实际的值和用户预期的值是否一样
Assert.assertEquals(18,user.getAge());
Assert.assertThat(user.getName(),is("zhonghualong"));
}
}
三 测试
1 测试不通过,控制台输出如下
java.lang.AssertionError:
Expected: is "zhonghualong"
but: was "zhonghua"
Expected :zhonghualong
Actual :zhonghua
来源:CSDN
作者:cakincqm
链接:https://blog.csdn.net/chengqiuming/article/details/103753632