jmockit / jmockit1

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

Why follow tutorial can't pass? #274

Closed samuelliang closed 8 years ago

samuelliang commented 8 years ago

OS : Windows 7 x64 JDK : 1.7.80 x64 jmockit.version : 1.22 maven.version : 3.3.9

I follow tutorial happen below two problem:

  1. In test code @Mock int doSomething(), IDE highlight "Mocked real method not found in type"
  2. When I modified new MockUp() to new MockUp() above problem was solved, but run this test case always failure. this Service#doSomething() method not redirect to mock method.
    • Sample Codes:
public interface Service { int doSomething(); }
final class ServiceImpl implements Service { public int doSomething() { return 1; } }

public final class TestedUnit
{
   private final Service service1 = new ServiceImpl();
   private final Service service2 = new Service() { public int doSomething() { return 2; } };

   public int businessOperation()
   {
      return service1.doSomething() + service2.doSomething();
   }
}
import static org.junit.Assert.*;
import org.junit.Test;
import mockit.Mock;
import mockit.MockUp;

public class FakingUnitTest {
    @Test
    public <T extends Service> void mockingImplementationClassesFromAGivenBaseType() {

        new MockUp<T>() {
            @Mock
            public void $clinit() {}
            @Mock
            int doSomething() {
                return 7;
            }
        };

        int result = new TestedUnit().businessOperation();

        assertEquals(14, result);
    }
}
rliesenfeld commented 8 years ago

I executed the code above with the same versions of JMockit and the JDK (on Windows 7 32bit), but I see no problem. The test always passes, either from my IDE or from Maven.

samuelliang commented 8 years ago

Oh, Oh. Eclipse IDE confuse me. When I remove JMockit Plugin from Eclipse. This "Mocked real method not found in type" message disappear. And, run pass. Anyway, Thanks rliesenfeld. :)

samuelliang commented 8 years ago

Hi, rliesenfeld. I am sure this message that was caused from JMockit Plugin. Becasue when I reinstall this plugin, this message appear again. May you response this problem to JMockit Plugin Developer?