Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8221538

Mock of Calendar.getInstance() with lambda cause StackOverfowError

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10, jdk1.8.0_66

      A DESCRIPTION OF THE PROBLEM :
      There is a different behavior with mocking of Calendar.getInstance() using anonymous class and lambda expression. For example, if PowerMockito is used and you want to write the code using an anonymous class it will looks like:
      PowerMockito.mockStatic(Calendar.class);
      when(Calendar.getInstance()).thenAnswer(new Answer<Calendar>() {

         public Calendar answer(InvocationOnMock invocation) throws Throwable {
            Calendar realCalendar = Calendar.getInstance();
            realCalendar.setTime(currentDateTime); // Set some time
            return realCalendar;
         }
      });

      But if you will rewrite it with lambda:
      PowerMockito.mockStatic(Calendar.class);
      when(Calendar.getInstance()).thenAnswer((Answer<Calendar>) invocation -> {
         Calendar realCalendar = Calendar.getInstance();
         realCalendar.setTime(currentDateTime);
         return realCalendar;
      });
      It will cause the StackOverflowError. I am not sure that it is a bug, but I believe that both variants of code should work the same way.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See the description

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both methods should work the same way
      ACTUAL -
      StackOverflowError using lambda expression

      ---------- BEGIN SOURCE ----------
      See the description
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      To avoid error you need to use the anonymous class

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal (Inactive)
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: