Stub of the spring JpaRepository method

Lev

I have next JPQL query:

@Query(value = "select t.ts as ts, t.ko.eolink.guid as guid from ObjPar t "
        + "where t.tuser.cd = ?1 and t.lst.cd=?2 and t.ts between ?3 and ?4")
List<MeterData> findTimestampByUser(String userCd, String lstCd, Date dtFrom, Date dtTo);

where MeterData - is just projection interface:

public interface MeterData {
    Date getTs();
    String getGuid();
}

Now I need to write something like a stub. I don't need to my query to be executed, but I need it to be returned the List of MeterData.

How can I accomplish it? Should I create the Class implemented MeterData and instantiate it? May be somebody could hint me more simple approach?

My solution:

List<MeterData> findTimestampByUser(String userCd, String lstCd, Date dtFrom, Date dtTo) {

        class LocalMeterData implements MeterData {
            public Date getTs() {
                return new Date();
            }
            public String getGuid() {
                return "2312-1316-4564-4654-4463";
            }
        }

        List<MeterData> lst = new ArrayList<>(5);
        MeterData elem = new LocalMeterData();
        lst.add(elem);
        lst.add(elem);
        lst.add(elem);
        lst.add(elem);

return lst;

}
Lorelorelore

If you need to stub your repository for unit test just use Mockito. Declare your class implementation as private inner class in your test as you already did and instruct Mockito on using it:

MeterData elem = new LocalMeterData();
Mockito.when(yourRepository.findTimestampByUser(anyString(), any(Date.class), any(Date.class)))
        .thenReturn(Arrays.asList(elem, elem, elem, elem));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it possible to use "TRUNCATE" in Spring Data JPA using JpaRepository? Or a more effective method than the standard deleteAll()?

Spring JpaRepository ENUM mismatch

spring-data JpaRepository save() method side effect

Spring boot JpaRepository interface

%Like% Query in spring JpaRepository

Pageable and @Param in a spring data JpaRepository method issue [2]

Spring not able to locate JpaRepository

Spring : How to retrieve set of entities using JpaRepository method without using the primary key?

Custom delete method in JpaRepository

Spring JPARepository Update a field

Spring Data Query Execution Optimization: Parallel Execution of Hibernate @Query Method in JpaRepository

Spring Data JPA: Named method without JpaRepository

Stub unimplemented method in rspec

JpaRepository method name with DISTINCT ON

NonUniqueResultException: JPARepository Spring boot

Spring boot overiding save Method in JpaRepository interface

Hibernate @Filter does not work with Spring JpaRepository.findById method

Rspec - stub module method

Rspec stub public method

Pageable and @Param in a spring data JpaRepository method issue

Regarding Spring JpaRepository method thread safety

Stub method of a constructor property

deleteByIdAndXXX with Spring Boot JpaRepository

Spring Boot Appllication. Batch doesn't work in JpaRepository.saveAll method

Spring Boot JpaRepository not importing

creating Spring JPARepository method for entity variable with name "cId"

Null pointer Error when accessing JPARepository method in Spring data jpa

Spring Data JpaRepository throws LazyInitializationException when using method getById() but not when using findById()

How can I make a Spring Boot JpaRepository repository method pegeable?