Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过设置私有字段,Spring单元测试Rest控制器_Java_Spring Boot_Unit Testing_Junit_Mockito - Fatal编程技术网

Java 通过设置私有字段,Spring单元测试Rest控制器

Java 通过设置私有字段,Spring单元测试Rest控制器,java,spring-boot,unit-testing,junit,mockito,Java,Spring Boot,Unit Testing,Junit,Mockito,我有一个简单的Rest控制器,如下所示 @RestController public class HealthController { private static final CustomLogger logger = CustomLogger.getLogger(HealthController.class.getName()); private HealthService healthService; @Autowired

我有一个简单的Rest控制器,如下所示

 @RestController
    public class HealthController {
    
  private static final CustomLogger logger = CustomLogger.getLogger(HealthController.class.getName());
    
      private HealthService healthService;
    
      @Autowired
      public HealthController(HealthService healthService) {
        this.healthService = healthService;
      }
    
      @RequestMapping(value = "/health", method = RequestMethod.GET)
      public ResponseEntity<?> healthCheck() {
           return healthService.checkHealth();
      }
    
    
    }
我遇到的问题是我的
CustomLogger
。由于它具有外部依赖性,我在尝试测试时遇到了问题。我的服务类中也存在相同类型的记录器。 我如何测试这样一个类。我试过下面的东西

  • 在测试中创建了自定义类名
    CustomLoggerForTest
    。使用
    ReflectionTestUtils.setField(healthService,“logger”,新的CustomerLoggerForTest(healthService.class.getName())
    设置中
    。但这没有帮助。使用此选项,我们无法设置静态字段,因此甚至尝试将其转换为非静态字段
  • setup
    中模拟
    CustomLogger
    尝试如下
    mockStatic(CustomLogger.class);when(CustomLogger.getLogger(any())。然后return(new CustomLoggerForTest(HealthController.class.getName())
    但是没有运气。
    是否有任何我做错了的事情导致了这一切

将记录器导入bean,而不是静态地创建它,像其他依赖项一样模拟它。请看:(特别地)谢谢。如果我这样做的话,效果和预期一样
@Service
public class HealthService {


 private static final CustomLogger logger = CustomLogger.getLogger(HealthController.class.getName());

  public ResponseEntity<?> checkHealth() {
    logger.info("Inside Health");
    if (validateHealth()) {
      return new ResponseEntity<>("Healthy", HttpStatus.OK);
    } else {
      return new ResponseEntity<>("Un Healthy", HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  boolean validateHealth() {
      return true;
  }


}
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HealthController.class)
public class HealthControllerTest {


  @Autowired
  private MockMvc mockMvc;



  @MockBean
  private HealthService healthService;



  @Test
  public void checkHealthReturn200WhenHealthy() throws Exception {
    ResponseEntity mockSuccessResponse = new ResponseEntity("Healthy", HttpStatus.OK);
    when(healthService.checkHealth()).thenReturn(mockSuccessResponse);
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
        "/health").accept(
        MediaType.APPLICATION_JSON);
    MvcResult healthCheckResult = mockMvc
        .perform(requestBuilder).andReturn();
    Assert.assertEquals(HttpStatus.OK.value(), healthCheckResult.getResponse().getStatus());
  }



}