Class MockitoMockBuilder


  • public class MockitoMockBuilder
    extends java.lang.Object
    Utilities for Building string constructs of Mockito mock expressions. Date: 31/03/2017
    Author:
    Yaron Yamin
    • Constructor Summary

      Constructors 
      Constructor Description
      MockitoMockBuilder​(boolean isMockitoMockMakerInlineOn, boolean stubMockMethodCallsReturnValues, TestSubjectInspector testSubjectInspector, java.lang.String mockitoCoreVersion)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String buildMockArgsMatchers​(java.util.List<Param> params, java.lang.String language)
      constructs mocked arguments expression
      java.lang.String getImmockabiliyReason​(java.lang.String prefix, Field field)
      constructs an error message explaining why field cannot be mocked
      java.lang.String getInitMocksMethod()  
      java.lang.String getMockitoCoreVersion()
      Mockito core version.
      boolean hasMockable​(java.util.List<Field> fields)
      true - if any given field can be mocked
      boolean hasMocks​(Method ctor, java.util.Map<java.lang.String,​java.lang.String> defaultTypes)
      true - if method had any argument that can be mocked given the provided default types
      boolean isMockable​(Field field)
      true - field can be mocked
      boolean isMockable​(Param param, java.util.Map<java.lang.String,​java.lang.String> defaultTypes)
      true - if argument can be mocked given the provided default types
      boolean isMockExpected​(Field field)  
      boolean shouldStub​(Method testMethod, Method ctor, java.util.Map<java.lang.String,​java.lang.String> defaultTypes)
      true - if tested method should be stubbed
      boolean shouldStub​(Method testMethod, java.util.List<Field> testedClassFields)
      true - if should stub tested method
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MockitoMockBuilder

        public MockitoMockBuilder​(boolean isMockitoMockMakerInlineOn,
                                  boolean stubMockMethodCallsReturnValues,
                                  TestSubjectInspector testSubjectInspector,
                                  @Nullable
                                  java.lang.String mockitoCoreVersion)
    • Method Detail

      • isMockable

        public boolean isMockable​(Field field)
        true - field can be mocked
      • isMockable

        public boolean isMockable​(Param param,
                                  java.util.Map<java.lang.String,​java.lang.String> defaultTypes)
        true - if argument can be mocked given the provided default types
      • hasMockable

        public boolean hasMockable​(java.util.List<Field> fields)
        true - if any given field can be mocked
      • hasMocks

        public boolean hasMocks​(Method ctor,
                                java.util.Map<java.lang.String,​java.lang.String> defaultTypes)
        true - if method had any argument that can be mocked given the provided default types
      • getImmockabiliyReason

        public java.lang.String getImmockabiliyReason​(java.lang.String prefix,
                                                      Field field)
        constructs an error message explaining why field cannot be mocked
        Parameters:
        prefix - add prefix to message
        field - reported field
        Returns:
        an error message explaining why field cannot be mocked
      • buildMockArgsMatchers

        public java.lang.String buildMockArgsMatchers​(java.util.List<Param> params,
                                                      java.lang.String language)
        constructs mocked arguments expression
        Parameters:
        params - method params being mocked
        language - String representation of test code Language
        Returns:
        mocked arguments expression
        See Also:
        Language
      • shouldStub

        public boolean shouldStub​(Method testMethod,
                                  java.util.List<Field> testedClassFields)
        true - if should stub tested method
        Parameters:
        testMethod - method being tested
        testedClassFields - fields of owner type being tested
      • shouldStub

        public boolean shouldStub​(Method testMethod,
                                  Method ctor,
                                  java.util.Map<java.lang.String,​java.lang.String> defaultTypes)
        true - if tested method should be stubbed
        Parameters:
        testMethod - - method being tested
        ctor - - constructor of method return type
        defaultTypes - - default type values
      • isMockExpected

        public boolean isMockExpected​(Field field)
        Returns:
        true - if Field should be mocked
      • getMockitoCoreVersion

        @Nullable
        public java.lang.String getMockitoCoreVersion()
        Mockito core version. in case mockito-core jar can be found on the target test module classpath. null otherwise
      • getInitMocksMethod

        public java.lang.String getInitMocksMethod()
        Returns:
        Mockito init mocks method name. typically "initMocks" or "openMocks" depending on Mockito version