mockito

How does one verify state for a mock object?

孤者浪人 提交于 2020-01-07 01:24:43
问题 I have a program Foo that runs on a device and first calls method1() and then method2() on an external module Bar. When both are called, then in response, Bar calls request() on Foo. To mock Bar, it has a reference to Foo and a method call_Request() which calls foo.request() // in class BarTest @Test public void barFlow() { InOrder inOrder = inOrder(mockBar); inOrder.verify(mockBar).method1(anyInt()); inOrder.verify(mockBar).method2(); // what to put as 'thenCallMethod'? when(mockBar.method2(

How to capture arguments which are functions using ArgumentCaptor mockito

老子叫甜甜 提交于 2020-01-06 05:57:47
问题 I have Somefun(){ mockedService.execute(()->{ //function body }) } So I want to run the execute the the method inside the mock. How can I do so? I figured out that somehow if I capture the arguments of this mock (which is a function) and execute it, my work would be done. Is there any way to achieve this? Or some other way. Thanks! 回答1: It would look something like this: @RunWith(MockitoRunner.class) public class SomeFunTest { @Mock Service mockedService; @Captor ArgumentCaptor<Runnable /* or

Can I chain returns with PowerMockito using doReturn?

给你一囗甜甜゛ 提交于 2020-01-06 03:34:08
问题 I have a method call that needs to return valueA the first time it is called and valueB the second time it is called. I'm using a PowerMockito spy, so if I were just needing to return one value it would look like this: PowerMockito.doReturn(valueA).when(mockedObject, "methodName"); It looks like I can do chained returns like this: PowerMockito.when(mockedObject, "methodName").thenReturn(valueA).thenReturn(valueB); But I need to indicate chained returns with doReturn so that the real

Mockito any matcher not working for doAnswer with overloaded method

筅森魡賤 提交于 2020-01-05 07:51:13
问题 I'm trying to stub a void method from a gradle class that has 2 overloads and mockito matchers are matching the worng method and my stub is failling. Here is what I have so far: doAnswer(new Answer<Void>() { @Override Void answer(final InvocationOnMock invocation) throws Throwable { Closure clo = invocation.arguments[0] as Closure clo.run() return null } }).when(mProject).afterEvaluate(any(Closure.class)) But when I debugged the call to see the matcher, it was looking for the method public

error in mocking nested calls

时光怂恿深爱的人放手 提交于 2020-01-05 07:08:43
问题 I have a simple line of Code: DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil .getDraftCampaignDetailsfromCreateDraftRequest(request)); I am trying to mock it like this: ConvertionUtil action1 = PowerMockito.mock(ConvertionUtil.class); when(action1.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details); when(draftCampaignI.createDraftCampaign(details)).thenReturn(details); But I am getting this error: when() requires an argument which

Mock elastic search data

这一生的挚爱 提交于 2020-01-05 05:29:05
问题 How do I mock elastic search data. In the below code rs is an interface which is implemented by ElasticSearchDriver class. rs will have elastic search db details which inturn gets added in the results. While writing unit tests for this code how do we mock the elastic search data. ListThread listThread = new ListThread(rs, index, QueryMap, sortOrder) results.add(executor.submit(listThread)); 来源: https://stackoverflow.com/questions/40084131/mock-elastic-search-data

Mockito not Mocking in Instrumented Android Test [duplicate]

▼魔方 西西 提交于 2020-01-05 04:01:30
问题 This question already has answers here : Mockito AbstractMethodError on initMocks (5 answers) Closed 3 years ago . EDIT: Solution: Changed Mockito Version to 1.+. In my androidTest folder I have something like this: @Before public void setUp() throws Exception { LoginPresenter context = Mockito.mock(LoginPresenter.class); } I got the following dependencies: androidTestCompile "org.mockito:mockito-core:2.2.9" androidTestCompile "com.android.support.test:runner:0.5" androidTestCompile "com

How to unit test Log.e in android?

柔情痞子 提交于 2020-01-04 02:45:09
问题 I need to perform an unit test where I need to check if an error message is logged when a certain condition occurs in my app. try { //do something } catch (ClassCastException | IndexOutOfBoundsException e) { Log.e(INFOTAG, "Exception "+e.getMessage()); } How can I test this? I am getting the below error while unit testing. Caused by: java.lang.RuntimeException: Method e in android.util.Log not mocked. 回答1: There are two ways to do this: You turn to Powermock or Powermokito; as those mocking

Mockito test not seen by Maven?

我与影子孤独终老i 提交于 2020-01-04 01:22:29
问题 I'm building my package with maven (runs without problems) and then I try to make a Mockito test for a class. Dependencies in the pom.xml are as follows: <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-project</artifactId> <version>2.0.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> </dependencies> </project> The mockito

Mockito test not seen by Maven?

爱⌒轻易说出口 提交于 2020-01-04 01:21:18
问题 I'm building my package with maven (runs without problems) and then I try to make a Mockito test for a class. Dependencies in the pom.xml are as follows: <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-project</artifactId> <version>2.0.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> </dependencies> </project> The mockito