-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JUnit testing using Mockito : Not able to Inject the fluentJDBC mocks #79
Comments
I'd suggest not unit testing the repository, but unit test the domain logic
that uses it - the repository is very easy/natural to mock, its one of the
main benefits of the pattern. The repository itself can be covered by
hooking it to a real - or an embedded database.
However, if you do have strong reasons to unit test the repo code, make
sure you mock all relevant interfaces of FluentJdbc. For instance,
Query.select returns a SelectQuery instance. So your mock Query's select
should return a mock of SelectQuery. You won't hit the real implementation
- or npe that way.
…On Wed, Jul 29, 2020, 6:39 AM Shobith Pejathaya ***@***.***> wrote:
IDE : Intellij 2020.1.2
Java 8
fluentJDBC - 1.8.5
Below is the sample code I have written to mock the fluent JDBC behavior
for any select query
@SpringBootTest <https://github.com/SpringBootTest>
@RunWith <https://github.com/RunWith>(MockitoJUnitRunner.class)
@ContextConfiguration(classes = FluentJDBCConfiguration.class)
public class PatientRepositoryTest {
@Injectmocks
private PatientRepository patientRepository;
@mock
private ObjectMappers objectMappers;
@mock
private DataSource dataSource;
@mock
private Query fluentJDBCQuery;
private List<Patient> patientList;
@test <https://github.com/test>
public void getpatientSuccess() throws ParseException,
PatientNotFoundException {
Mockito.when(fluentJDBCQuery
.select(Mockito.anyString()).listResult(Mockito.any()))
.thenReturn(Collections.singletonList(patientList));
}
Above line always throws error : NULL Pointer exception (as per the
notes,due to conn object NULL in PreparedStatementFactory.createSingle
method
Even changing to something like below where a valid object mapper is
passed :
Mockito.when(fluentJDBCQuery .select(Mockito.anyString()).listResult(patientMapper))
.thenReturn(tasyPatients);
Does result in the same thing.
When i manually set the connection object along with proper query (which i
should not be ideally) it still fails in the mockito library
Can you please let me know if there is any predefined way how the
fluentJDBC should be injected or change in behavior when integrated with
Mockito framework .
Thanks
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#79>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAPPNROXRBVPX7XG54QR2Q3R56RYDANCNFSM4PLGC3LQ>
.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IDE : Intellij 2020.1.2
Java 8
fluentJDBC - 1.8.5
Below is the sample code I have written to mock the fluent JDBC behavior for any select query
@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = FluentJDBCConfiguration.class)
public class PatientRepositoryTest {
@test
public void getpatientSuccess() throws ParseException, PatientNotFoundException {
Mockito.when(fluentJDBCQuery .select(Mockito.anyString()).listResult(Mockito.any()))
.thenReturn(Collections.singletonList(patientList));
}
Above line always throws error : NULL Pointer exception (as per the notes,due to conn object NULL in PreparedStatementFactory.createSingle method
Even changing to something like below where a valid object mapper is passed :
Does result in the same thing.
When i manually set the connection object along with proper query (which i should not be ideally) it still fails in the mockito library
Can you please let me know if there is any predefined way how the fluentJDBC should be injected or change in behavior when integrated with Mockito framework .
Thanks
The text was updated successfully, but these errors were encountered: