简体   繁体   中英

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. 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
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.

I'd want to write junit tests on H2 database which verifies record is inserted correctly. 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.

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.

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. (authentication request) ie. mock AuditRepository and verify object.

verify(auditRepository).recordUserAudit(user_id,datetime, function_code, ipAddress);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM