Code Monkey home page Code Monkey logo

Comments (3)

Iconjurer avatar Iconjurer commented on May 27, 2024 1

Expected behaviour should be consistent with the other argument matchers. In the simplest case that means it should not fail if there is at least a single matching verification. In assertArg(...)'s case this would mean there should be at least a single verification that does not throw a throwable of some sort. (junit's AssertionFailedError extends Error, not exception)

@Test
void assertArgShouldNotFailExample() {
    Consumer<Integer> mock = mock();
    mock.accept(1);
    mock.accept(2);
    
    verify(mock).accept(assertArg(i -> assertThat(i).isEqualTo(2)));
}

Because the below two examples do not fail either:

@Test
void verifyExampleA() {
    Consumer<Integer> mock = mock();
    mock.accept(1);
    mock.accept(2);

    verify(mock).accept(argThat(i -> i == 2));
}
@Test
void verifyExampleB() {
    Consumer<Integer> mock = mock();
    mock.accept(1);
    mock.accept(2);

    verify(mock).accept(eq(2));
}

from mockito.

hajubal avatar hajubal commented on May 27, 2024

In the code above, the 'someMock.someMethod' function is called twice with different arguments.
The two verify() functions written in the verification code will be called twice.

verify(someMock).someMethod(
    assertArg(someInt -> assertThat(someInt).isEqualTo(1)),
    anyString()
);

In the above code, assert false is thrown in assertThat() when called with '2' entered as the first parameter. I think it's a question of usage.

from mockito.

mmakos avatar mmakos commented on May 27, 2024

There is one way but it is not very elegant, so just treat it as a fun fact 🙂

You can make some static utility method similar to assertArg:

public static <T> T assertArgs(Consumer<T>... assertions) {
  AtomicInteger counter = new AtomicInteger();
  return ArgumentMatchers.argThat(actual -> {
    int index = counter.getAndIncrement();
    if (assertions.length > index) {
      assertions[index].accept(actual);
    }
    return true;
  });
}

Then in your test you can write:

@Test
void verifyTwoDifferentCallsWithAssertArgs() {
  someMock.someMethod(1, "any string");
  someMock.someMethod(2, "any string");

  verify(someMock, times(2)).someMethod(
          assertArgs(
                  firstInt -> assertThat(firstInt).isEqualTo(1),
                  secondInt -> assertThat(secondInt).isEqualTo(2)),
          anyString());
}

There is probably one more better way to do it, but I didn't check it yet: you could try to handle you assertion in assertArgs so it doesn't break your test, but returns true/false + assertion fail description. Then if verification fails, you have to pass assertion fail description to verification fail message (this can be probably done by properly implementing Matcher or something).

from mockito.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.