【问题标题】:Test the service in springboot在spring boot中测试服务
【发布时间】:2019-09-28 22:33:03
【问题描述】:

这是我的服务:

@Service
public class UserInfoService {

    @Autowired
    private UserInfoServiceClient UserInfoServiceClient; // Call another Rest API

    public ResponseEntity<ResponseUserInfoData> sendUserInfo(String UserId) throws RuntimeException {

        ResponseUserInfoData responseUserInfoData = new ResponseUserInfoData();

           //Get the body from the User service client

            UserServiceDTO UserServiceDTO = UserInfoServiceClient.sendResponse(UserId).getBody();

            //Set the values of responseUserInfoData

            Optional<UserServiceDTO> UserServiceDTOOptional = Optional.ofNullable(UserServiceDTO);

            if (UserServiceDTOOptional.isPresent()) {

                UserServiceDTOOptional.map(UserServiceDTO::getId).ifPresent(responseUserInfoData::setid);

            }
            else return ResponseEntity.noContent().build();

        }


    }

我必须测试它。我是 JUnit 测试的新手。我想测试以下几点:

  1. 检查服务是否返回响应实体

  2. 检查get和set方法是否有效

这是我开始的?

@RunWith(MockitoJUnitRunner.class)
public class ServiceTests {

    @InjectMocks
    private UserInfoService UserInfoService;

    @Mock
    private UserInfoServiceClient UserInfoServiceClient;

    @Mock
    private UserServiceDTO UserServiceDTO;

    @Test
    public void shouldReturnUserInfoData() throws IOException{

        UserInfoService.sendUserInfo("ABC");
    }
}

有什么帮助吗?

【问题讨论】:

    标签: java spring-boot unit-testing junit


    【解决方案1】:

    Mockito 可用于模拟服务的依赖项,以便您可以测试服务中的所有代码路径。在您的情况下,您将希望对 serInfoServiceClient.sendResponse(UserId) 的调用存根,并让它为每个测试用例返回一个特定的 UserServiceDTO。

    测试文件看起来设置正确,您只需要模拟该方法即可为您提供特定测试所需的结果,例如

    @RunWith(MockitoJUnitRunner.class)
    public class ServiceTests {
    
        @InjectMocks
        private UserInfoService UserInfoService;
    
        @Mock
        private UserInfoServiceClient UserInfoServiceClient;
    
        @Test
        public void shouldReturnUserInfoData() throws IOException{
            final String userId = "123";
            // The mocked return value should be set up per test scenario
            final UserServiceDto dto = new UserServiceDto();
            final ResponseEntity<UserServiceDTO> mockedResp = new ResponseEntity<>(dto, HttpStatus.OK);
    
            // set up the mock service to return your response
            when(UserInfoServiceClient.sendResponse(userId)).thenReturn(mockedResp);
    
            // Call your service
            ResponseEntity<ResponseUserInfoData> resp = UserInfoService.sendUserInfo(userId);
    
            // Test the result
            Assert.isNotNull(resp);
        }
    }
    
    

    还有其他方法可以使用 Mockito 来模拟依赖项。我建议通过https://site.mockito.org/的快速入门

    【讨论】:

    • 如果 dto 为空(我没有用值初始化它)那么它表明测试失败。但我得到了 NullPointerException。服务抛出 Runtime Excpetion 并返回 null。 @Bradley Meintjes
    • @bkumar 这很有意义,因为在您的服务中调用了 getBody()。模拟的返回类型应该是ResponseEntity&lt;ResponseUserInfoData&gt;。相应地编辑答案
    猜你喜欢
    • 2021-03-15
    • 2020-08-20
    • 2017-08-01
    • 2022-10-24
    • 2015-08-02
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多