使用嘲笑单元测试和Spring Boot模拟方法内部的局部变量

时间:2019-08-27 01:39:47

标签: java spring unit-testing mockito

我正在尝试模拟局部变量上方法的返回,但是它始终为null,显示java.lang.NullPointerException

我在@service中包含以下代码:

@Service
@AllArgsConstructor
public class VideoEncodingService {
  private final MainService mainService;
  @Value("${bucket.videos.aws.s3}")
  private String bucketS3;

  @Value("${accessKey.acess.bucket}")
  private String accessKeyS3;

  @Value("${secretKey.acess.bucket}")
  private String secretKeyS3;

  @Value("${bit.movin.url.api.output.s3}")
  private String urlEndpointOutputS3;

public void createOutputS3(Config cf) {
    // encoding/outputs/s3
    OutputS3DTO os3 = mainService.returnObject(urlEndpointOutputS3,
        new OutputS3DTO(bucketS3, accessKeyS3, secretKeyS3), new TypeReference<OutputS3DTO>() {});
    cf.setIdOutputS3(os3.getId());
  }
}
@Service
@AllArgsConstructor
public class MainService {

  private final RestTemplate restTemplate;


  private final ObjectMapper mapper;

  @Value("${bit.movin.url}")
  private String urlBaseBitMovin;

  @Value("${bit.movin.api.key}")
  private String apiKey;

  protected <T> String post(String urlApi, T object) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("X-Api-Key", apiKey);
    HttpEntity<?> request = new HttpEntity<>(getJSON(object), headers);
    return restTemplate.postForObject(urlBaseBitMovin + urlApi, request, String.class);
  }

  private <T> String getJSON(T instance) throws RuntimeException {
    try {
      return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(instance);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  @SuppressWarnings("unchecked")
  public <T> T returnObject(String endpoint, T instance, TypeReference<T> typeReference)
      throws RuntimeException {
    try {
      String jsonResult = post(endpoint, instance);
      JSONObject jSONObject = new JSONObject(jsonResult);
      JSONObject resultObject;
      resultObject =
          jSONObject.getJSONObject(KEY_DATA_BIT_MOVIN).getJSONObject(KEY_RESULT_BIT_MOVIN);
      if (resultObject != null) {
        return (T) mapper.readValue(resultObject.toString(), instance.getClass());
      }
    } catch (Exception e) {
      throw new RuntimeException(e.toString());
    }
  }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OutputS3DTO{
  /**
   * 
   */
  private static final long serialVersionUID = 1L;

  private String id;
  private String name = "S3 storage config";
  private String bucketName;
  private String accessKey;
  private String secretKey;

  public OutputS3DTO(String id) {
    this.id = id;
  }
}

我正在用模拟测试:

package br.com.video;

@RunWith(MockitoJUnitRunner.class)
public class EncodingVideoTestApplicationTests {

  @InjectMocks
  private VideoEncodingService videoEncodingService;

  @Mock
  private MainService mainService;


  @Test
  public void createOutputS3() {
    videoEncodingService = new VideoEncodingService(mainService);
    OutputS3DTO s3 = new OutputS3DTO("1");
    Config config = new Config();
    Mockito.when(mainService.returnObject(Mockito.anyString(), Mockito.any(), Mockito.any()))
        .thenReturn(s3);
    videoEncodingService.createOutputS3(config);
    assertEquals(config.getIdOutputS3(), "1");
  }

}

如何在VideoEncodingService.createOutputS3中获取OutputS3DTO os3,使其在测试类中具有OutputS3DTO s3的值?

0 个答案:

没有答案