如何在基于SpringBoot的服务的JUnit测试中插入验证器?

如何在基于SpringBoot的服务的JUnit测试中插入验证器?,spring,spring-boot,junit,hibernate-validator,Spring,Spring Boot,Junit,Hibernate Validator,我正在基于调用Spring服务的REST控制器构建SpringBoot CRUD应用程序。收到的POJO具有与验证相关的注释(包括自定义验证器),实际验证在服务内部触发(见下文) 在SpringBoot执行中,一切都非常好 我现在需要为我的服务构建相关的单元测试用例,也就是说,我不想通过SpringBootRunner启动应用服务器 下面是我的带有验证相关注释的患者类 @数据 @实体 公立病人{ @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) 私

我正在基于调用Spring服务的REST控制器构建SpringBoot CRUD应用程序。收到的POJO具有与验证相关的注释(包括自定义验证器),实际验证在服务内部触发(见下文)

在SpringBoot执行中,一切都非常好

我现在需要为我的服务构建相关的单元测试用例,也就是说,我不想通过SpringBootRunner启动应用服务器

下面是我的带有验证相关注释的患者类

@数据
@实体
公立病人{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@NotEmpty(message=“patient.name.mandatory”)
私有字符串名称;
@过去(message=“过去患者的出生日期”)
@NotNull(message=“patient.dateOfBirth.mandatory”)
私人出生日期;
私人紧急情况;
// [...]
}
这是SpringBoot的REST控制器调用的服务

@Service
@Validated
public class PatientService {
  @Autowired
  private PatientRepository repository;

  @Autowired
  private Validator validator;

  public Patient create(Patient patient) {
    if (! patient.isEmergency()) {
      validator.validate(patient);
      // then throw exception if validation failed
    }
    // [...]
  }
}
这是我的JUnit测试

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@Import(ModalityTestConfiguration.class)
public class PatientServiceTest {
  @InjectMocks
  private PatientService service;

  @MockBean
  private PatientRepository repository;

  @Autowired
  private Validator validator;

  @Test
  public void invalidEmptyPatientNoEmergency() {
    Patient p = new Patient();
    Patient result = null;
    try {
      result = service.create(p); // validations must fail -> exception
      assert(false);
    } catch (ConstraintViolationException e) {
      // Execution should get here to verify that validations are OK
      assert(result != null);
      assert(e.getConstraintViolations() != null);
      assert(e.getConstraintViolations().size() != 0);
      // [...]
    } catch (Exception e) {
      assert(false);
    }
}

以防万一,这里是REST控制器(我认为与JUnit测试无关)

我的问题是,我的服务中的验证器总是空的

  • 我已经尝试过用验证器bean创建一个专用的配置文件

以及将配置链接到测试用例

@ContextConfiguration(locations = {"/modality-test-config.xml"})
  • 我试图为验证器定义一个本地@Bean
  • 我玩过@Autowire和不玩@Autowire
  • 我已经更改了@RunWith
我确信这一定是某个地方的一个小细节,但我只是没有在服务的JUnit测试中得到一个NOTNULL验证器

更新

下面是我在headrush注释之后添加的TestConfiguration类

@TestConfiguration
public class ModalityTestConfiguration {

    @Bean("validator")
    public Validator validator() {
        return (Validation.buildDefaultValidatorFactory().getValidator());
    }
}
我还在上面的测试类中添加了相应的@Import注释


仍然不走运:验证程序字段在测试类和服务中都保持为空。另外,TestConfiguration类中的断点似乎没有被调用。

创建一个
@TestConfiguration
类,该类提供
验证器
bean。@我尝试过的headrush没有成功:也许你应该将
@TestConfiguration
类编辑到你的帖子中。@headrush现在完成了。为之前的超短注释表示歉意,该注释本应包含更多信息,但在按Enter键后提交(注释中似乎没有换行)。在
validator
方法的返回上设置一个断点,调试规范并查看是否命中断点。
@TestConfiguration
public class ModalityTestConfiguration {

    @Bean("validator")
    public Validator validator() {
        return (Validation.buildDefaultValidatorFactory().getValidator());
    }
}