因为JpaRepository有@DataJpaTest. @DataJpaTest允许在 Spring 中对 JPA 存储库进行简单和隔离的测试。
我们正在使用spring-data-r2dbc. 是否有等效的@DataJpaTestforReactiveCrudRepository来单独测试它?
r2dbc配置:
Run Code Online (Sandbox Code Playgroud)spring: profiles: default r2dbc: url: r2dbc:postgresql://testserver.dev.net:1234/test?ssl=true&sslmode=require username: test password: test connection_timeout: 20000
jpa配置:
Run Code Online (Sandbox Code Playgroud)spring: profiles: default datasource: url: jdbc:postgresql://testserver.dev.net:1234/test?ssl=true&sslmode=require username: test password: test hikari: connectionTimeout: 20000 maximumPoolSize: 5
jpa连接工作正常并返回结果,r2dbc无法连接到服务器,无法找到有效的证书
导致:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
当 r2dbc 和 ssl 关闭时,它表示 pg_hba.conf 没有主机条目。为什么它只要求带有 r2dbc 配置的证书。
与 r2dbc 的依赖关系:
Run Code Online (Sandbox Code Playgroud)<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-r2dbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency>
对于jpa,我使用spring web starter和jpa starter,两者都是spring版本2.4.1。我被困住了,找不到这个错误的原因。欢迎任何解决方案。
postgresql ssl spring-boot spring-data-r2dbc r2dbc-postgresql
我为某些服务编写单元测试,但我不明白为什么我模拟的某些方法不调用。我尝试测试testDeleteDeviceWhenDeviceNotFound()方法并且通过了,但是当我尝试时testDeleteDeviceSuccess()遇到问题
@Service
@Slf4j
@AllArgsConstructor
public class DeviceService {
private DeviceRepository deviceRepository;
private SyncSender syncSender;
public Mono<Void> deleteDevice(long deviceId) {
return deviceRepository
.findById(deviceId)
.switchIfEmpty(Mono.error(new NotFoundException()))
.flatMap(existingDevice -> deviceRepository
.delete(existingDevice)
.then(syncSender.sendDeviceDelete(existingDevice.getDeviceId()))
);
}
Run Code Online (Sandbox Code Playgroud)
和我的测试
@ExtendWith(MockitoExtension.class)
class DeviceServiceTest {
@Mock
private DeviceRepository deviceRepository;
@Mock
private SyncSender syncSender;
@InjectMocks
private DeviceService deviceService;
@Test
@DisplayName("Test deleteDevice when NotFoundException")
void testDeleteDeviceWhenDeviceNotFound() {
long deviceId = 100L;
Mockito.when(deviceRepository.findById(deviceId)).thenReturn(Mono.empty());
Mono<Void> mono = deviceService.deleteDevice(deviceId);
StepVerifier
.create(mono)
.expectErrorMatches(throwable -> throwable instanceof NotFoundException)
.verify();
}
@Test
@DisplayName("Test …Run Code Online (Sandbox Code Playgroud) 如标题所述,我无法在 Postgres 数据库中插入或更新记录。我刚刚开始使用 spring 和 kotlin,所以可能缺少一些非常基本的配置。提前致谢
这是我的代码库
用户存储库
@Repository
interface UserRepository : ReactiveCrudRepository<User, Int> {}
Run Code Online (Sandbox Code Playgroud)
用户模型 @Table(“user_app”)
data class User (
@Id
@Column("id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
val id : Int? = null,
@Column("username")
val username : String?,
@Column("email")
val email : String?,
@Column("name")
val name : String?,
@Column("password")
val password : String?
)
Run Code Online (Sandbox Code Playgroud)
用户控制器 @Component
class UserController{
@Autowired
private lateinit var userRepository : UserRepository
fun getAllUsers() : Flux<User> = userRepository.findAll()
fun getUserById( userId: Int) : Mono<User> = userRepository.findById(userId)
fun createUser(user: User): Mono<User> …Run Code Online (Sandbox Code Playgroud) postgresql reactive-programming kotlin spring-data-r2dbc r2dbc
java ×2
postgresql ×2
r2dbc ×2
kotlin ×1
mockito ×1
spring ×1
spring-boot ×1
spring-data ×1
ssl ×1
unit-testing ×1