title: Arquillian with Mockito and CDI
body: <p>Is it possible to create spy(mock) object in testing class?</p>
  
  <p>Here is tested class.</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@Stateless
  @Slf4j
  public class UserDao {
  
      @Inject
      private TestBean testBean;
  
      public String mock() {
          return testBean.mock();
      }
  
      public String notMock() {
          return testBean.notMock();
      }
  }
  </code></pre>
  
  <p>TestBean code </p>
  
  <pre class="lang-java prettyprint-override"><code>@Stateless
  @Slf4j
  public class TestBean {
      public String notMock() {
          return "NOT MOCK";
      }
  
      public String mock() {
          return "IMPLEMENTED MOCK";
      }
  }
  </code></pre>
  
  <p>Here's my test</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(Arquillian.class)
  public class UserDataTest {
  
      @Rule
      public ExpectedException thrown = ExpectedException.none();
  
      @Inject
      private UserDao userDao;
  
      @Deployment
      protected static Archive createWar() {
  
          File[] dependencies = Maven.configureResolver()
                  .withRemoteRepo("nexus-remote", "http://maven.wideup.net/nexus/content/groups/public/", "default")
                  .withRemoteRepo("nexus-release", "http://maven.wideup.net/nexus/content/repositories/releases/", "default")
                  .resolve(
                          "org.slf4j:slf4j-simple:1.7.7",
                          "eu.bitwalker:UserAgentUtils:1.15",
                          "org.mockito:mockito-all:1.10.8"
                  ).withoutTransitivity().asFile();
  
          return ShrinkWrap
                  .create(WebArchive.class, "pass.jpa.war")
                  .addAsWebInfResource("jbossas-ds.xml")
                  .addAsWebInfResource("jboss-deployment-structure.xml")
                  .addAsLibraries(
                          PassApiDeployments.createDefaultDeployment(),
                          PassUtilLibrary.createDefaultDeployment(),
                          PassJpaDeployments.createDefaultDeployment()
                  ).addAsLibraries(dependencies);
      }
  
      @Test
      public void testMock() {
          assertEquals("MOCK", userDao.mock());
      }
  
      @Test
      public void testNotMock() {
          assertEquals("NOT MOCK", userDao.notMock());
      }
  }
  </code></pre>
  
  <p>I'd like to create a spy object on TestBean to change result on method test() of this bean.</p>
  
  <p>So is it possible to create TestBean spy in UserDao.</p>
  
  <p>I solve some problems through producer like this. </p>
  
  <pre class="lang-java prettyprint-override"><code>@Singleton
  public class MockFactory {
      @Produces
      @ArquillianAlternative
      public TestBean getTestBean() {
          return when(mock(TestBean.class).mock()).thenReturn("MOCK").getMock();
      }
  }
  </code></pre>
  
  <p>But in this example I need create on Bean completely on my own. And if it is bean with additional dependencies and thus i will manage all dependencies.</p>