Mockito 2和junit 5:模拟类

Mockito 2和junit 5:模拟类,mockito,junit5,Mockito,Junit5,我对Mockito2和JUnit5有一种奇怪的行为:mockito不能模拟类 我将测试提取到一个简单的测试用例中: @ExtendWith(MockitoJavaExtension.class) class JavaTest { @Test void shouldMockClass(){ final MockedJavaClass mock = mock(MockedJavaClass.class); when(mock.execute()).t

我对Mockito2和JUnit5有一种奇怪的行为:mockito不能模拟类

我将测试提取到一个简单的测试用例中:

@ExtendWith(MockitoJavaExtension.class)
class JavaTest {

    @Test
    void shouldMockClass(){
        final MockedJavaClass mock = mock(MockedJavaClass.class);
        when(mock.execute()).thenReturn(Collections.singletonList("some value"));

        assertEquals(1, mock.execute().size());
    }

    @Test
    void shouldMockInterface(){
        final MockedJavaInterface mock = mock(MockedJavaInterface.class);
        when(mock.execute()).thenReturn(Collections.singletonList("some value"));

        assertEquals(1, mock.execute().size());
    }

}

class MockedJavaClass{
    List<String> execute(){
        throw new IllegalArgumentException();
    }
}

interface MockedJavaInterface{
    default List<String> execute(){
        throw new IllegalArgumentException();
    }
} 
这意味着这个类根本没有被嘲笑

我还尝试了一个外部类(而不是内部类),问题仍然是一样的

这也是我的身材。格雷德尔:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.junit.platform:junit-platform-gradle-plugin:1.1.1"
    }
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

sourceCompatibility = 8
targetCompatibility = 8

repositories {
    mavenCentral()
}

dependencies {
    testCompile "org.mockito:mockito-core:2.18.0"
    testCompile "org.mockito:mockito-junit-jupiter:2.18.0"


    testCompile("org.junit.jupiter:junit-jupiter-api:5.1.1")
    testCompile("org.junit.jupiter:junit-jupiter-params:5.1.1")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.1")
    testRuntime("org.junit.platform:junit-platform-launcher:1.1.1")
}

junitPlatform {
    filters {
        engines {
            include 'junit-jupiter'
        }
        includeClassNamePattern '.*Test'
    }
}
我还尝试使用mockito junit5扩展来注入mock,但仍然存在问题


任何帮助都将不胜感激

您没有在
MockClass
上模拟
execute
函数,因此它使用了类定义中的
execute
实现,换句话说,
IllegalArgumentException

您需要设置mock,以便它们提供方法的虚拟实现,这些方法将作为测试用例的依赖项。在这种特殊情况下,您似乎并没有真正测试任何东西,只是学习Mockito


因此,答案是-提供模拟实现,否则将使用默认值。

我最终找到了解决方案:我的第一个测试是Kotlin测试,在一个“开放类”中,但是一个非开放方法声明

Mockito无法模拟具有包访问权限的方法,也无法模拟最终方法

在这种特殊情况下,当您试图模拟真实方法时,它只调用真实方法,而不告诉您任何事情


也许这篇文章会对某些人有用

以前版本中的Mockito在找不到已定义的mock时返回emptyList。我只是想看看它是否与新版本有所不同!为了清楚起见,我的测试在这里被简化了,但实际上你是对的,我们应该在这里定义mock。mockito内联扩展似乎也允许mock最终方法(不仅仅是最终类)。
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.junit.platform:junit-platform-gradle-plugin:1.1.1"
    }
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

sourceCompatibility = 8
targetCompatibility = 8

repositories {
    mavenCentral()
}

dependencies {
    testCompile "org.mockito:mockito-core:2.18.0"
    testCompile "org.mockito:mockito-junit-jupiter:2.18.0"


    testCompile("org.junit.jupiter:junit-jupiter-api:5.1.1")
    testCompile("org.junit.jupiter:junit-jupiter-params:5.1.1")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.1")
    testRuntime("org.junit.platform:junit-platform-launcher:1.1.1")
}

junitPlatform {
    filters {
        engines {
            include 'junit-jupiter'
        }
        includeClassNamePattern '.*Test'
    }
}