jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
458 stars 238 forks source link

Overridden methods from base class call methods from fake subclass #705

Open jmf-tls opened 3 years ago

jmf-tls commented 3 years ago

Consider this test scenario

   public static class BaseClass {
      protected int doSomething() { return 1; }
   }

   public static class Subclass extends BaseClass {
      @Override protected int doSomething() { return super.doSomething() + 1; }
   }

   public static final class FakeForSubclass extends MockUp<Subclass> {
      @Mock public int doSomething(Invocation inv) { return inv.proceed(); }
   }

When a test uses FakeForSubclass and creates an instance of Subclass, calling subclass.doSomething() results in a StackOverflowError.
Initially the invocation proceeds to run the code on Subclass, which calls the method on the base class that it overrides.
This call is intercepted by FakeForSubclass, which calls the code in Subclass again, resulting in a loop that ends in the StackOverflowError. The code in BaseClassshould be called instead.

I created a pull request that adds a validation for this issue on the FakedClassWithSuperClassTest: #704

jmf-tls commented 3 years ago

Probably related with #671