【问题标题】:IllegalStateException when using Mockito with IntelliJ's Java在 IntelliJ 的 Java 中使用 Mockito 时出现 IllegalStateException
【发布时间】:2018-03-06 00:05:53
【问题描述】:

我收到的错误信息:

java.lang.IllegalStateException:无法初始化插件: 接口 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】:

    我尝试创建一个新项目并进行测试。以下是我在 gradle 文件中的依赖项的外观:

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

    这行得通。

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 1970-01-01
      • 2013-08-27
      • 2023-03-18
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      相关资源
      最近更新 更多