为了账号安全,请及时绑定邮箱和手机立即绑定

无法在 Spring Boot 测试中模拟persistenceContext

无法在 Spring Boot 测试中模拟persistenceContext

临摹微笑 2021-08-06 08:39:00
我正在使用带有 Mockito 框架的 spring-boot 测试来测试我的应用程序。存储库类之一 EntityManager 作为参考。我的班级如下所示。    @Repository    @Transactional    @Slf4j    public class SomeRepositoryService {        @PersistenceContext        private EntityManager entityManager;        public List<Run> findBySearchCriteria(String searchCriteria,Integer  offset,Integer limit,Integer userId) {        //code        }    }测试类看起来像:@RunWith(SpringRunner.class)@SpringBootTestpublic class RunRepositoryServiceTests {    @MockBean    EntityManager entityManager;     @Autowired    private RunRepositoryService runRepositoryService;    @Test    public void testFindBySearchCriteria() {//code to test    }}当我运行这个时,我得到Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.jpa.repository.support.DefaultJpaContext]: Constructor threw exception; nested exception is java.lang.NullPointerException    Caused by: java.lang.NullPointerException: null        at org.springframework.data.jpa.repository.support.DefaultJpaContext.<init>(DefaultJpaContext.java:53) ~[spring-data-jpa-2.0.9.RELEASE.jar:2.0.9.RELEASE]谁能让我知道如何测试或解决这个问题?
查看完整描述

3 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

您可以使用 SpringRunner 而无需@DataJpaTest. 这对我有用:


@RunWith(SpringRunner.class)

@SpringBootTest(classes = {DataRepository.class, EntityManager.class, 

EntityManagerFactory.class})

public class DataRepositoryTest {


    @MockBean

    private EntityManager entityManager;


    @MockBean

    private EntityManagerFactory entityManagerFactory;


    @Autowired

    private DataRepository repository;


    @Before

    public void setup() {

        Mockito.when(entityManagerFactory.createEntityManager()).thenReturn(entityManager);

    }


    @Test

    public void resultTest() {


        Query q = mock(Query.class);

        when(q.setParameter(anyString(), any())).thenReturn(q);

        when(q.getResultList()).thenReturn(createMockReponse());


        when(entityManager.createQuery(anyString())).thenReturn(q);


        Result r = repository.callQuery();



    }


}


查看完整回答
反对 回复 2021-08-06
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

我遇到了类似的问题,为了解决它,我不得不使用 Springs ReflectionTestUtils 来注入模拟:


@RunWith(SpringJUnit4ClassRunner.class)

public class RunRepositoryServiceTests {



    private EntityManager entityManagerMock; 



    @Autowired

    private RunRepositoryService runRepositoryService;


    @Before

    public void setUp () {

        entityManagerMock = Mockito.mock(EntityManager.class);

        ReflectionTestUtils.setField(runRepositoryService, "entityManager", entityManagerMock);

    }


    @Test

    public void testFindBySearchCriteria() {


        ....


        when(entityManagerMock.anyMethodToMock(anyObject())).thenReturn(...);


        ....


    }

}


查看完整回答
反对 回复 2021-08-06
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

您可以使用 JMockit 轻松模拟使用 @PersistentContext 注释的依赖项


@RunWith(JMockit.class)

public class RunRepositoryServiceTests {


@Mocked EntityManager entityManager; 


private RunRepositoryService runRepositoryService;


@Before

public void setup(){

    runRepositoryService = new RunRepositoryService();

    Deencapsulation.setField(runRepositoryService, entityManager); //because its a private field

}


@Test

public void testFindBySearchCriteria(@Mocked Query mockQuery) {

    //random fake values for input args

    String searchCriteria = "";

    Integer offset = 1;

    Integer limit = 2;

    Integer userId = 1;

    //fake object for output arg

    List<Run> runList = new ArrayList<Run>();

    new Expectations(){{

        entityManager.someMethodToMock(argumentMatchers);

        result = mockQuery;

        times = 1;

    //remaining expactations in your code, which will eventually return result

    }};


    //call method to test

    List<Run> result = runRepositoryService.findBySearchCriteria(searchCriteria, offset, limit, userId);


    //assertions

    assertEquals(runList, result);

}

}


查看完整回答
反对 回复 2021-08-06
  • 3 回答
  • 0 关注
  • 251 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信