Last active
April 19, 2021 02:31
-
-
Save sebhoss/fb41b560672ce3cdd341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DSLContext database = Mockito.mock(DSLContext.class, Mockito.RETURNS_DEEP_STUBS); | |
Mockito.when(database.select(ACTIVITY_TYPE.ID) | |
.from(ACTIVITY_TYPE) | |
.where(ACTIVITY_TYPE.SINGLE_PERSON_USABLE.isTrue()) | |
.fetch(ACTIVITY_TYPE.ID)) | |
.thenReturn(ImmutableList.of(singleActivityTypeId)); | |
Mockito.when(database.select(ACTIVITY_TYPE.ID) | |
.from(ACTIVITY_TYPE) | |
.where(ACTIVITY_TYPE.MULTI_PERSON_USABLE.isTrue()) | |
.fetch(ACTIVITY_TYPE.ID)) | |
.thenReturn(ImmutableList.of(multiActivityTypeId)); |
With some int
s:
package com.company.application;
import org.jooq.DSLContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import static com.company.model.db.production.Tables.ACTIVITY_TYPE_RULE;
public class MockTest {
private DSLContext database;
@Before
public void mockDslContext() {
database = Mockito.mock(DSLContext.class, Mockito.RETURNS_DEEP_STUBS);
}
/**
* This test case both returns an integer result and uses an integer parameter during query constructor.
*/
@Test
public void shouldWorkWithIntegerResult() {
// given
final Integer expectedResult = Integer.valueOf(12);
BDDMockito.given(database
.select(ACTIVITY_TYPE_RULE.MAX)
.from(ACTIVITY_TYPE_RULE)
.where(ACTIVITY_TYPE_RULE.MIN.equal(5))
.fetchOne(ACTIVITY_TYPE_RULE.MAX)).willReturn(expectedResult);
// when
final Integer actualResult = database
.select(ACTIVITY_TYPE_RULE.MAX)
.from(ACTIVITY_TYPE_RULE)
.where(ACTIVITY_TYPE_RULE.MIN.equal(5))
.fetchOne(ACTIVITY_TYPE_RULE.MAX);
// then
Assert.assertEquals(expectedResult, actualResult);
}
/**
* Checks whether or not int/Integer based parameters can be used. This test case will throw:
*
* java.lang.ClassCastException:
* org.mockito.internal.creation.jmock.ClassImposterizer$ClassWithSuperclassToWorkAroundCglibBug$$EnhancerByMockitoWithCGLIB$$2c431a1c
* cannot be cast to java.lang.Integer
*
* The reason is that the mocked query expects '5' as input, but the actual input was '3'.
*/
@Test
public void shouldWorkWithIntegerParameter() {
// given
final Integer expectedResult = Integer.valueOf(12);
BDDMockito.given(database
.select(ACTIVITY_TYPE_RULE.MAX)
.from(ACTIVITY_TYPE_RULE)
.where(ACTIVITY_TYPE_RULE.MIN.equal(5))
.fetchOne(ACTIVITY_TYPE_RULE.MAX)).willReturn(expectedResult);
// when
final Integer actualResult = database
.select(ACTIVITY_TYPE_RULE.MAX)
.from(ACTIVITY_TYPE_RULE)
.where(ACTIVITY_TYPE_RULE.MIN.equal(3))
.fetchOne(ACTIVITY_TYPE_RULE.MAX);
// then
Assert.assertEquals(expectedResult, actualResult);
}
/**
* Compared to the previous test case, this one uses <code>org.mockito.Matchers#anyInt()</code> instead of
* specifying a specific integer to match against.
*/
@Test
public void shouldWorkWithAnyIntMatcher() {
// given
final Integer expectedResult = Integer.valueOf(12);
BDDMockito.given(database
.select(ACTIVITY_TYPE_RULE.MAX)
.from(ACTIVITY_TYPE_RULE)
.where(ACTIVITY_TYPE_RULE.MIN.equal(org.mockito.Matchers.anyInt()))
.fetchOne(ACTIVITY_TYPE_RULE.MAX)).willReturn(expectedResult);
// when
final Integer actualResult = database
.select(ACTIVITY_TYPE_RULE.MAX)
.from(ACTIVITY_TYPE_RULE)
.where(ACTIVITY_TYPE_RULE.MIN.equal(3))
.fetchOne(ACTIVITY_TYPE_RULE.MAX);
// then
Assert.assertEquals(expectedResult, actualResult);
}
}
table is defined as:
Table "production.activity_type_rule"
Column | Type | Modifiers
------------------+---------+-----------
activity_type_id | uuid | not null
valid_from | date |
valid_to | date |
min | integer |
max | integer |
I like the programmatic deep stubbing:
DSLContext database = Mockito.mock(DSLContext.class, Mockito.RETURNS_DEEP_STUBS);
I like to keep annotations to a minimum. Otherwise you have no idea what your annotation-heavy code does (which is the same reason I hate Spring).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or without the
List
wrapper: