[英]junit test cases for SpringBoot application which uses non JPA Repository object
I am writing an springboot component which is simply responsible for auditing login operation. 我正在编写一个springboot组件,该组件仅负责审核登录操作。 Since component is only responsible to write into database and there will be no retrieve(select) operation on table.
由于组件仅负责写入数据库,因此对表将没有任何检索(选择)操作。 I am simply using
@Repository
(org.springframework.data.repository.Repository) package and implemented method with insert into statement to write in database. 我只是使用
@Repository
(org.springframework.data.repository.Repository)包和带有insert into语句的实现方法来写入数据库。
@Repository
public interface AuditRepository extends Repository<UserAudit,String> {
@Modifying
@Query(value = "insert into user_audit(user_id,datetime,function_code,ip_address) values (:user_id,:datetime,:function_code,:ip_address)",nativeQuery = true)
@Transactional
public void recordUserAudit(@Param("user_id")String user_id, @Param("datetime") Timestamp datetime, @Param("function_code") int function_code, @Param("ip_address") String ipAddress);
}
Execution of this method on http request does works out. 在http请求上执行此方法确实可以解决。
I'd want to write junit tests on H2 database which verifies record is inserted correctly. 我想在H2数据库上编写junit测试,以验证记录是否正确插入。 for which I am using test profile.
为此,我正在使用测试配置文件。 inserting record on test method with H2 dependency also seem to work - however I currently don't see a way to verify existence of record.
在具有H2依赖关系的测试方法上插入记录似乎也可以工作-但是我目前看不到验证记录存在的方法。
Any suggestions How to achieve it? 有什么建议怎么实现呢?
Ps. 附言 I understand this is possible with either
@JpaRepository
(org.springframework.data.jpa.repository.JpaRepository) or @CrudRepository
but I'd prefer not to use them as using Repository and distinct method will make application light weight. 我知道这可以通过
@JpaRepository
(org.springframework.data.jpa.repository.JpaRepository)或@CrudRepository
但是我不希望使用它们,因为使用Repository,不同的方法会使应用程序轻巧。
you can @Mock
your @Repository
object and with org.mockito.Mockito.verify
and will be able to verify if the Sql written above does gets executed upon calling. 你可以
@Mock
您@Repository
对象,并使用org.mockito.Mockito.verify
,将能够如果上面写的SQL的功能被调用时执行验证。 (authentication request) ie. (身份验证请求),即 mock
AuditRepository
and verify object. 模拟
AuditRepository
并验证对象。
verify(auditRepository).recordUserAudit(user_id,datetime, function_code, ipAddress);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.