question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InstanceOf matcher does not accept null values -> mockedObject.setValue(null) is not possible

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bric3commented, Nov 2, 2016

@Martin-Wegner Yes there is in AdditionalMatchers

0reactions
Martin-Luftcommented, Nov 2, 2016

@bric3 thanks for your reply. Yes I think in my case it is not a problem. Is there an or operator for matchers?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found