Reputation: 1291
In my test when I assert de exception message I'm getting null
I'm not getting mock the message inside Service.... :(
I have:
My test:
@RunWith(MockitoJUnitRunner.class)
public class ServiceImplTest {
@InjectMocks
private ServiceImpl service;
@Mock
private Message message;
public static final String REQUIRED_FIELD = "required.field";
@Before
public void setUp() {
when(message.getMessage(eq(REQUIRED_FIELD), any(List.class))).thenReturn(REQUIRED_FIELD);
System.out.println(message.getMessage(REQUIRED_FIELD, new ArrayList()));
}
@Test(expected = MyException.class)
public void testCancel_shouldValidCancellation_and_ThrowTicketException_with_RequiredFieldMessage() {
try {
Object object = //... new object
service.do(object);
} catch (Exception e) {
assertEquals(REQUIRED_FIELD, e.getMessage()); // <-- e.getMessage return null!!!
}
}
}
My service:
@Service
public class ServiceImpl implements Service {
@Autowired
Message message;
@Override
public Object do(Object object) {
if(object.getSomeAttribute() == null)) {
throw new MyException(message.getMessage("required.field", "attribute"));
}
//doSomething...
return something;
}
}
In the setUp()
of test the printLn()
prints required.field
but I can't use message
of Service
!!
Can someone help me?
Upvotes: 0
Views: 2957
Reputation:
It is hard tell for sure, without knowledge about the interface of Message
, but it is easy to spot that you configure mock object to stub method with signature getMessage(String, List)
:
when(message.getMessage(eq(REQUIRED_FIELD), any(List.class))).thenReturn(REQUIRED_FIELD);
However, ServiceImpl
uses getMessage(String, String)
. The default value which is returned by mock in this case is null
. You have to configure mock object properly.
Upvotes: 1