简体   繁体   English

使用非JPA存储库对象的SpringBoot应用程序的junit测试用例

[英]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.

相关问题 Spring JPA存储库findAll在JUnit测试中不返回任何数据 - Spring JPA Repository findAll returns no data in JUnit test 为什么我的Spring Boot自动连接的JPA存储库未通过JUnit测试? - Why is my Spring Boot autowired JPA Repository failing JUnit test? 带有 Junit 测试用例的 ClassCastException - ClassCastException with Junit Test Cases 用于配置的JUnit测试用例 - JUnit Test Cases for Config 如何为在源控制器中自动装配的junit测试用例注入servletcontext - How to inject servletcontext for junit test cases which are autowired in source controller 如何测试在 Springboot 中使用 Mapstruct 映射器的服务 class 方法? - How to test Service class methods which uses Mapstruct mappers in Springboot? 使用2个单独的spring应用程序上下文运行Junit测试用例 - Running Junit test cases using 2 separate spring Application Context 为具有自动装配组件的Spring Boot Service应用程序编写JUnit测试用例 - Writing JUnit test cases for a Spring Boot Service Application with autowired components 在诸如JUnit和TestNG的测试框架中,测试执行侦听器的典型用例是什么? - What are typical uses cases for Test Execution Listeners in Testing Frameworks such as JUnit and TestNG? Spring,JPA事务仅在JUnit测试中起作用,但在应用程序中不起作用 - Spring, JPA transactions works only in JUnit test but not in application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM