Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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));
@sebhoss

This comment has been minimized.

Copy link
Owner Author

@sebhoss sebhoss commented Aug 11, 2015

Or used within a complete example:

import com.google.common.collect.ImmutableList;
import com.company.shared.domain.model.id.aggregate.ActivityTypeId;
import org.hamcrest.Matchers;
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 java.util.List;

import static com.company.model.db.production.Tables.ACTIVITY_TYPE;

public class MockingTest {

    private DSLContext database;

    @Before
    public void mockDslContext() {
        database = Mockito.mock(DSLContext.class, Mockito.RETURNS_DEEP_STUBS);
    }

    @Test
    public void shouldMockDeeply() {
        // given
        final ActivityTypeId expectedTypeId = new ActivityTypeId();
        BDDMockito.given(database
                .select(ACTIVITY_TYPE.ID)
                .from(ACTIVITY_TYPE)
                .where(ACTIVITY_TYPE.SINGLE_PERSON_USABLE.isTrue())
                .fetch(ACTIVITY_TYPE.ID)).willReturn(ImmutableList.of(expectedTypeId));

        // when
        final List<ActivityTypeId> fetchedTypeIds = database
                .select(ACTIVITY_TYPE.ID)
                .from(ACTIVITY_TYPE)
                .where(ACTIVITY_TYPE.SINGLE_PERSON_USABLE.isTrue())
                .fetch(ACTIVITY_TYPE.ID);

        // then
        Assert.assertThat(fetchedTypeIds, Matchers.contains(expectedTypeId));
    }

}
@sebhoss

This comment has been minimized.

Copy link
Owner Author

@sebhoss sebhoss commented Aug 11, 2015

Or without the List wrapper:

import com.company.shared.domain.model.id.aggregate.ActivityTypeId;
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;

public class MockingTest {

    private DSLContext database;

    @Before
    public void mockDslContext() {
        database = Mockito.mock(DSLContext.class, Mockito.RETURNS_DEEP_STUBS);
    }

    @Test
    public void shouldMockDeeply() {
        // given
        final ActivityTypeId expectedTypeId = new ActivityTypeId();
        BDDMockito.given(database
                .select(ACTIVITY_TYPE.ID)
                .from(ACTIVITY_TYPE)
                .where(ACTIVITY_TYPE.SINGLE_PERSON_USABLE.isTrue())
                .fetchOne(ACTIVITY_TYPE.ID)).willReturn(expectedTypeId);

        // when
        final ActivityTypeId fetchedTypeId = database
                .select(ACTIVITY_TYPE.ID)
                .from(ACTIVITY_TYPE)
                .where(ACTIVITY_TYPE.SINGLE_PERSON_USABLE.isTrue())
                .fetchOne(ACTIVITY_TYPE.ID);

        // then
        Assert.assertEquals(expectedTypeId, fetchedTypeId);
    }

}
@sebhoss

This comment has been minimized.

Copy link
Owner Author

@sebhoss sebhoss commented Aug 12, 2015

With some ints:

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 |
@sarnobat

This comment has been minimized.

Copy link

@sarnobat sarnobat commented Sep 6, 2018

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