无法在测试方法中访问 JpaRepository

问题描述

我正在尝试在 SpringBoot 中对具有 JPA 存储库对象的服务进行单元测试。我创建了一个存储库类的模拟对象,它给出了编译时错误错误:无法访问 JpaRepository when(mockClaimEntiryRepository.findClaimEntityByClaimNumber(actual)).thenReturn(claimEntity);

有人能帮我解决这个问题吗?

注意:我可以从我的主应用程序中使用 jpa 存储库。

以下是我的代码片段:

测试类:

package com.allstate.ClaimHistory.api.services;
import com.allstate.ClaimHistory.Repositories.ClaimEntityRepository;
import com.allstate.ClaimHistory.entity.ClaimEntity;
import com.allstate.ClaimHistory.services.GetFileNoteDataService;
import org.hamcrest.core.Is;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBoottest;
import org.springframework.test.context.junit4.springrunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(springrunner.class)
@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.RANDOM_PORT)
public class GetFileNoteDataServiceTest {

    @InjectMocks
    GetFileNoteDataService mockGetFileNoteDataService;

    @Mock
    private ClaimEntityRepository mockClaimEntiryRepository;

    @Test
    public void test(){

        String expected = "a";
        String actual =  "a";
        ClaimEntity claimEntity= new ClaimEntity();

        when(mockClaimEntiryRepository.findClaimEntityByClaimNumber(actual)).thenReturn(claimEntity);

        mockGetFileNoteDataService.getClaimIdByClaimNumber(actual);
        assertEquals(actual,expected);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)