模拟投影结果Spring Data JPA

Ank*_*sal 2 mockito spring-boot-test

我在我的spring boot项目中使用spring data jpa。

我正在触发JPQL查询,并使用投影来存储查询结果。我的投影:

public interface VeryBasicProjection {
    String getTitle();
    String getUrl();
}
Run Code Online (Sandbox Code Playgroud)

我的服务称为此投影:

public List<VeryBasicDTO> getLatestData(int limit){

        // Pageable for Limit
        Pageable pageable = new PageRequest(0, limit);

        // Get Data from DB
        List<VeryBasicProjection> latestData = tableRepository.getLatestData("live", 2,pageable);
        List<VeryBasicDTO> responseDTO = new ArrayList<>();

        // Map Projection to DTO
        for(VeryBasicProjection veryBasicProjection : latestData){
            VeryBasicDTO veryBasicDTO = new VeryBasicDTO();
            veryBasicDTO.buildDTO(veryBasicProjection);
            responseDTO.add(veryBasicDTO);
        }

        return responseDTO;
    }
Run Code Online (Sandbox Code Playgroud)

现在,我想使用Mockito(单元测试用例)测试此服务,我正在使用when和thenReturn模拟对存储库的调用。

我的问题是如何模拟存储库的结果?那么返回应该是什么?我的意思是如何创建投影实例和setData?

小智 14

这是在 TEST 中模拟投影的最简单方法

VeryBasicProjection VeryBasicProjection = new VeryBasicProjection() {
    String getTitle() {
        return "Title";
    }

    String getUrl() {
        return "url";
    }
};
Run Code Online (Sandbox Code Playgroud)


小智 8

作为 Nis 答案的补充:

如果界面没有设置器,则可以使用地图初始化投影:

ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
Map<String, String> map = Map.of(
        "title", "theTitle",
        "url", "theUrl"
);
VeryBasicProjection projection = factory.createProjection(VeryBasicProjection.class, map);
Run Code Online (Sandbox Code Playgroud)


Nis*_*Nis 5

如果要创建投影实例而不创建实现该接口的类,则可以使用SpelAwareProxyProjectionFactory。

import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;

...
ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
VeryBasicProjection projection = factory.createProjection(VeryBasicProjection.class);
projection.setTitle("theTitle");
projection.setUrl("theUrl");
Run Code Online (Sandbox Code Playgroud)

您还需要在投影中添加setter:

public interface VeryBasicProjection {
    String getTitle();
    String getUrl();
    void setTitle(String title);
    void setUrl(String url);
}
Run Code Online (Sandbox Code Playgroud)

来源:https : //github.com/spring-projects/spring-data-examples/blob/master/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java

  • 你错过了这一部分 `ProjectionFactory factory = new SpelAwareProxyProjectionFactory();` (2认同)

小智 2

我们通过以下方式实现了相同的东西

首先模拟了两种类型的对象:

@Mock
private EntityManager em;
    
@Mock
private DemoProjectRepo demoProjectRepo;
Run Code Online (Sandbox Code Playgroud)

我的demoProjectRepo.findByAll退货List<DemoProjectDevices>

DemoProjectDevices device1 = new DemoProjectDevices();
device1.setAcctNbr("2365897412236589");
device1.setdeviceSeq(new BigDecimal(1));
device1.setCrteTms("2017-07-29 01:21:44.910807");

List<DemoProjectDevices> demoProjectDevices = new ArrayList<DemoProjectDevices>();
demoProjectDevices.add(device1);
Run Code Online (Sandbox Code Playgroud)

对于模拟whenthenReturn

Mockito.when(demoProjectRepo.findByAll("2365897412236589", em))
        .thenReturn(demoProjectDevices);
Run Code Online (Sandbox Code Playgroud)