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.

mockito
4,386 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
5 votes
1 answer
1k views

How to ignore a toast in JUnit tests?

I want to test a method in a fragment but in this method I call a Toast: Toast.makeText(getActivity(), getString(R.string.signs), Toast.LENGTH_SHORT).show(); Now I want to test the method with JUnit ...
schokomonsterchen's user avatar
5 votes
2 answers
2k views

Replacing ManagedExecutorService with ExecutorService in test

I have some service which uses an Java EE ManagedExecutorService (in Wildfly 9) public class FooService{ @Resource ManagedExecutorService executorService; } For a test with Mockito, I would ...
user140547's user avatar
  • 7,900
5 votes
0 answers
2k views

Testing a fragment that requires the parent activity to implement a listener

I'm trying to test a fragment using Robolectric. The fragment contains the code: public class MyDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle ...
clay_to_n's user avatar
  • 583
5 votes
0 answers
2k views

Spring MVC controller JSON date serialization during testing

I'm writing unit tests for Spring MVC 4.2.x REST controllers and have an issue with dates serialized by Jackson (2.6.x). While running the application the dates (java.util.Date) are serialized in the ...
yktoo's user avatar
  • 2,826
5 votes
0 answers
1k views

Android Unit Test with Retrofit/RxJava/Roboletric and Mockito

I'm trying to write a Unit test with Robolectric and Mockito. I've a retrofit observable and I would like to test with a mock response. I've tried something like that link here but I got a java.lang....
fGab's user avatar
  • 59
5 votes
2 answers
1k views

Can you mock a chain of method calls with Mocktio/PowerMock if one of them is static?

Imagine I have the following line in a piece of code I am unit testing: SomeClass.method1().method2(); Where SomeClass is a class, method1() is a static method inside SomeClass that returns some ...
Seb's user avatar
  • 959
5 votes
1 answer
2k views

Mockito ArgumentCaptor Capture Conditional on Matcher

In mockito we can capture arguments for a method that might be called many times like so: verify(mocked, atLeastOnce()) .myMethod(myCaptor.capture()); Then myCaptor.getAllValues() However then ...
qwwqwwq's user avatar
  • 7,189
5 votes
0 answers
3k views

Mock MVC javax.validation test

I would like to mock my validation to test. Size validation doesn't run in my test. Max size of id is 40 character, and I wait for an exception when I test it with 42 long id. MyObject.java @...
BravoNine's user avatar
  • 101
5 votes
1 answer
3k views

Can I stub methods called through reflection using Mockito?

I'm trying to write unit tests for my code that uses reflection to call hidden methods on an object I want to stub for my test: methodUnderTest(Arg argument) { Method toCall = Arg.class.getMethod("...
Greg's user avatar
  • 10.6k
5 votes
2 answers
1k views

RoboGuice unit test injecting app module instead of test module

I am trying to write a JUnit test for an Android Service using RoboGuice 2.0. I have a test module that binds injected dependencies to Mockito mock objects. However, when I run the test, the real ...
svattom's user avatar
  • 330
5 votes
1 answer
1k views

Android. Robolectric. Testing AccountManager results

In my application I use method with account manager for getting owner emails. How I can test this method with Robolectric? Should I use for this purpose mocking? If I'm right, can I use Mockito? Is ...
NikedLab's user avatar
  • 893
5 votes
1 answer
2k views

Android Junit test - Mockito class not found exception

I'm attempting to build a unit test on Android which utilizes the mockito-all-1.8.5.jar file from mockito.org. I added the jar file to my project using project > properties > project build path > add ...
Jack BeNimble's user avatar
5 votes
1 answer
758 views

How to inject a bean in a ConstraintValidator with MockMVC?

