Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito mock objects returns null

I try to implement some tests for my JSF application and for the mocks I am using mockito. (I also use spring)

@RunWith(MockitoJUnitRunner.class) public class GeneralConfigServiceImplTest  {      private GeneralConfigService generalConfigService;      @Mock     private GeneralConfigDAO generalConfigDAO;      @Mock     private GeneralConfig gen;      @Before     public void setUp() {         MockitoAnnotations.initMocks(this);         generalConfigService = new GeneralConfigService();         ReflectionTestUtils.setField(generalConfigService, "generalConfigDAO", generalConfigDAO);                       }      @Test     public void testAddGeneralConfigCallDAOSuccess() throws DAOException, EntityNullException, IllegalEntityArgumentException, ParseException, EntityPersistException {         gen = createGeneralConfigs("label", "value");          generalConfigService.setInstance(gen);         generalConfigService.persist();         log.info(generalConfigService.getInstance().toString());     } } 

The test succeeds, but when I want to retrieve the instance with the getInstance method. All Parameters which I have set before (via the constructor before) are null. I am new to mocked objects, so is this behavior normal, or is there a mistake in my code?

like image 413
flo8433 Avatar asked May 15 '13 16:05

flo8433


People also ask

Why is mock returning null object?

If a method return type is a custom class, a mock returns null because there is no empty value for a custom class. RETURN_MOCKS will try to return mocks if possible instead of null . Since final class cannot be mocked, null is still returned in that case.

How to return null using Mockito?

thenReturn(null); for (String next = reader. readLine(); next != null; next = reader. readLine()) do something...

Does Mockito any match null?

Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments.

What is the difference between doReturn and thenReturn?

Following are the differences between thenReturn and doReturn : * Type safety : doReturn takes Object parameter, unlike thenReturn . Hence there is no type check in doReturn at compile time. In the case of thenReturn , whenever the type mismatches during runtime, the WrongTypeOfReturnValue exception is raised.


2 Answers

It really depends on GeneralConfigService#getInstance() implementation. Also you can simplify your test code a lot if you use @InjectMocks annotation.

When using MockitoJUnitRunner you don't need to initialize mocks and inject your dependencies manually:

@RunWith(MockitoJUnitRunner.class) public class GeneralConfigServiceImplTest  {      @InjectMocks     private GeneralConfigService generalConfigService;      @Mock     private GeneralConfigDAO generalConfigDAO;      @Test     public void testAddGeneralConfigCallDAOSuccess() {        // generalConfigService is already instantiated and populated with dependencies here        ...     } } 
like image 174
hoaz Avatar answered Sep 18 '22 13:09

hoaz


My problem here was the incorrect import for Test anotation:

Was

import org.junit.jupiter.api.Test;

Correct

import org.junit.Test;

like image 24
FabianoLothor Avatar answered Sep 17 '22 13:09

FabianoLothor