The Arquillian Mockito provides a simple way to bring Mockito into the Arquillian framework.
The following example illustrates how Arquillian Mockito can be used:
@RunWith(Arquillian.class)
public class ServiceTest {
/**
* Creates a testing WAR application
*
* @return WebArchive to be tested
*/
@Deployment
public static WebArchive createDeployment() {
return Deployments.createDeployment();
}
//Inject the spyed resource
@Inject @Spy
private SomeInjected spyedResource;
@Before
public void beforeTest(){
reset(spyedResource);
}
@Test
public void testMethod() {
when(spyedResource.executeMethod()).thenReturn(someObject);
//execute real method
}
}
You need to put following configuration to the pom.xml file of your project:
<dependencies>
<!-- To use Arquillian Mockito -->
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-mockito</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
Note
|
You need to take care the CDI bean scope. Make sure the bean injected in the test is the same one used in the application. |