13,916 questions
0
votes
1
answer
210
views
Cannot invoke "java.lang.reflect.Method.getDeclaringClass()" because "method" is null in Junit 5
I want to mock a complex case of Java method response into JUnit 5 test which is using hateoas.
The JUnit 5 test:
@ExtendWith(MockitoExtension.class)
public class ReportingATest {
@Spy
protected ...
0
votes
0
answers
22
views
Verify that a mocked object has been called with an specific lambda argument
This is my simple viewmodel
@HiltViewModel
class SplashViewModel @Inject constructor(
checkRemoteConfigUseCase: CheckRemoteConfigUseCase,
private val navigator: Navigator,
@EntryPoint(...
1
vote
1
answer
151
views
How to properly verify extension function calls with Mockito in Kotlin?
I'm trying to verify calls to a Kotlin extension function using Mockito, but I'm encountering an issue where the test is counting internal property accesses rather than the actual function call.
Here'...
0
votes
3
answers
3k
views
@MockitoBean is null while testing spring boot rest controller
I am trying to test Spring Boot's Rest controller.
Problem:
I am using @MockitoBean annotation for mocking the ContactService.java class. The Rest controller depends on this ContactService.java class.
...
0
votes
0
answers
68
views
Mockito can only mock non-private & non-final classes, but the root cause of this error might be different
I want to write some UI tests for a Composable screen.
I constantly facing the same error again and again. The viewmodel cannot be mocked, even though I have declared the viewmodel class as open.
here ...
0
votes
1
answer
40
views
Does Mockito reject a match to an object even if the fields have the same values?
I need to make a mock that says something like
object.name = "Object 1";
when(some_method(object)).thenReturn(someValue)
Being object an instance of an object with certain specific fields, ...
0
votes
1
answer
57
views
Spring Mockito Unable to mock RestTemplate
Hi!
I'm Unable to mock fetching data from GitHubApi
To fetch data i use RestTemplate Exchange
Instead of taking data from github i want to mock my own
Expected :1
Actual :11
Fetch Logic:
@Component
...
0
votes
0
answers
63
views
Flutter easy_localization Widget tests fail with "Localization not found for current context"
I have tried everything for hours and don't seem to be able to make tests work:
// this is taken from https://github.com/aissat/easy_localization/issues/359
// more on this below
class ...
0
votes
1
answer
127
views
Mockito mock a method that returns void with ArgumentMatcher.any() as parameter
I am trying to mock a method and want to execute the internal logic as is when called.
The method returns void so When I am using when(MockedObject.methodReturingVoid(any())).callRealMethod(); It ...
0
votes
0
answers
57
views
I want to Mock internal method calls of a method present in same class
I have a SpringBoot Service class. I am writing Junit for one of the method which internally calls multiple method of the same class. Internal method also calls another method. I tried using when-...
1
vote
0
answers
79
views
MockedConstruction of class with type parameters (type inference?)
I want to test a method that creates an ArrayList<String>. I want to capture what it puts into the list. How do I create a MockedConstruction<ArrayList<String>>? For example:
try(...
0
votes
0
answers
51
views
ClassFormatError while writing a JUnit 4 test with MockitoJunitRunner
I am trying to write a JUnit test for my Android project. I am using the Android junit:4.13.2 and Mockito org.mockito:mockito-core:5.15.2 dependencies.
Here is the gradle sourceSet which I have used
...
1
vote
1
answer
160
views
Unit test IOException with Java, JUnit and Mockito
I'm having problems testing/mocking an IOException when using a Reader.
Code:
public class Loader {
public static void load(Reader reader) {
try (BufferedReader bufferedReader = new ...
-2
votes
2
answers
480
views
How to mock a class having a field set to System.getEnv("...")?
I am trying to write a unit tests for a class that has a variable value set to System.getEnv("..."). Now I am getting initialization error when running the test class. I've tried mocking the ...
0
votes
0
answers
43
views
Unit Testing, sendSignInLinkToEmail(...) must not be null
I am trying to write a unit test case of success for firebase password less sign-in but getting null pointer exception and error that sendSignInLinkToEmail(...) must not be null.
@Mock
private ...
0
votes
2
answers
197
views
Inject a list of mocks during testing in Quarkus
I have a bean like this:
@ApplicationScoped
public class MyService {
@Inject
@All
List<MyDataProvider> dataProviders;
public List<String> getAllData() {
var data = ...
1
vote
1
answer
89
views
JUnit testing error in Java (eclipse)- set up "when..." does not work
In the setup in my test, the line:
when(kc.getToken()).thenReturn(token);
does not work and I get the following error from the ServiceImplementation:
java.lang.NullPointerException: Cannot invoke &...
0
votes
2
answers
43
views
Does this testing code interact with the database or not?
I was asked if this code connects to a database or not. I am not seeing @Mock annotation or @MockBean annotation so I said that it does fetch data from database. Was my answer correct?
@DisplayName(&...
2
votes
1
answer
60
views
How to add executables of "assertAll()" to a list and execute them at the end of the test method?
The following JUnit Jupiter Tests with Mockito are working fine:
@Test
void testVerifMethod() {
System.out.println("*** Start test verify method ***");
mockedList....
0
votes
1
answer
45
views
Why Eclipse doesn't recognize my class which one is for Junit testing?
I am facing a issue that even my Junit class doesn't give an error, Eclipse doesn't run my junit test(I am super new in Junit Testing but, i guess eclipse doesn't recognize my junit test methods.
here ...
0
votes
0
answers
80
views
Mockito is behaving in a weird way
I was doing some testing and I can't get it right.
I was making some tests using mockito and jqwik and I wanted to assert if a method was being called exactly 5 times for that I used the function
@...
0
votes
1
answer
64
views
not able to mock functional interface when testing controller and functional interface is being called from controller
SomeClass instance = someService.getSomething.apply("by-something");
above statement is being used in controller to fetch data. I want to test controller. So i am mocking above line in test ...
1
vote
1
answer
49
views
@Mock and assertNotNull(mock)
Does it make sense to write assertNotNull(mockObject) in header of test method body? When is a null check necessary for a mocked object?
@ExtendWith(MockitoExtension.class)
class SomeTest {
@Mock
...
0
votes
0
answers
74
views
tests working in springboot 2.7(mockito 4.x) not working in spring boot 3.4.0 or 3.1.4 (mockito 5.x)
tests working in mockito 4.x are not working with mockito 5.x getting error after migrating spring from 2.7 to 3.1.4.
Error:
Suppressed: org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
...
1
vote
0
answers
42
views
Ignoring specific library for a module in Gradle
I have migrated our multi-module Gradle project from Mockito v1 to Mockito v3. As part of the migration, the following libraries were upgraded or added:
mockito-core from 1.10.19 to 3.12.4 (and added ...