lundi 11 juillet 2016

Mocked method returns null insted of expected

I would like to use mockito for testing EJB. The get method returns with the result of a database query and the transformedGet does the bussiness logic in the session bean.

If the mockFoo.get() is called directly, it works as expected. But when the mockFoo.transformedGet() calls the get() method it returns with null, instead of the expected value.

I would like to test the bussiness logic, and isolate the database query. Is it possible this with mockito?

Class to test

public class Foo {
    public String get() {
         return "get";
    }

    public String transformedGet() {
        return "transformed" + get();
    }
}

The test:

@Test
public void mockitoTest() {
    Foo mockFoo = Mockito.mock(Foo.class);
    String expected = "test";

    Mockito.when(mockFoo.get()).thenReturn(expected);

    String actual = mockFoo.get();
    Assert.assertEquals(actual, expected);
    String transformed = mockFoo.transformedGet();
    Assert.assertEquals(transformed, "transformed" + expected);
}

Aucun commentaire:

Enregistrer un commentaire