Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify method bug with Maps #2000

Closed
catch441 opened this issue Aug 11, 2020 · 3 comments
Closed

Verify method bug with Maps #2000

catch441 opened this issue Aug 11, 2020 · 3 comments

Comments

@catch441
Copy link

Hey,

I think I found a bug with the verify method. It seems that there is some async action behind the method. Here is a sample code with the main problem:

import static org.mockito.Mockito.verify;

import java.util.HashMap;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class TestClass {

	@Mock
	Validator validator;

	@Test
	public void validateTest() {
		new SomeClass().myMethod(validator);
		verify(validator).validate(new HashMap<String, String>(), "potatoe");
	}
}
import java.util.HashMap;
import java.util.Map;

public class SomeClass {

	public void myMethod (Validator validator) {
		Map<String, String> map = new HashMap<>();
		validator.validate(map, "potatoe");
		map.put("tomatoe", "ketchup");
	}
}
import java.util.Map;

public interface Validator {

	public void validate(Map<String, String> map, String value);
}

Every time I execute the test I get the following message:

Argument(s) are different! Wanted:
validator.validate(
{},
"potatoe"
);
-> at com.ue.economyplayer.logic.impl.TestClass.validateTest(TestClass.java:21)
Actual invocations have different arguments:
validator.validate(
{"tomatoe" = "ketchup"},
"potatoe"
);
-> at com.ue.economyplayer.logic.impl.SomeClass.myMethod(SomeClass.java:10)

at com.ue.economyplayer.logic.impl.TestClass.validateTest(TestClass.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

The arguments are not matching, but the map is filled after the method invocation and should be empty at the invocation time.

My Environment:

JDK 1.8
JUnit 5
Mockito-Core 3.4.6
Mockito-JUnit-Jupiter 3.4.6

@catch441 catch441 changed the title Verify bug with Maps Verify method bug with Maps Aug 12, 2020
@TimvdLippe
Copy link
Contributor

Does the issue also happen when you remove the .put?

@catch441
Copy link
Author

No. After removing the put, the test is working.

@TimvdLippe
Copy link
Contributor

Unfortunately, Map is one of the classes that Mockito relies on internally for its behavior. Stubbing Map will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on a DoNotMock feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as "Infeasible". Apologies for the uninformative exception that is thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants