Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-14050

ReflectionTestUtils should automatically unwrap proxies

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.2.5
    • Fix Version/s: 4.3 RC1
    • Component/s: Test
    • Labels:

      Description

      When using Spring AOP and in a test case you use ReflectionTestUtils.setField, this fails when using (class-based) proxies. Specifically, the value is set in the proxy itself rather than in the underlying instance.

      Currently one would first have to call AopTestUtils.getUltimateTargetObject and then pass that result to the ReflectionTestUtils.setField method.

      Object actualTarget = AopTestUtils.getUltimateTargetObject(injectedProxy);
      ReflectionTestUtils.setField(actualTarget, "field", "new-value");
      

      Arguably calling ReflectionTestUtils.setField on a proxied class isn't the best practice, but it would be nice if the call to AopTestUtils.getUltimateTargetObject could be embedded in the ReflectionTestUtils class.

        Attachments

          Activity

            People

            Assignee:
            sbrannen Sam Brannen
            Reporter:
            mdeinum Marten Deinum
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 23 weeks, 6 days ago