【问题标题】:I got the not found error when using Optional.of in junit mockito在 junit mockito 中使用 Optional.of 时出现未找到错误
【发布时间】: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


    【解决方案1】:

    您的配置有问题,我进行了类似的测试并得到了正确的响应。可以分享一下你的配置吗?

    地址存储库

    @Repository
    public interface AddressRepository extends CrudRepository<Address, Integer> {
    
    }
    

    AddressServiceImpl

    @Service
    @RequiredArgsConstructor
    public class AddressServiceImpl implements AddressService {
    
        private final AddressRepository addressRepository;
        
        public Address findById(final Integer id) {
            return addressRepository.findById(id).get();
        }
    
    }
    

    pom.xml

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
            <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>
        </dependencies>
    

    尝试使用:

    when(addressRepository.findById(Mockito.eq(12))).thenReturn(Optional.of(myAddress));
    

    改为:

    when(addressRepository.findById(12)).thenReturn(Optional.of(myAddress));
    

    【讨论】:

    • 谢谢。我在我的问题中添加了 pom.xml,但我认为这不是配置问题,因为当我使用 any() 而不是“12”作为请求的 id 时,一切都运行良好。
    • 在实现时尝试使用 Mockito.eq(12) 而不是 12(我已经在此评论中添加了示例)。
    【解决方案2】:

    为什么会出现这个错误?

    你试图在这里模拟addressRepository.findById(12)。当addressRepository.findById(12) 被调用时,你说它应该返回Optional.of(myAddress)。问题是,它不可能返回Optional.of(myAddress),即Optional&lt;Address&gt;findById 似乎被声明为返回Address,而不是Optional&lt;Address&gt;

    当您删除Optional.of 时,您是在告诉它只返回myAddress,它当然可以这样做。 myAddressAddress 类型,findById 被声明为返回 Address

    就所涉及的类型而言,when 接受T 并返回OngoingStubbing&lt;T&gt;,其中有一个名为thenReturn 的方法,它接受T。由于您将Address 传递给when,因此T 被推断为Address,因此thenReturn 也接受Address

    使用或不使用Optional.of有什么区别?

    如您所见,使用Optional.of 会给您一个错误,而不使用它不会。在这里使用Optional.of 是没有意义的,除非findById 被声明为返回Optional&lt;Address&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      相关资源
      最近更新 更多