Dennis van Opstal
Dennis van Opstal

Reputation: 1338

Setting up an unit test with Mockito

I'm trying to set up a simple unit test with mockito for the injects. This project is just a Proof of Concept for a test my friend is making for his project.

The problem I'm having is that I get null from the method I'm calling instead of "Hello World". Also when I debug I get into a class named MethodInterceptorFilter that's calling the method intercept with IndexOutOfBoundsExpetion as one of its arguments.

Does someone know what I'm doing wrong?

DAO:

@Stateless
public interface DAO {
     public String helloWorld();
}

DAO Implementation:

@Stateless
public class DAOImpl implements DAO{

    public String helloWorld() {
        return "Hello World";
    }
}

Service:

@Stateless
public class Service {
    @Inject
    private DAO dao;

    public String helloWorld() {
        return dao.helloWorld();
    }
}

Test:

public class RandomTest {

    @InjectMocks
    Service service = new Service();

    @Mock
    DAO dao;

    @Before
    public void init(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testTest() {
        assertEquals("Hello World", service.helloWorld());
    }
}

By the way, I'm using IntelliJ (not sure if that matters, but saying it anyway).

Upvotes: 1

Views: 323

Answers (2)

LazerBanana
LazerBanana

Reputation: 7211

First, '@InjectMocks' creates an instance of the class and injects the mocks that are annotated with the '@Mock' (or '@Spy') annotations, therefore try to delete the @Before method, or do injects them manually but delete @InjectMocks annotation.

Second, the '@Mock' classes are classes that extend your classes and don't implement any methods (return null), to configure a behavior for the method you need to stub it with

when(...).thenReturn()

or if using BDD

given(...).willReturn()

If you're stubbing the void method try using a'@Spy' and Lastly you need to use the runner '@RunWith(MockitoJUnitRunner.class)' annotation on the Test class if using Mockito as the JUnit test runner (which is the default runner) don't know anything about Mockito.

Upvotes: 2

Mahmad Othman
Mahmad Othman

Reputation: 1

The DAO class is mocked in your Service class, you need to specify the return object to mock.

@Test
public void testTest() {
    when(dao.helloWorld()).thenReturn("Hello World")
    assertEquals("Hello World", service.helloWorld());
}

Or you can use construct injection :

@Stateless
public class Service {

    private DAO dao;

    @Inject
    public Service(DAO dao) {
       this.dao = dao;        
    }

    public String helloWorld() {
        return dao.helloWorld();
    }
}

public class RandomTest {

    Service service;

    @Before
    public void init(){
        service = new Service(new DAOImpl());
    }

    @Test
    public void testTest() {
        assertEquals("Hello World", service.helloWorld());
    }
}

Or reflection, for example in Spring there is a utility class http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/util/ReflectionTestUtils.html

Upvotes: 0

Related Questions