Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

java - I got the not found error when using Optional.of in junit mockito

I'm trying to implement a simple test in junit mockito to fetch a record from database. This is my test class:

@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);
    }
}

But i got this error message: "no suitable method found for thenReturn(java.util.Optional<com.luv2code.springsecurity.demo.entity.Address>)"

If i remove "Optional.of" and use only the object, I didn't get any errors.

My questions are:

  • why did I get that error?
  • What does Optional.of really do?.In another word what's the difference in using or not using Optional.of? I asked second question because I couldn't see the result of using Optional.of.

Here is my 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>
question from:https://stackoverflow.com/questions/65641230/i-got-the-not-found-error-when-using-optional-of-in-junit-mockito

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

why did I get that error?

You are trying to mock addressRepository.findById(12) here. When addressRepository.findById(12) is called, you are saying that it should return Optional.of(myAddress). The thing is, it can't possibly return Optional.of(myAddress), which is an Optional<Address>. findById seems to be declared to return Address, not Optional<Address>.

When you remove Optional.of, you are telling it to return just myAddress, which it certainly can do. myAddress is of type Address, and findById is declared to return an Address.

In terms of the types involved, when accepts a T and returns an OngoingStubbing<T>, which has a method called thenReturn, which takes a T. Since you passed an Address into when, T is inferred to be Address, and so thenReturn takes an Address too.

what's the difference in using or not using Optional.of?

As you have seen, using Optional.of gives you an error, while not using it doesn't. It doesn't make sense to use Optional.of here, unless findById is declared to return Optional<Address>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...