【发布时间】:2021-10-01 21:28:20
【问题描述】:
我正在为调用 REST 后端服务和 MongoDB 的服务层编写单元测试。但是,当在本地执行我的测试时工作正常,但是在编译期间将我的应用程序打包到我的 CICD 管道中时,它会尝试运行单元测试,同时在使用 RestTemplate 和 MongoTemplate bean 时会出现空指针异常。
MyTestService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private MongoTemlate mongoTemplate;
public String callDBAndBackend() {
// some business logic
UpdatedObject mongoDoc = mongoTemplate.updateFirst(updateObject);
restTemplate.exchange(reqEntity, ResponseObject.class);
return "Success";
}
}
@ExtendWith(MockitoExtension.class)
MyTestServiceTest {
@Mock
private RestTemplate restTemplate;
@Mock
private MongoTemplate mongoTemplate;
@InjectMocks
private MyTestService myTestService;
@Test
public void testCallDBAndBackend() {
when(restTemplate.exchange(reqEntity, ResponseObject.class)).thenReturn("Success"); // In this line getting Null pointer. Cant able to inject restTemplate
when(mongoTemplate.updateFirst(updateObject)).thenReturn("Success"); // In //this line getting Null pointer. Cant able to inject mongoTemplate
myTestService.callDBAndBackend();
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- <exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions> -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
编译此代码时,我将 RestTemplate 和 MongoTemplate 对象设为 null,这会导致 NPL。如果我从 IDE 运行相同的代码,它可以正常工作并获得有效的 RestTemplate 和 MongoTemplate 模拟对象。
【问题讨论】:
-
你能添加你的测试类的导入吗?是否可以在同一个测试中混合使用 JUnit 4 和 JUnit 5?
标签: unit-testing mockito spring-boot-test