Class MockByReflectionRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class MockByReflectionRule
    extends Object
    implements org.junit.rules.TestRule
    This rule handles field injections.
    The mechanism is very simple:
    • saving the previous value
    • setting the given value
    • at the end of the test, the old value is set again
    This prevent from getting an unstable context of tests in case of chained execution.
    Author:
    Yohann Chastagnier
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base, org.junit.runner.Description description)  
      <T> T mockField​(Object instanceOrClass, Class<T> classToMock, String fieldNames)
      Mocks a field specified by the given field name of the given instance with a new mock instance of the specified class.
      <T> T setField​(Object instanceOrClass, T value, String fieldNames)
      Sets a field specified by the given field name of the given instance with a given value of the specified class.
      <T> T spyField​(Object instanceOrClass, Class<T> classToMock, String fieldNames)
      Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.
      <T> T spyField​(Object instanceOrClass, T value, String fieldNames)
      Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.
      protected void unsetMockedFields()  
    • Constructor Detail

      • MockByReflectionRule

        public MockByReflectionRule()
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base,
                                                       org.junit.runner.Description description)
        Specified by:
        apply in interface org.junit.rules.TestRule
      • unsetMockedFields

        protected void unsetMockedFields()
                                  throws Exception
        Throws:
        Exception
      • mockField

        public <T> T mockField​(Object instanceOrClass,
                               Class<T> classToMock,
                               String fieldNames)
        Mocks a field specified by the given field name of the given instance with a new mock instance of the specified class.
        Type Parameters:
        T - the type of the mocked instance.
        Parameters:
        instanceOrClass - the instance or class into which the field will be mocked.
        classToMock - the class to get a new mock instance.
        fieldNames - the aimed field name. If several, then it represents a path to access to the field. If the fieldName path part starts with '.' character, it sets that the field is static.
        Returns:
        the new mocked instance.
      • spyField

        public <T> T spyField​(Object instanceOrClass,
                              Class<T> classToMock,
                              String fieldNames)
        Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.
        Type Parameters:
        T - the type of the mocked instance.
        Parameters:
        instanceOrClass - the instance or class into which the field will be mocked.
        classToMock - the class to get a new mock instance.
        fieldNames - the aimed field name. If several, then it represents a path to access to the field. If the fieldName path part starts with '.' character, it sets that the field is static.
        Returns:
        the new mocked instance.
      • spyField

        public <T> T spyField​(Object instanceOrClass,
                              T value,
                              String fieldNames)
        Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.
        Type Parameters:
        T - the type of the mocked instance.
        Parameters:
        instanceOrClass - the instance or class into which the field will be mocked.
        value - the value to spy.
        fieldNames - the aimed field name. If several, then it represents a path to access to the field. If the fieldName path part starts with '.' character, it sets that the field is static.
        Returns:
        the new mocked instance.
      • setField

        public <T> T setField​(Object instanceOrClass,
                              T value,
                              String fieldNames)
        Sets a field specified by the given field name of the given instance with a given value of the specified class.
        Type Parameters:
        T - the type of the mocked instance.
        Parameters:
        instanceOrClass - the instance or class into which the field will be mocked.
        value - the value to set.
        fieldNames - the aimed field name. If several, then it represents a path to access to the field. If the fieldName path part starts with '.' character, it sets that the field is static.
        Returns:
        the new mocked instance.