将Mockito与IntelliJ的Java一起使用时出现IllegalStateException

问题描述 投票:0回答:1

我收到的错误消息:

java.lang.IllegalStateException:无法初始化插件:interface org.mockito.plugins.MockMaker

有问题的代码:

List<String> mockList = mock(List.class);

build.gradle依赖项:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    def mockito_version = 'latest.release'

    // For local unit tests on your development machine
    testCompile "org.mockito:mockito-core:$mockito_version"
}

我试过看同样问题的其他人,并不断获得对PowerMock的引用。我不知道这意味着什么,所以如果这是重复我道歉。似乎没有其他问题有解决我的问题的解决方案。库已正确导入,因为我没有任何编译错误。任何帮助将不胜感激。

java unit-testing gradle mockito
1个回答
0
投票

我试图创建一个新项目并测试它。下面是我在gradle文件中的depdencies的样子:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile "org.mockito:mockito-core:2.+"
}

下面是我的测试类:

import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TestList {
    @Test
    public void Test(){
        List<String> myList = mock(List.class);
        when(myList.get(0)).thenReturn("hello world");
        Assert.assertEquals("hello world",myList.get(0));
    }
}

这很有效。

© www.soinside.com 2019 - 2024. All rights reserved.