Uploaded image for project: 'Unitils'
  1. Unitils
  2. UNI-247

ArgumentMatcher in behavior setup of chained Mocks produce UnitilsException

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 3.3
    • Fix Version/s: None
    • Component/s: mock
    • Labels:
      None
    • Environment:
      Unitils 3.3, Java 1.6, TestNG, Eclipse, Ubuntu

      Description

      Use of ArgumentMatcher in chained mock behaviour setup produces Exceptions.

      Test:

      import static org.unitils.mock.ArgumentMatchers.isNull;
      import static org.unitils.mock.ArgumentMatchers.notNull;

      import org.unitils.UnitilsTestNG;
      import org.unitils.mock.Mock;

      import org.testng.annotations.Test;


      public class ArgumentMatcherInChainedMockTest extends UnitilsTestNG {
          
          interface A {
              B getB(String x);
          }
          interface B {
              String getS(String y);
          }
          
          
          Mock<A> a;
          
          
          @Test(groups="Unit")
          public void testArgumentMatcherInChainedMock_working() {
              
              a.returns("ABC").getB(null).getS(null);
              a.returns("ABC").getB(null).getS("");
              a.returns("ABC").getB("").getS(null);
              a.returns("ABC").getB("").getS("");
              a.returns("ABC").getB(notNull(String.class)).getS(null);
              a.returns("ABC").getB(notNull(String.class)).getS("");

          }

          @Test(groups="Unit")
          public void testArgumentMatcherInChainedMock_not_working01() {
              
              a.returns("ABC").getB(notNull(String.class)).getS(isNull(String.class));
          }
          
          
          @Test(groups="Unit")
          public void testArgumentMatcherInChainedMock_not_working02() {
              a.returns("ABC")
                  .getB(notNull(String.class))
                  .getS(isNull(String.class));

          }

      }

      Result (shortened):

      PASSED: testArgumentMatcherInChainedMock_working

      FAILED: testArgumentMatcherInChainedMock_not_working01
      org.unitils.core.UnitilsException: An argument matcher cannot be used outside the context of a match statement.
       at baag.test.unitils.mock.ArgumentMatcherInChainedMockTest.testArgumentMatcherInChainedMock_not_working01(baag.test.unitils.mock.ArgumentMatcherInChainedMockTest:54)

      at org.unitils.mock.argumentmatcher.ArgumentMatcherPositionFinder$MethodInterpreter.throwUnitilsException(ArgumentMatcherPositionFinder.java:438)
      at org.unitils.mock.argumentmatcher.ArgumentMatcherPositionFinder$MethodInterpreter.naryOperation(ArgumentMatcherPositionFinder.java:375)
      at thirdparty.org.objectweb.asm.tree.analysis.Frame.execute(Unknown Source)
      at thirdparty.org.objectweb.asm.tree.analysis.Analyzer.analyze(Unknown Source)
      at org.unitils.mock.argumentmatcher.ArgumentMatcherPositionFinder.findArgumentMatcherIndexes(ArgumentMatcherPositionFinder.java:145)
      at org.unitils.mock.argumentmatcher.ArgumentMatcherPositionFinder.getArgumentMatcherIndexes(ArgumentMatcherPositionFinder.java:90)
      at org.unitils.mock.argumentmatcher.ArgumentMatcherPositionFinder.getArgumentMatcherIndexes(ArgumentMatcherPositionFinder.java:62)
      at org.unitils.mock.core.matching.MatchingInvocationBuilder.createArgumentMatchers(MatchingInvocationBuilder.java:99)
      at org.unitils.mock.core.matching.MatchingInvocationBuilder.handleProxyInvocation(MatchingInvocationBuilder.java:86)
      at org.unitils.mock.core.matching.MatchingInvocationBuilder$InvocationHandler.handleInvocation(MatchingInvocationBuilder.java:124)
      at org.unitils.mock.core.proxy.CglibProxyMethodInterceptor.intercept(CglibProxyMethodInterceptor.java:82)
      at baag.test.unitils.mock.ArgumentMatcherInChainedMockTest$A$$EnhancerByCGLIB$$d57ee44f.getB(<generated>)
      at ArgumentMatcherInChainedMockTest.testArgumentMatcherInChainedMock_not_working01(ArgumentMatcherInChainedMockTest.java:54)
      ...

      FAILED: testArgumentMatcherInChainedMock_not_working02
      org.unitils.core.UnitilsException: Argument matchers cannot be used outside the context of a behavior definition or assert statement
      at org.unitils.mock.argumentmatcher.ArgumentMatcherRepository.registerArgumentMatcher(ArgumentMatcherRepository.java:84)
      at org.unitils.mock.ArgumentMatchers.registerArgumentMatcher(ArgumentMatchers.java:141)
      at org.unitils.mock.ArgumentMatchers.isNull(ArgumentMatchers.java:45)
      at ArgumentMatcherInChainedMockTest.testArgumentMatcherInChainedMock_not_working02(ArgumentMatcherInChainedMockTest.java:62)

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              klaschkaf Florian Klaschka
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: