我们正在尝试使用我们的应用程序进行Spring Contract测试。我们的应用程序使用JPA进行数据库访问。在我们的应用程序中,我们试图模拟数据库存储库类的返回。但是,Spring会在应用程序启动时尝试初始化JPA,因此即使我们从不想访问数据库,它也会尝试连接到数据库。
是否有某种方法可以禁止JPA初始化?我们已经在application.yml中使用了它,但是仅仅停止JPA还不够:
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
这是合同的基本类别:
@ExtendWith({SpringExtension.class, MockitoExtension.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMessageVerifier
public class BaseContractTest {
@Autowired
MobileWearController mobileWearController;
@MockBean
UserDeviceRepository userDeviceRepository;
@BeforeEach
public void setup() {
userDeviceRepository = mock(UserDeviceRepository.class);
UserDevice mockUserDevice = UserDevice.builder()
.scorecardId("XYZ123")
.accessToken("ACCESSABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
.refreshToken("REFRESHABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").build();
// mock return data from database here
when(userDeviceRepository.getUserTokens(anyString())).thenReturn(java.util.Optional.of(mockUserDevice));
StandaloneMockMvcBuilder standaloneMockMvcBuilder
= MockMvcBuilders.standaloneSetup(mobileWearController);
standaloneMockMvcBuilder.setControllerAdvice(GlobalExceptionMapper.class);
RestAssuredMockMvc.standaloneSetup(standaloneMockMvcBuilder);
}
}
有什么想法吗?