【发布时间】:2021-04-14 21:11:21
【问题描述】:
我正在尝试在 junit mockito 中实现一个简单的测试,以从数据库中获取记录。 这是我的测试课:
@ExtendWith(MockitoExtension.class)
class AddressControllerTest {
@Mock
AddressRepository addressRepository;
@InjectMocks
AddressServiceImpl addressServiceImpl;
@Test
void findByIdTest(){
Address myAddress = new Address();
when(addressRepository.findById(12)).thenReturn(Optional.of(myAddress));
Address theAddress = addressServiceImpl.findById(12);
assertNotNull(theAddress);
}
}
但我收到此错误消息:
“没有找到适合 thenReturn(java.util.Optional
如果我删除“Optional.of”并仅使用对象,我没有收到任何错误。
我的问题是:
- 为什么会出现这个错误?
- Optional.of 的真正作用是什么?换句话说,使用或不使用 Optional.of 有什么区别? 我问了第二个问题,因为我看不到使用 Optional.of 的结果。
这是我的 pom.xml:
<properties>
<java.version>1.11</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<junit-platform.version>5.7.0</junit-platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
标签: java spring spring-boot junit mockito