无法在 Spring Boot 测试中模拟persistenceContext

我正在使用带有 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)

@SpringBootTest

public 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]

谁能让我知道如何测试或解决这个问题?


临摹微笑
浏览 252回答 3
3回答

波斯汪

您可以使用 SpringRunner 而无需@DataJpaTest. 这对我有用:@RunWith(SpringRunner.class)@SpringBootTest(classes = {DataRepository.class, EntityManager.class,&nbsp;EntityManagerFactory.class})public class DataRepositoryTest {&nbsp; &nbsp; @MockBean&nbsp; &nbsp; private EntityManager entityManager;&nbsp; &nbsp; @MockBean&nbsp; &nbsp; private EntityManagerFactory entityManagerFactory;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private DataRepository repository;&nbsp; &nbsp; @Before&nbsp; &nbsp; public void setup() {&nbsp; &nbsp; &nbsp; &nbsp; Mockito.when(entityManagerFactory.createEntityManager()).thenReturn(entityManager);&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void resultTest() {&nbsp; &nbsp; &nbsp; &nbsp; Query q = mock(Query.class);&nbsp; &nbsp; &nbsp; &nbsp; when(q.setParameter(anyString(), any())).thenReturn(q);&nbsp; &nbsp; &nbsp; &nbsp; when(q.getResultList()).thenReturn(createMockReponse());&nbsp; &nbsp; &nbsp; &nbsp; when(entityManager.createQuery(anyString())).thenReturn(q);&nbsp; &nbsp; &nbsp; &nbsp; Result r = repository.callQuery();&nbsp; &nbsp; }}

尚方宝剑之说

我遇到了类似的问题,为了解决它,我不得不使用 Springs ReflectionTestUtils 来注入模拟:@RunWith(SpringJUnit4ClassRunner.class)public class RunRepositoryServiceTests {&nbsp; &nbsp; private EntityManager entityManagerMock;&nbsp;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private RunRepositoryService runRepositoryService;&nbsp; &nbsp; @Before&nbsp; &nbsp; public void setUp () {&nbsp; &nbsp; &nbsp; &nbsp; entityManagerMock = Mockito.mock(EntityManager.class);&nbsp; &nbsp; &nbsp; &nbsp; ReflectionTestUtils.setField(runRepositoryService, "entityManager", entityManagerMock);&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testFindBySearchCriteria() {&nbsp; &nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; &nbsp; &nbsp; when(entityManagerMock.anyMethodToMock(anyObject())).thenReturn(...);&nbsp; &nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; }}

慕码人8056858

您可以使用 JMockit 轻松模拟使用 @PersistentContext 注释的依赖项@RunWith(JMockit.class)public class RunRepositoryServiceTests {@Mocked EntityManager entityManager;&nbsp;private RunRepositoryService runRepositoryService;@Beforepublic void setup(){&nbsp; &nbsp; runRepositoryService = new RunRepositoryService();&nbsp; &nbsp; Deencapsulation.setField(runRepositoryService, entityManager); //because its a private field}@Testpublic void testFindBySearchCriteria(@Mocked Query mockQuery) {&nbsp; &nbsp; //random fake values for input args&nbsp; &nbsp; String searchCriteria = "";&nbsp; &nbsp; Integer offset = 1;&nbsp; &nbsp; Integer limit = 2;&nbsp; &nbsp; Integer userId = 1;&nbsp; &nbsp; //fake object for output arg&nbsp; &nbsp; List<Run> runList = new ArrayList<Run>();&nbsp; &nbsp; new Expectations(){{&nbsp; &nbsp; &nbsp; &nbsp; entityManager.someMethodToMock(argumentMatchers);&nbsp; &nbsp; &nbsp; &nbsp; result = mockQuery;&nbsp; &nbsp; &nbsp; &nbsp; times = 1;&nbsp; &nbsp; //remaining expactations in your code, which will eventually return result&nbsp; &nbsp; }};&nbsp; &nbsp; //call method to test&nbsp; &nbsp; List<Run> result = runRepositoryService.findBySearchCriteria(searchCriteria, offset, limit, userId);&nbsp; &nbsp; //assertions&nbsp; &nbsp; assertEquals(runList, result);}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java