Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
云Firestore单元测试Java_Java_Spring Boot_Google Cloud Firestore_Mockito_Junit5 - Fatal编程技术网

云Firestore单元测试Java

云Firestore单元测试Java,java,spring-boot,google-cloud-firestore,mockito,junit5,Java,Spring Boot,Google Cloud Firestore,Mockito,Junit5,我在SpringBoot中创建了一个RESTAPI,我一直在对我的服务进行单元测试,这会调用我的firestore数据库。我试图模拟我的Firestore数据库,这样我就不会在测试中向Firestore数据库添加不必要的数据。然而,当我试图在模拟的Firestore对象上存根响应时,我得到了一个空指针异常。我正在使用JUnit5并通过以下方式模拟Firestore类 @Mock private Firestore db; @InjectMocks private ProductsService

我在SpringBoot中创建了一个RESTAPI,我一直在对我的服务进行单元测试,这会调用我的firestore数据库。我试图模拟我的Firestore数据库,这样我就不会在测试中向Firestore数据库添加不必要的数据。然而,当我试图在模拟的Firestore对象上存根响应时,我得到了一个空指针异常。我正在使用JUnit5并通过以下方式模拟Firestore类

@Mock
private Firestore db;

@InjectMocks
private ProductsService productsService;
在我的测试中,我通过

// Getting a null pointer exception here
when(db.collection("products").add(productToCreate).get()).thenReturn(any(DocumentReference.class));

您不仅要模拟
Firestore
,还要模拟来自它的每个方法调用链中返回的每个对象。mock不是“深”的,不知道如何为该mock上的方法生成更多的mock对象。您必须单独告诉它每个方法调用返回什么

您不仅要模拟
Firestore
,还要模拟来自它的每个方法调用链中返回的每个对象。mock不是“深”的,不知道如何为该mock上的方法生成更多的mock对象。您必须单独告诉它每个方法调用返回什么。这很有效。谢谢你,道格·史蒂文森。如果您想创建一个答案,我会将其标记为已接受的答案。您有任何示例吗?