Newest 'mockito' Questions - Stack Overflow

Questions tagged [mockito]

Mockito is a mocking framework for Java. It is inspired by EasyMock but aims to simplify mock stubbing, verification and tooling even further.

0
votes
2answers
13 views

Java Mockito: How to prepare different answers

int calledCnt = 0; doAnswer(i -> { if (calledCnt == 0) { calledCnt += 1; throw new InternalServerErrorException("test"); } return false; }).when(myClass.someFunction(any(...
0
votes
0answers
15 views

Powermock can't mock static class

I have a class with code similar to : public class class1{ private static final ConfigurationService config = Util.getInstance(ConfigurationService.class); private SendQueueMessages sender; ...
0
votes
0answers
8 views

Using Mockito to test deleting records from an HBase Table?

I am writing unit test cases for an offset management utility (table stores offsets of partitions along with their status, and I am fetching/updating when needed). To do so I need to mock a connection ...
0
votes
2answers
19 views

How to correctly use Mockito's verify on a spring @Service test

I have this service (all in kotlin): @Service class MyService { fun getSomeString(): String = "test" } And this integration test class: @RunWith(SpringRunner::class) @SpringBootTest @...
0
votes
0answers
15 views

javax.validation.ConstraintDeclarationException: HV000131. Cascaded validation in hierarchical objects

I have the next hierarchy: public class A extends B { @Valid public A(String languageCode, C token) { super(languageCode, token); } } The B class has a property call language code ...
0
votes
1answer
42 views

Losing mocking if the mocked object sets to null in the implementation class

I want to mock an Object which works in a loop. Code to be tested in such a way that the object (which I want to mock) gets re initializes at every iteration. So when I am mocking I can see Mocked ...
0
votes
1answer
16 views

How to mock a mapper.query method when the return type is a PaginatedList of items JAVA

I am trying to test a method that uses DynamoDBMapper to query items from a dyamoDB table. My method works fine but the problem comes when I'm trying to test. I'm using Junt4 and Mockito. Basically,...
1
vote
0answers
17 views

Unit testing coroutine with LiveData observer

I have a viewmodel which contains two livedata member variables and a method that uses coroutines to call out to an API. This all works fine running the app, my problem is when unit testing this it ...
1
vote
2answers
58 views

How to write JUnit test for a method that calls API?

I've to write test for a class that calls an API and then processes the response. The class has two public functions and a private function. The first public method fetches a list of IDs. The second ...
2
votes
1answer
31 views

How to use conditional mock in Mockito

I have a scala method call mocked in with mockito. Let the object be a and method be b and the method b takes parameter of type Map[String, String]. I want to have a Map which meets a certain ...
0
votes
1answer
15 views

Example of using multiple then in test Android

While learning android testing have figured out that I can use 'then' several times. But don't understand cases where it might be helpful. For example when(mockObject.add(any())) .thenReturn(true,...
2
votes
2answers
61 views

Determine two lists are equal without caring about ordering Mockito

I have got into a situation where I am stubbing through when-then to match a list but I am getting error most of the times due to ordering issue. List<String> myListToMatch = new ArrayList<...
3
votes
1answer
25 views

InvalidUseOfMatchersException when using mockito to force a CloneNotSupportedException in a Copy() method being junit tested

I'm trying to access part of a copy method protected by a try catch using mockito while attempting to get 100% coverage in my junit tests. The class that contains the method I want to access ...
0
votes
0answers
9 views

SharedPreferences always null in test class

In test class SharedPreferences object is always null.What is the issue? How to solve this? This is my code and here assertNull always true public class MyBagPresenterTest { MyBagPresenter ...
-1
votes
1answer
14 views

Problems learning stubbing and mocking: Not able to link mocked class with Unit Test

At first I have a general question about mocking and stubbing. I can't really see the difference between these two words. Here is what I know so far: When creating a Unit test and the method being ...
2
votes
1answer
31 views

How to mock service and test POST controller method

Expecting the controller method to return the newly created weather resource, but the response body is empty. Mocked the service to return a weather resource when the service method is called. POST ...
-1
votes
1answer
30 views

When I run my test case, my main method of spring-boot gets evoked and my mockito method doesn't work. How to solve this?

I have written a test case which mocks a method call by which the application is reading an excel file from the system. When I run my test case in debug, I observe that my control goes through the ...
0
votes
0answers
11 views

Error when trying to mock the NavController

Trying to follow the example https://developer.android.com/guide/navigation/navigation-testing, I have an expected error org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: ...
3
votes
1answer
58 views

Mocking with anonymous class in Java

I'm learning about dependecy injection and testing with Mockito. And I just found a tutorial where someone explain an application with dependency injection and without dependency injection. This is ...
0
votes
0answers
22 views

Unit test the ThreadPoolTaskExecutor initialization

I have a method that starts a ThreadPool and returns an Executor object. The code is provided below, @Configuration @EnableAsync @ComponentScan( "com.xyz.bbb.ccc" ) public class EllaConfiguration { ...
0
votes
1answer
38 views

Null Pointer exception passed by test method

Method that needs to be tested - located in AnalyticsServiceImpl.java class public void trackSplashState(MeasurementViewEnum measurementViewEnum) { HashMap<String, Object> data = ...
0
votes
1answer
18 views

Mocking constructor using powermock on the class which needs to be tested

I am able to mock a constructor call using powermock from inside a class which I want to test. This works when I add the class I want to test in @PrepareForTest. But once I add my class there, even ...
0
votes
0answers
13 views

verifyPrivate not working in Android unit tests

I have been struggling to verify number of calls of a private method in my Android code. The unit tests that I am using is listed below. The signature of the private method that I am testing is ...
0
votes
0answers
16 views

Mockito Mocking of final class static method not always working [duplicate]

Setup: Two classes, both final with static methods: UUID (java), BUKKIT (spigot-api). import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.junit.Before; import org.junit.Test; ...
0
votes
1answer
28 views

Tests fail when run together but succeed individually even when instances are re-mocked before each test

I've looked at this SO post and made sure my tests don't share the same mocked instances, but the tests still fail when run together but succeed when run individually. I suspect there might be ...
0
votes
1answer
11 views

Mockito: argThat for methods taking multiple arguments

I am trying to use Mockito's argThat API: verify(mockService).methodA(argThat((List ids, int b) -> ids.get(0).equals("123"))); mockService has methodA which takes two parameters: a List and a ...
0
votes
0answers
23 views

Testing: doAnswer never called because arguments with any() not match

I have a problem with a doAnswer declaration which is never called. First of all, say that i have the same declaration for other method and it works. So my suspects are in the arguments matching.. ...
0
votes
0answers
20 views

How to write Android unit test for fetching image using content resolver?

I am a beginner in Android unit testing. I wanted to write unit test for the following code. I am not able to find a way to mock an image for using in the test. Please help. fun ...
0
votes
3answers
36 views

Mocking instance method [duplicate]

Maybe I am missing something, can someone tell me why this unit test case does not work? Thanks. import java.util.Random; public class A { private B b; public A() { b = new B(); ...
0
votes
1answer
13 views

Writing Junit test case of DataSource

I want to use mock it to mock data source object in the below code but every time I am getting null pointer exception. @Component @Transactional public class ConnectionUtils { @Autowired private ...
0
votes
0answers
8 views

Dart mockito, verify that one of two possible calls chains was performed

Imagine I am unit-testing Meyers diff algorithm implementation: I have [1, 2, 3, 4] as a first data snapshot and [2, 1, 4 ,3] as a second. The implementation itself dispatches updates to some listener,...
0
votes
0answers
10 views

Mockito test for setting values

I am trying to write a unit test for the following code for my fragment Code: @Override public void setEditAddressData(String addressLineOne, String addressLineTwo, String postalCode, ...
2
votes
1answer
19 views

Use Mockito to register spring bean when doing mockMvc testing

Here is my idea, I try to test my Restful controller with MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); mockMvc.perform(post(...).param(..)) .andExpect(...); The API ...
0
votes
0answers
17 views

Android: Espresso + Dagger + DaggerMock + Mockito

I'm trying to setup Espresso in our Android project (which uses Dagger). I'm having lots of troubles with Mockito, and I'm getting the errors in here. This is part of my app.gradle: ...
0
votes
2answers
42 views

How to make Unit test passed with use Mockito?

How I understand Mockito.mock create the stub of the service (or another object). I have simple handler: public class Handler { private HttpSender sender; public Handler(...
1
vote
1answer
16 views

calling another method from method :: org.mockito.exceptions.misusing.MissingMethodInvocationException

I want to use Mockito to mock methods call inside the method I want to test . So that I can return the value which I want for the further processing of method . Code : DAOFactory dao = org.mockito....
0
votes
1answer
22 views

Mocking static method inside static method on powermock

I have a static method that looks for the current userSession object and returns a string. public static String getCurrentUser() { UserSession userSession = buildUserSession(); ...
0
votes
0answers
16 views

JUnit Testing Log4j2 logging with Lombok

I have a java class annotated with lombok like so: @Log4j2 public class Foo { private boolean logStr = true; public void doStuff(String str) { if (logStr) { log.info(str); } ...
0
votes
1answer
41 views

How to write a unit test to verify returning Observable?

I have an ApiRepository class which will contain all my API calls, but currently only has one: public class RestApiRepository { private RestClient restClient; public RestApiRepository(RestClient ...
1
vote
1answer
19 views

Mock with void method causes 'local class name incompatible with stream class name “void”' when de-/serialized via Spark

When trying to create a test for an application using Spark I face the following error: java.io.InvalidClassException: java.lang.Void; local class name incompatible with stream class name "void" ...
-1
votes
1answer
19 views

What's the difference between mockitoSession and MockitoAnnotations.initMocks

I read about mockitoSession and also about MockitoAnnotations.initMocks but i didn't understand what is the difference between the two? MockitoAnnotations.initMocks(this); Mockito.mockitoSession() ...
0
votes
1answer
15 views

mocking when multiple RestTemplate

I have multiple resttempletes in configured for my springboot project. I am unable to mock individual resttemplate for my test case. I tried with same name mock, the mocking is not happening. Here ...
0
votes
0answers
27 views

Mocking Service call which call a different class

HI I am trying to mock service which internally calls a different class for the execution .Do I need to Mock that class as well. Below is the code snippet. I am Mocking class B .My question is how to ...
0
votes
1answer
35 views

mockito spy does not work on a factory bean method

I have a springboot that I am writing unit test for. There is a factoryBean out of which I get service object in runtime. I want to test that a specific method on this service object is invoked. Here ...
0
votes
2answers
37 views

How to return the argument passed to a Mockito-mocked method in the thenReturn function?

This is what I want to achieve. MyClass doSomething = mock(MyClass.class); when(doSomething.perform(firstParameter, any(Integer.class), any(File.class))) .thenReturn(firstParameter); Basically ...
0
votes
1answer
18 views

What are the ways to write Mockito Junits for Rowmapper class

What are the possible ways to write Mockito Junit test cases for Rowmapper class? Without interacting with DB how we can do mocking data.
0
votes
1answer
19 views

Unable to mock a JSON response in Spring RestTemplate

I'm trying to mock a string in RestTemplate response, But I'm getting below error in code. Please provide inputs. Error: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced ...
0
votes
0answers
17 views

How to mock unit test a class with both mocked and unmocked classes

Mock some of injected classes and for some use real object of classes. @RunWith(MockitoJUnitRunner.class) public class TestClass { @Mock private A a; @Mock private B b; @Mock ...
0
votes
0answers
26 views

creating a custom argument matcher confusing implementation

android 3.4.1 I have seen someone creating a custom argument matcher like the following. However, I am having difficulty understanding how it works. What I can understand its a method that takes a ...
-1
votes
1answer
42 views

How to write Unit test for ViewModel that contains RxJava/RxAndroid

I'm trying to refactor one pretty old project, so I started implementing new architecture (MVVM) with Dagger2, RxJava, RxAndroid... Now everything is connected and working fine, now the problem is, I ...