@Stateless
public class ServiceFacade {
@EJB
Service service;
public boolean isOne(){
int nr = service.getNumber();
return (nr == 1);
}
}
I would like to test, whether the ServiceFacade returns "false" in case the number is not 1.
@Stateless
public class Service {
public int getNumber(){
return 1;
}
}
...its hardcoded....
Mockito is just perfect for testing such cases.
import com.abien.business.nointerface.control.Service;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class ServiceFacadeTest {
@Test
public void twoIsFalse(){
Service service = mock(Service.class);
when(service.getNumber()).thenReturn(2);
ServiceFacade facade = new ServiceFacade();
facade.service = service;
assertFalse(facade.isOne());
}
}
You can easily change the return value of every (non-final) class for test purposes. The whole project, with mockito libraries, was pushed into http://kenai.com/projects/javaee-patterns (tested with Netbeans 6.7rc3 and Glassfish v3 Preview).
The whole test is executed in 0.2 seconds on my machine. Four times faster than 0.8 :-). Btw. Glassfish v3 (with EJB 3.1 container) starts in about 5-10 seconds - the whole application is deployed in about 2 seconds...
[In the Real World Patterns - Rethinking Best Practices book several unit-testing strategies are discussed - but not mockito. It will change in the Iteration Two]