【问题标题】:mockito when() is not availablemockito when() 不可用
【发布时间】:2019-08-26 18:07:56
【问题描述】:

我想使用 when() 方法模拟 userRepository,但该方法本身不可用 Spring boot下载的jar包import static org.mockito.Mockito.when是没有的

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.bapan</groupId>
  <artifactId>SpringBoot-Mockito</artifactId>
  <version>0.0.1-SNAPSHOT</version>


  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>
</project>

    /**
 * 
 */
package org.bapan.api.test;

import org.bapan.repository.UserRepository;
import org.bapan.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApiTest {
    /**
     * 
     */
    @Autowired
    private UserService userService;
    /**
     * 
     */
    @MockBean
    private UserRepository userRepository;

    @Test
    public void getAllUsers() {
        //when(userRepository.findAll()).thenReturn();
    }

}

当方法自身不存在时。 在哪个 jar 中我可以找到 when 方法()? 获取when方法需要添加哪些确切的依赖项

【问题讨论】:

    标签: mockito spring-boot-actuator


    【解决方案1】:

    根据mvnrepo pagespring-boot-starter-parent 包括mockito-core 2.23.4。 像往常一样,它应该在那个罐子里找到。

    Mockito 类版本2.23.4Javadoc

    如果您无法在您的 testsource 目录中解析任何与 mockito 相关的类,您可能需要正确配置 maven(或您的项目)来告诉它 testSourceDirectory 是哪个(在构建部分下/参见 link) .

    如果这无助于从您的类路径中检查 jar,则可能它已损坏,您需要强制 maven 重新下载它。

    (注意:我没有在您的文件中看到导入,但我猜您已将其删除,因为它不起作用)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多