InstanceOf matcher does not accept null values -> mockedObject.setValue(null) is not possible
See original GitHub issueI don’t know if this is Mockito or GwtMockito related (https://github.com/google/gwtmock
https://github.com/google/gwtmockito/issues/62). I mocked a text field and used an answer object to connect the getValue()
and setValue()
methods. Both methods work with a string. This worked fine with Mockito 1.10.19 but since Mockito 2.1.0 the following case is broken: textField.setValue(null);
I debugged the code and saw that the answer object is never been called. The reason is, that the InstanceOf.matches()
method does not accept a null value:
public boolean matches(Object actual) {
return (actual != null) &&
(Primitives.isAssignableFromWrapper(actual.getClass(), clazz)
|| clazz.isAssignableFrom(actual.getClass()));
}
textField.setValue(null);
-> actual
is null and therefore the answer object is not called.
In other words: It is not possible to set a null string.
My answer implementation:
public class ValueAnswer implements Answer<Object> {
private Object value;
@Override
public Object answer(final InvocationOnMock invocation) throws Throwable {
if ((invocation != null) && (invocation.getArguments() != null) && (invocation.getArguments().length == 1)) {
value = invocation.getArguments()[0];
}
return value;
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Set value to mocked object but get null - Stack Overflow
@Tom , I know this, but I want to know why I can not directly set value on mocked object, I am not...
Read more >null - JavaScript - MDN Web Docs - Mozilla
The null value represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy ......
Read more >Properties in Mocked object set to null? - Typemock Q&A
I'm trying to isolate a class to test. This class has a couple of properties: SortedList.
Read more >Tutorial - Mocking - JMockit
When a mocked method/constructor has one or more parameters, a recorded/verified expectation like doSomething(1, "s", true); will only match an invocation in ...
Read more >chai.Assertion.null JavaScript and Node.js code examples
getValue ', () => { const variable = new Variable(); it('should return an existing variable ... instanceOf(Object).and.to.be.not.null; // eslint-disable-line ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Martin-Wegner Yes there is in
AdditionalMatchers
@bric3 thanks for your reply. Yes I think in my case it is not a problem. Is there an
or
operator for matchers?