相关疑难解决方法(0)

如何用mockito模拟最后一堂课

我有一个最后的课,类似这样:

public final class RainOnTrees{

   public void startRain(){

        // some code here
   }
}
Run Code Online (Sandbox Code Playgroud)

我在其他类中使用此类,如下所示:

public class Seasons{

   RainOnTrees rain = new RainOnTrees();

   public void findSeasonAndRain(){

        rain.startRain();

    }
}
Run Code Online (Sandbox Code Playgroud)

在我的JUnit测试类中,Seasons.java我想模拟这个RainOnTrees类.我怎么能和Mockito一起做这件事?

java junit mockito

169
推荐指数
13
解决办法
21万
查看次数

最终方法嘲笑

我需要使用mockito使用final方法模拟一些类.我写过这样的东西

@Test
public void test() {
    B b = mock(B.class);
    doReturn("bar called").when(b).bar();   
    assertEquals("must be \"overrided\"", "bar called", b.bar());
    //bla-bla
}


class B {
    public final String bar() {
        return "fail";
    }
}
Run Code Online (Sandbox Code Playgroud)

但它失败了.我尝试了一些"黑客",它的确有效.

   @Test
   public void hackTest() {
        class NewB extends B {
            public String barForTest() {
                return bar();
            }
        }
        NewB b = mock(NewB.class);
        doReturn("bar called").when(b).barForTest();
        assertEquals("must be \"overrided\"", "bar called", b.barForTest());
    }
Run Code Online (Sandbox Code Playgroud)

它有效,但"闻起来".

那么,正确的方式在哪里?

谢谢.

java testing unit-testing mocking mockito

60
推荐指数
5
解决办法
6万
查看次数

Android Kotlin MockMaker问题

嗨,我正在尝试模拟最后一个类(默认情况下,kotlin中的所有类都是最终类),并在gradle中添加了以下依赖项:

testImplementation 'junit:junit:4.12'
testImplementation 'au.com.dius:pact-jvm-consumer-junit_2.11:3.5.10'
testImplementation "org.mockito:mockito-android:2.13.0"
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation "org.mockito:mockito-core:2.13.0"
//testImplementation 'io.mockk:mockk:1.8'
testImplementation 'org.assertj:assertj-core:3.8.0'

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "org.mockito:mockito-core:2.13.0"
androidTestImplementation "org.mockito:mockito-android:2.13.0"
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"
Run Code Online (Sandbox Code Playgroud)

该模仿内联应该使您能够模拟最终的kotlin类,因此我使用testImplementation和androidTestImplementation将其添加到我的Java单元测试和仪器测试中

在构建项目时,出现以下错误:

More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?如果我删除了类内联的androidTestImplementation,它可以正常编译,但是在运行仪器测试时,我收到了类比错误,说它无法模拟最终类。

android mockito kotlin

6
推荐指数
1
解决办法
2089
查看次数

如何在eclipse项目中使用mockito启用最终类模拟?

我在 maven 项目中使用mockito-core-2.7.21,它按预期工作。

但是为了能够模拟最终类,我在mockito-extensions\org.mockito.plugins.MockMaker源文件夹下创建了一个文件并添加了文本mock-maker-inline.

根据https://www.baeldung.com/mockito-final以及使用 Mockito 2模拟最终课程,它应该允许我模拟最终课程但是当我运行我的 junit 测试时,我收到以下错误:

Java 版本:1.8

操作系统:Windows 10

Junit:4 和 5(我都试过)

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
    at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
    at com.sun.proxy.$Proxy7.isTypeMockable(Unknown Source)
    at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
    at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
    at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186)
    at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
    at org.mockito.Mockito.mock(Mockito.java:1729)
    at org.mockito.Mockito.mock(Mockito.java:1642)
    at siemens.esd.grpc.services.UTCreateModuleServiceTest.testCreateBlockInterface(UTCreateModuleServiceTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) …
Run Code Online (Sandbox Code Playgroud)

mockito

4
推荐指数
1
解决办法
2244
查看次数

模拟对象使用Mockito调用final类静态方法

我刚刚开始模拟我们应用程序的不同层.当我调用最终类静态方法时,我的一个模拟对象正在返回NPE.有没有解决的办法?

例如

when(mockObject.someMethod(FinalClass.staticMethod(someParam)).anotherMethodCall)
    .thenReturn("someString");
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing mockito

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

mockito ×5

java ×3

junit ×2

unit-testing ×2

android ×1

kotlin ×1

mocking ×1

testing ×1