How to unit test Kotlin suspending functions

I’ve found out that it’s because of a CoroutineDispatcher. I used to mock UI context with EmptyCoroutineContext. Switching to Unconfined has solved the problem Update 02.04.20 The name of the question suggests that there’ll be an exhaustive explanation how to unit test a suspending function. So let me explain a bit more. The main problem … Read more

How to make Mockito throw an exception when a mock is called with non-defined parameters?

You could provide a default Answer in the construction of your mock that always throws an exception. Then every call that is stubbed will act like usual. Everything outside those paths will throw an exception. Something like this: final String arg = “some arg”; Collection<Object> object = mock(Collection.class, new Answer<Object>() { @Override public Object answer(InvocationOnMock … Read more

How to test DAO methods using Mockito?

Here is a good start using Mockito to test your UserDAO. This code uses a good amount of the Mockito features, so you can see how to use them. Let me know if you have questions. import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import … Read more

Mockito isA(Class clazz) How to resolve type safety?

Mockito/Hamcrest and generic classes Yes, this is a general problem with Mockito/Hamcrest. Generally using isA() with generic classes produces a warning. There are predifined Mockito matchers for the most common generic classes: anyList(), anyMap(), anySet() and anyCollection(). Suggestions: anyIterable() in Mockito 2.1.0 Mockito 2.1.0 added a new anyIterable() method for matching Iterables: when(client.runTask(anyString(), anyString(), anyIterable()).thenReturn(…) … Read more

Mockito when method not working

Mockito mock works when we mock the objects loosely. Here is the change i have made to make it work: when(controlWfDefTypeService.getDqCntlWfDefnTypCd(any(DqCntlWfDefn.class)) .thenReturn(dqCntlWfDefnTyp); Instead of passing the object of the Mock class, I passed the class with the Matcher any() and it works.

Mocking an interface with Mockito

@InjectMocks will not instantiate or mock your class. This annotation is used for injecting mocks into this field. If you want to test serviceImpl you will need to mock in this way: @Mock private OrderIF order; @InjectMocks private Service reqService = new ServiceImpl(); To make it work you either need to use runner or MockitoAnnotations.initMocks(this); … Read more

After migrating flutter code to null-safety, mock objects not accepting `any`

When assigning the Mock object, it needs to be of the Mock object type, not the BaseClass. @GenerateMocks(MockSpec<ITransactionRepository>(as: #MockTransactionRepository), ) void main() { …. ITransactionRepository baseObject = MockTransactionRepository(); // wrong MockTransactionRepository mockObject = MockTransactionRepository(); // right when(baseObject.method(any)); // results in compile error when(mockObject.method(any)); // OK … } Source: https://github.com/dart-lang/mockito/issues/364

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)