I have a custom ConstraintValidator: @Component public class FooValidator implements ConstraintValidator<FooAnnotation, String> { @Inject private FooRepository fooRepository; @...
oscar's user avatar
  • 1,716
5 votes
1 answer
7k views

How to make android unit test for notifications?

I have the class handleFirebaseMessages . It contains the function onMessageReceived() . This function is in charge of receiving data and creating notifications. onMessageReceived() receives data in a ...
mjsxbo's user avatar
  • 2,196
5 votes
1 answer
4k views

difference between Powermock and Powermockito

Can anyone could elaborate about PowerMock and PowerMockito. I didn't even get documentation for powermockito. Both used for mocking static and private methods in different way I guess. ...
anand krish's user avatar
  • 4,373
4 votes
0 answers
4k views

Mockito ClassImposterizer$3 not found in GitLab CI/CD

I have set up some tests in my project, that uses Mockito to mock some classes. To run those tests, I use Gradle. When running them locally, everything works and all tests pass. However, for one week ...
szoszk's user avatar
  • 399
4 votes
0 answers
156 views

How to Stub on Spy Object’s suspend method?

I got the idea how to stub suspend functions of regular mock object and also how to stub regular method of spy object. However, I did not find a way to stub the suspend function of spy objects. For e....
Shanks Redemption's user avatar
4 votes
0 answers
717 views

@SpyBean doesn't intercept method calls when test class isn't executed separately

I have multiple @WebMvcTest-annotated test classes that run successfully if executed separately (e.g. via mvn -Dtest=BTest test or via IDE). However if they'are executed together (e.g. via mvn test or ...
ka3ak's user avatar
  • 2,631
4 votes
1 answer
4k views

How do static variables work in Junit when initiailizing them from multiple modules?

Lets assume I have a class with a static variable like so: public class var{ public static String myvar = "thor"; } And junit classes like so: public class test1{ @Test public ...
Fernando's user avatar
  • 413
4 votes
0 answers
419 views

Espresso: How to UI test Sms Retriever with User consent?

I am currently listening for Google Play services broadcasted action SmsRetriever.SMS_RETRIEVED_ACTION to check whether OTP SMS was retrieved. First, I start SmsRetriever: SmsRetriever.getClient(...
Jokubas Trinkunas's user avatar
4 votes
1 answer
5k views

How to correctly mock NavigatorObserver in Flutter with Mockito?

I have a test where I'm trying to observe the behaviour of the [Navigator] when the app navigates from contacts_footer.dart to create_and_edit_contact.dart (push) and back (pop). Using verify from the ...
Calvin Gonsalves's user avatar
4 votes
0 answers
1k views

Mocking functions used inside spark udfs

I had a user defined function as follows: def myFunc(df: DataFrame, column_name: String, func:(String)=>String = myFunc) = { val my_udf = udf{ (columnValue: String) => func(columnValue) } ...
ghost's user avatar
  • 1,127
4 votes
0 answers
118 views

Project (with Guice, Gson and Mockito) + Java module system possible without using "open" extensively?

I am trying to add module-info.java to my project to make it conform with Java module system. However, I am struggling with a lot of issues as I am using libraries which are doing some injections like:...
pitschr's user avatar
  • 156
4 votes
0 answers
4k views

Mockito when().thenAnswer() not mocking future

I have a repository class which return Future by calling another function from another class which returns same Future. And I was trying to mock the repository class using the mockito when()....
Elias Andualem's user avatar
4 votes
0 answers
3k views

Mockito cannot mock this class exception

I'm trying to run instrumentation tests in Release. Running the tests on on API 28 - Pie (only) I get the exception below for all tests. No issues running the same tests on devices with APIs 23-27 &...
vvbYWf0ugJOGNA3ACVxp's user avatar
4 votes
0 answers
387 views

VerifyStatic on System class is failing with PowerMock 2.0.2

Reference to verifystatic on System class is failing after upgrading powermock version to 2X from 1X Below are my current configurations mockito-core - 2.27.0 powermock-api-mockito2 - 2.0.2 powermock-...
arkay's user avatar
  • 149
4 votes
0 answers
1k views

Unit Testing Firebase Database with Android

I have created an app that allows users to create a transaction. When this transaction is created it is added to the database (Firebase Realtime Database). Once a transaction is created, a series of ...
C.Gibbons's user avatar
  • 313
4 votes
0 answers
283 views

Mockito - @SpyBean on bean with @RabbitListener annotation method creates another consumer

I have an integration test class with @SpyBean annotation above one dependency and testing number of invocations using verify, however @SpyBean adds another queue consumer and verify method answering ...
Adam De's user avatar
  • 131
4 votes
0 answers
2k views

Mocking LiveData object in Android ViewModel using Mockito

I have a ViewModel look like this. class SignInViewModel(private val requestDataUseCase: RequestDataUseCase) : ViewModel() { ... var isLoading = MediatorLiveData<Boolean>() ... fun ...
Boonya Kitpitak's user avatar
4 votes
0 answers
88 views

Mockito + Espresso + Dagger2 => ClassNotFoundException when running SDK is lower than target SDK

I came across this limitation when trying to mock a class that refers to classes present in SDK >= 26 and executed the test in a device running SDK 24. I created a test app to better understand the ...
Augusto Pedroza's user avatar
4 votes
1 answer
2k views

Why do my Android unit tests fail when run together, but pass when run individually?

Here is my whole test class: @RunWith(JUnit4::class) class ExplorerRemoteImplTest { // Mocks private val mockDatabase = mock<FirebaseFirestore>( defaultAnswer = ...
szaske's user avatar
  • 1,977
4 votes
0 answers
595 views

(Mockito + Kotlin) eq() and any() in Android Test returns null

Things are going fine in test package, however, they are going wayward in androidTest package. Things I have already tried: Instead of Mockito.any() I have already tried following snippet fun <...
Dinesh Singh's user avatar
4 votes
2 answers
2k views

Mocking the arguments passed to callbacks (lambdas)

How would I mock methods that accept a lambda using Mockito so that I am able to control which arguments are passed into the callback? I am specifically trying to mock the JDBI method useExtension ...
oligofren's user avatar
  • 21.8k
4 votes
0 answers
391 views

Inject Context object to create FirebaseAnalytic instance | Mockito

I'm writting test case for Firebase Analytics(FBA) event wrote in application. In one of case I need to check if FBA object is available or not. I'm following this to create context using Mockito: ...
CoDe's user avatar
  • 11.1k
4 votes
1 answer
2k views

SQLiteOpenHelper test class for android

I am trying to write a unit test for my accessor class for an SQLite database in Android developer. However when I run the test class, I get the following error: java.lang.RuntimeException: Method ...
Jack Wilkinson's user avatar
4 votes
0 answers
820 views

Mocking Java 8 lamba function with Mockito 2

I am using JDBI 3 and have written the following utility function. public class JdbiHelper { // ... public <T, DAO> T fetch(final Function<DAO, T> function, Class<DAO> ...
Simon Kent's user avatar
4 votes
0 answers
4k views

Error while using powermock in JUnit

While I am running JUnit file I am getting the following type of error. Here is the complete stacktrace below: org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not ...
LPatil's user avatar
  • 233
4 votes
1 answer
840 views

Mockito verify a method is called onces after spy though it is never call

I have a unit test and find a strange problem in the very beginning GSLClient gslClient = spy(new GSLSolaceClient()); String orgId = "10006"; int orgFlag = Destination....
JaskeyLam's user avatar
  • 15.6k
4 votes
1 answer
4k views

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/jms/JMSException

im getting the below issue when i run junit test cases, the class is as follow:; public class RoeSubmissionBeanTest { RoeSubmissionBean roeSubmissionBean; ServiceActivation serviceAct = new ...
Nare Hopane's user avatar
4 votes
1 answer
3k views

Error testing with Spring Boot Test

I'm trying to implement a simple unit test on my service. The code of my test class is the following: @RunWith(SpringRunner.class) @SpringBootTest public class UserServiceTest { @Mock ...
davioooh's user avatar
  • 24.2k
4 votes
1 answer
576 views

@PrepareForTest, @RunWith and lambda expressions

Today I was debugging quite a huge test case that involved a lot of whenNew mocking. Eventually it all boiled down to the fact that using @PrepareForTest and @RunWith annotations with PowerMock don'...
Coderslang Master's user avatar
4 votes
2 answers
174 views

How to test a method that depends on another already tested method?

I'm working on a method that can be considered a specialization of another already defined and tested method. Here's an example code to illustrate: public class ProductService { public void ...
Psycho Punch's user avatar
  • 6,600
4 votes
0 answers
229 views

FrameLayout.addView() on Mockito spy object adds view to mChildren, but mChildrenCount is still 0

I encountered a problem when using Mockito.spy() method. I am writing a unit test to add indifferent (it doesn't matter if it's a real view, or a mock) view to Mockito spy object of FrameLayout in a ...
koto's user avatar
  • 730
4 votes
0 answers
4k views

Mockito MVC - how to pass value for @RequestParam binding a string array

I'd like to write a unit test using Mockito. The method to be tested is written by Spring MVC as follows: @RequestMapping(value = "/save", method = RequestMethod.POST, produces = "text/html;charset=...
Mengjun's user avatar
  • 3,189
4 votes
1 answer
136 views

Spring MVC release 4.2.6 seems does not inject mock service into the controller when testing controller method

I really searched and followed the steps of creating a unit test class for spring MVC controller, however unit test is running with a green pass flag but the framework uses the original service class ...
user3496320's user avatar
4 votes
0 answers
407 views

Akka Http Websocket Test using Mocking

I am quite new to akka-http and especially to websockets. I have a service which runs on localhost:8080 and has a path /greeter. When a request comes on this path then it forwards the request to /...
sonu mehrotra's user avatar
4 votes
1 answer
525 views

Jersey mockito Exception

I have to test a class for REST web services an exception was generated. I use Jersey, Mockito and the IDE is Eclipse, it works fine with Maven command line but not with Eclipse. class : import ...
EL missaoui habib's user avatar
4 votes
0 answers
2k views

Method getReadableDatabase in android.database.sqlite.SQLiteOpenHelper not mocked

I'd like to test the methods inside the databasehelper class. I develop with Android studio. I use Mockito for testing and I got error message running the test: Method getReadableDatabase in ...
Ferenc's user avatar
  • 41
4 votes
0 answers
595 views

Scala / PowerMockito - Java final classes causing build error in Scala Tests using Powermockito

I have a final class: public final class AClass { private final AConfig aClassConfig; public final static BeanName = "aClass" } and I'm trying to mock it in tests: @RunWith(classOf[...
Bryce Langlotz's user avatar
4 votes
3 answers
2k views

Intellij ScalaTest with Mockito, test framework quit unexpectedly

I used ScalaTest and Mockito in the test. If I used command line the test passed. But when I run using Intellij got following error. It seems only for the test has mockito imported. An exception or ...
ttt's user avatar
  • 3,984

1
2
3 4 5
88