title: mockit.internal.MissingInvocation Exception - using Spring , Jmockit
body: <p>I am using JMockit with Spring and trying to verify some method from my service layer but i couldn't make it successful. </p>
  
  <p>Find the below sample code </p>
  
  <p>UserRepositoryImpl.java</p>
  
  <pre><code>package com.test.jmockit.JMockitsample.repository.impl;
  
  import org.springframework.stereotype.Repository;
  
  import com.test.jmockit.JMockitsample.User;
  import com.test.jmockit.JMockitsample.repository.UserRepository;
  
  @Repository("userRepository")
  public class UserRepositoryImpl implements UserRepository{
  
  
  
      /**
       * Not yet implemented. 
       * 
       * @param User
       * @return
       */
      public User getLoggedInUser() {
  
          User user =  new User();
          user.setUsername("admin");
          user.setPassword("test123");
          return user;
      }
  
  }
  </code></pre>
  
  <p>UserServiceImpl.java</p>
  
  <pre><code>/**
   * 
   */
  package com.test.jmockit.JMockitsample.service.impl;
  
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.test.jmockit.JMockitsample.User;
  import com.test.jmockit.JMockitsample.repository.UserRepository;
  import com.test.jmockit.JMockitsample.service.UserService;
  
  
      @Service("userService")
      public class UserServiceImpl implements UserService{
  
  
          @Autowired
          private UserRepository userRepository;
  
  
          public User getLoggedInUser() {
              // TODO Auto-generated method stub
  
              User user = userRepository.getLoggedInUser();
              System.out.println("user service " + user.getUsername());
              return user;
          }
  
      }
  </code></pre>
  
  <p>And my Test class</p>
  
  <pre><code>@ContextConfiguration(locations = { "classpath:/ApplicationContext.xml" })
  @RunWith(SpringJUnit4ClassRunner.class)
  public class JMockitVerificationTest {
  
      @Tested
      private UserService userService = new UserServiceImpl();
  
      @Injectable
      private UserRepository userRepository;
  
  
      @Test
      public void testUserServiceUserList() {
          new Expectations() {
              {
                  userRepository.getLoggedInUser();
                  result = new User("guest", "test123");
              }
          };
  
          userService.getLoggedInUser();
  
          new Verifications() {
  
              {
                  userRepository.getLoggedInUser();
              }
          };
  
      }
  }
  </code></pre>
  
  <p>It's running success if i remove the verification block and getting expected result.</p>
  
  <p>Trying to verify my method whether it is get invoked in service or not but getting below exception.</p>
  
  <pre><code>mockit.internal.MissingInvocation: Missing invocation of:
  com.test.jmockit.JMockitsample.repository.UserRepository#getLoggedInUser()
     on mock instance: com.test.jmockit.JMockitsample.repository.$Impl_UserRepository@425407
          at com.test.jmockit.JMockitVerificationTest$2.&lt;init&gt;(JMockitVerificationTest.java:45)
          at com.test.jmockit.JMockitVerificationTest.testUserServiceUserList(JMockitVerificationTest.java:42)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
          at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
          at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
          at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
          at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
          at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:242)
          at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:137)
          at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
          at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
          at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
          at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
          at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
  Caused by: Missing invocation
          ... 24 more
  </code></pre>
  
  <p>Don't know where i have done the mistake to make it success, thanks in advance.</p>