mockito

How to mock generic method in Java with Mockito?

你说的曾经没有我的故事 提交于 2021-01-21 04:18:44
问题 How can we mock the IRouteHandlerRegistry ? The error is Cannot resolve method thenReturn(IHandleRoute<TestRoute>) public interface RouteDefinition { } public class TestRoute implements RouteDefinition { } public interface IHandleRoute<TRoute extends RouteDefinition> { Route getHandlerFor(TRoute route); } public interface IRouteHandlerRegistry { <TRoute extends RouteDefinition> IHandleRoute<TRoute> getHandlerFor(TRoute route); } @Test @SuppressWarnings("unchecked") public void test() { // in

cannot resolve method “when”

萝らか妹 提交于 2021-01-18 11:11:28
问题 I'm following the Vogella tutorial on Mockito and get stuck pretty much immediately. IntelliJ displays cannot resolve method 'when' for the class below. ...what did I miss? import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class MockitoTest { @Test public void test1() { MyClass test = Mockito.mock(MyClass.class); // define return value for method getUniqueId() test.when

cannot resolve method “when”

只谈情不闲聊 提交于 2021-01-18 11:10:23
问题 I'm following the Vogella tutorial on Mockito and get stuck pretty much immediately. IntelliJ displays cannot resolve method 'when' for the class below. ...what did I miss? import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class MockitoTest { @Test public void test1() { MyClass test = Mockito.mock(MyClass.class); // define return value for method getUniqueId() test.when

cannot resolve method “when”

孤人 提交于 2021-01-18 11:06:33
问题 I'm following the Vogella tutorial on Mockito and get stuck pretty much immediately. IntelliJ displays cannot resolve method 'when' for the class below. ...what did I miss? import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class MockitoTest { @Test public void test1() { MyClass test = Mockito.mock(MyClass.class); // define return value for method getUniqueId() test.when

Mockito.mockedStatic for method with arguments

﹥>﹥吖頭↗ 提交于 2021-01-18 04:37:48
问题 All the examples provided for mockedStatic method is for method without parameters. Is there a way to mock methods with parameters. examples provided: https://javadoc.io/static/org.mockito/mockito-core/3.4.6/org/mockito/Mockito.html#static_mocks mocked.when(Foo::method).thenReturn("bar"); assertEquals("bar", Foo.method()); mocked.verify(Foo::method); } What I want: I tried below and it does not work. mocked.when(Foo.methodWithParams("SomeValue")) 回答1: It is possible, you need to use a lambda

Mockito.mockedStatic for method with arguments

五迷三道 提交于 2021-01-18 04:35:28
问题 All the examples provided for mockedStatic method is for method without parameters. Is there a way to mock methods with parameters. examples provided: https://javadoc.io/static/org.mockito/mockito-core/3.4.6/org/mockito/Mockito.html#static_mocks mocked.when(Foo::method).thenReturn("bar"); assertEquals("bar", Foo.method()); mocked.verify(Foo::method); } What I want: I tried below and it does not work. mocked.when(Foo.methodWithParams("SomeValue")) 回答1: It is possible, you need to use a lambda

Application.class mock in Android UnitTest

爱⌒轻易说出口 提交于 2021-01-07 04:14:51
问题 I have an Application class. open class AppController : MultiDexApplication() { companion object { @JvmStatic lateinit var instance: AppController private set } override fun onCreate() { super.onCreate() instance = this } } I use my code for extension. Int.kt fun Int.pxToDp(): Int { return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), AppControllerMaster.instance.applicationContext.resources.displayMetrics).toInt() } I need to use that in a unit test. when use that I

Application.class mock in Android UnitTest

醉酒当歌 提交于 2021-01-07 04:12:34
问题 I have an Application class. open class AppController : MultiDexApplication() { companion object { @JvmStatic lateinit var instance: AppController private set } override fun onCreate() { super.onCreate() instance = this } } I use my code for extension. Int.kt fun Int.pxToDp(): Int { return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, this.toFloat(), AppControllerMaster.instance.applicationContext.resources.displayMetrics).toInt() } I need to use that in a unit test. when use that I

SpringBoot系列: 单元测试2

会有一股神秘感。 提交于 2021-01-06 04:55:47
之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现在补齐SpringBoot单元测试的主要知识点. 测试有很多种, 有单元测试 、集成测试 、冒烟测试 、回归测试 、端到端测试 、功能测试。 它们作用不同, 但又有所重叠. 1. 单元测试: [责任人: 开发人员]针对 class 级别的测试, 针对一个类, 写一个测试类. 在项目中, class 之间依赖调用是很常见的事情, 如果要测试的 classA, 又调用了classB, 这时候就没有遵守 "在一个class范围内测试", 自然不算单元测试, 应归为集成测试了. 不过这时候, 我们可以使用 mock 技术模拟被依赖的 classB, 这样整个测试又聚焦在classA 自身的功能, 所以又变回为单元测试. 2. 集成测试: [责任人: 开发人员]是单元测试在粒度上更进一步, 测试不同class之间的组合功能. 3. 冒烟测试: 可以理解为预测试, 一旦预测试失败(发现重大bug), 就直接停止测试, 打回给开发人员. 4. 回归测试: 重跑原有测试用例. ===================== spring-boot-starter-test 依赖包括: ==========

Spock mock private variable

烈酒焚心 提交于 2021-01-03 09:54:22
问题 I'm wondering how I can mock some private variable in a class with Groovy/Spock. Let's say we have this code: public class Car { private Engine engine; public void drive(){ System.out.println("test"); if (engine.isState()) { // Do something } else { // Do something } } } In Mockito I can write: @Mock private Engine engine; @InjectMocks private Car car = new Car(); @Test public void drive() { when(engine.isState()).thenReturn(true); car.drive(); } But I don't know how to do the same in Spock.