WebMvcTest尝试使用JpaRepository

时间:2018-07-04 11:30:33

标签: spring-boot spring-data-jpa spring-test spring-boot-test

我有RestController,我想为此编写单元测试。

我写测试:

@RunWith(SpringRunner.class)
@WebMvcTest(CashierController.class)
public class CashierControllerTest {

  @Autowired
  private MockMvc mockMvc;

  @MockBean(name = "mockCashierService")
  private CashierService cashierService;

  private CashierDto cashierDtoResponse;
  private CashierDto cashierDtoRequest;

  @Before
  public void init() {

    cashierDtoRequest = new CashierDto();
    //populate data

    cashierDtoResponse = new CashierDto();
    //populate data

    when(cashierService.getCashierWithAuth(cashierDtoRequest)).thenReturn(cashierDtoResponse);
  }

  @Test
  public void shouldReturnDefaultMessage() throws Exception {
    ...
}

但是当我开始测试时,我得到了错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

我想我理解例外的原因:

我上课:

@Slf4j
@ControllerAdvice
public class ExceptionHandlerAdvice extends ResponseEntityExceptionHandler {

  private final ErrorMessagesRepository errorMessagesRepository;

  @Autowired
  public ExceptionHandlerAdvice(ErrorMessagesRepository errorMessagesRepository) {
    this.errorMessagesRepository = errorMessagesRepository;
  }

在本课程中,我将处理所有例外情况并从数据库中获取文本消息。

Spring测试上下文开始时-spring尝试创建ControllerAdviceErrorMessagesRepository时出现错误。

如何解决此错误?

0 个答案:

没有答案