后端学习2.1:引入Junit,对接口做单元测试

要求

引入Junit,对接口做单元测试

本文将对在Springboot中如何使用Junit进行单元测试进行简单示例和介绍,项目的完整目录层次如下图所示:
(在和main文件夹同级的文件中有一个test文件,在其中添加对应的测试类,一般以testXXXX或XXXXTest命名)
在这里插入图片描述

操作步骤:

1、添加依赖

<!--S        Junit依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
<!--E        Junit依赖-->
<!--S        springboot的测试框架,里面有对junit4的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--E        springboot的测试框架,里面有对junit4的依赖-->

2、对Controller层进行测试

在Springboot中可以通过TestRestTemplate对Controller进行测试;
目前遇到的是Controller中装配了其他Spring容器中的Bean这种情况,例如下面这个控制器,用到了com/example/demo/entity下的User。

@PostMapping(value = "/deleteUserInfo")
    public int deleteUser(@RequestBody User user) throws ServiceException{
        String userId = user.getUserId();
        if(userId==null || userId.equals("")){
            throw new ServiceException(500,"删除失败,请传入用户id");
        }else {
            List<User> users=service.queryByUserId(userId);
            if(users!=null && users.size()>0){
                int count = service.deleteUserInfo(userId);
                if(count>0){
                    return count;
                }else {
                    throw new ServiceException(500,"删除失败,请检查原因");
                }
            }else{
                throw new ServiceException(500,"删除失败,用户id不存在");
            }
        }
    }

此时除了要启动Spring容器,还需要启动内嵌的WEB环境,下面提供一种方法进行测试。

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoControllerTest {
    //注入我要测试的对象
    @Autowired
    private TestRestTemplate template;

    CommonResult result;
    
    @Test
    public void testDeleteUser() {
        User user=new User();
        result = template.postForObject("http://localhost:8080/updateUserInfo",user, CommonResult.class);
        Assert.assertEquals(500, result.getCode());//错误用例,验证用户没传id

        user.setUserId("d36b1940247d4fb59d4851105d5vbbv");
        result = template.postForObject("http://localhost:8080/updateUserInfo",user, CommonResult.class);
        Assert.assertEquals(500, result.getCode());//错误用例,验证id不存在

        user.setUserId("ba8ca9a376614215985ba7d0b2ac4b10");
        result = template.postForObject("http://localhost:8080/updateUserInfo",user, CommonResult.class);
        Assert.assertEquals(500, result.getCode());//成功用例
    }
}

3、遇到的问题

  1. 控制台报错:no runnable method
    原因:test包导入错误
    //import org.junit.jupiter.api.Test;
import org.junit.Test;//正确的包
  1. 要保证template.postForObject/template.getForObject/template.postForEntity/template.getForEntity返回结果和前面的变量类型对应

4、知识点

1.GET类型

1.1getForEntity

public <T> T getForEntity(String url, Class<T> responseType, Object... urlVariables) {
    return this.restTemplate.getForEntity(url, responseType, urlVariables);
}

第一个参数url为要调用的服务的地址;传递参数时可以用下例中name={name}的方法,或者直接写出具体参数值;
第二个参数responseType表示希望返回的body类型,一般为类名.class形式(可以是自定义的类);
示例:

@GetMapping("/getForEntity")
public ResponseEntity<String> getForEntity() {
    ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/getForEntity?name={name}", String.class);
    //ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/getForEntity?id=123456", String.class);
    System.out.println("statusCode:" + response.getStatusCode());
    System.out.println("Body:" + response.getBody());
	return response;
}

getForEntity方法的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。

1.2getForObject

getForObject函数是对getForEntity函数的进一步封装,如果只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject;

public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) {
    return this.restTemplate.getForObject(url, responseType, urlVariables);
}

2.POST类型

2.1postForEntity

public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... urlVariables) {
    return this.restTemplate.postForEntity(url, request, responseType, urlVariables);
}

第一个参数url为要调用的服务的地址;
第二个参数为请求体,传参位置;(也是体现了get和post请求传参时的一个差别所在)
第二个参数responseType表示希望返回的body类型,一般为类名.class形式(可以是自定义的类);
示例:

@PostMapping("/postForEntity")
public ResponseEntity<String> postForEntity() {
	User user=new User();
    user.setSex("女");
    ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/postForEntity",user,String.class);
    System.out.println("statusCode:" + response.getStatusCode());
    System.out.println("Body:" + response.getBody());
	return response;
}

postForEntity方法的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。

2.2postForObject

postForObject函数是对postForEntity函数的进一步封装,如果只关注返回的消息体的内容,对其他信息都不关注,此时可以使用postForObject;

public <T> T postForObject(String url, Object request, Class<T> responseType, Object... urlVariables) {
    return this.restTemplate.postForObject(url, request, responseType, urlVariables);
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个用于构建Java应用程序的开源框架,它提供了一种简化了配置的方式来快速构建应用程序。JUnit是一个用于编写和运行单元测试的开源测试框架,而Mockito是一个用于创建和管理模拟对象的Java库。 下面是一个使用Spring BootJUnit和Mockito进行单元测试的示例: 假设我们有一个UserService类,它依赖于一个UserRepository接口来访问数据库并进行一些操作。我们想要对UserService的方法进行单元测试。 首先,我们需要创建一个测试类,命名为UserServiceTest。在测试类中,我们将使用JUnit的注解来标记测试方法,并使用Mockito来创建模拟对象。示例代码如下: ```java @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { @InjectMocks private UserService userService; @Mock private UserRepository userRepository; @Test public void testGetUserById() { // 配置模拟对象的行为 User user = new User("1", "John"); when(userRepository.findById("1")).thenReturn(user); // 调用被测试的方法 User result = userService.getUserById("1"); // 验证结果 assertEquals("John", result.getName()); } } ``` 在上面的示例中,我们使用了@RunWith注解来指定使用MockitoJUnitRunner运行测试,这样就能自动创建和管理模拟对象。使用@InjectMocks注解将被测试的对象自动注入到测试类中,使用@Mock注解创建模拟对象。 在testGetUserById方法中,我们首先使用when方法配置userRepository模拟对象的行为,表示当传入参数为"1"时,返回一个指定的User对象。 然后,我们通过调用userService的getUserById方法来测试该方法的逻辑。最后,使用assertEquals断言来验证结果是否符合预期。 以上就是一个使用Spring BootJUnit和Mockito进行单元测试的示例。通过使用Mockito创建模拟对象,我们可以更容易地测试各个方法的逻辑,而不依赖于实际的数据库。这样可以提高测试效率并确保代码的质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值