Skip to content
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

Migration to mockito #289

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ dependencies {
implementation "org.slf4j:slf4j-api:$slf4jVersion"

testImplementation "junit:junit:4.13.2"
testImplementation "org.easymock:easymock:5.2.0"
testImplementation "org.powermock:powermock-module-junit4:2.0.9"
testImplementation "org.powermock:powermock-api-easymock:2.0.9"
testImplementation "org.mockito:mockito-core:$mockitoVersion"
testImplementation "org.apache.kafka:connect-api:$kafkaVersion"
testImplementation "commons-io:commons-io:2.14.0"
Expand All @@ -179,6 +176,7 @@ dependencies {
testImplementation "org.apache.derby:derbyshared:$derbyVersion"
testImplementation "org.apache.derby:derbytools:$derbyVersion"
testImplementation 'org.assertj:assertj-core:3.25.2'
testImplementation 'org.awaitility:awaitility:4.2.0'

testRuntimeOnly "org.slf4j:slf4j-log4j12:$slf4jVersion"

Expand Down
53 changes: 16 additions & 37 deletions src/test/java/io/aiven/connect/jdbc/JdbcSourceConnectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.kafka.connect.errors.ConnectException;

import io.aiven.connect.jdbc.config.JdbcConfig;
import io.aiven.connect.jdbc.dialect.DatabaseDialect;
import io.aiven.connect.jdbc.source.EmbeddedDerby;
import io.aiven.connect.jdbc.source.JdbcSourceConnectorConfig;
import io.aiven.connect.jdbc.source.JdbcSourceTask;
Expand All @@ -37,22 +36,19 @@
import io.aiven.connect.jdbc.util.ExpressionBuilder;
import io.aiven.connect.jdbc.util.TableId;

import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.MockedConstruction;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({JdbcSourceConnector.class, DatabaseDialect.class})
@PowerMockIgnore("javax.management.*")
public class JdbcSourceConnectorTest {

private JdbcSourceConnector connector;
Expand Down Expand Up @@ -101,33 +97,16 @@ public void testStartConnectionFailure() {

@Test
public void testStartStop() throws Exception {
final CachedConnectionProvider mockCachedConnectionProvider =
PowerMock.createMock(CachedConnectionProvider.class);
PowerMock.expectNew(
CachedConnectionProvider.class,
EasyMock.anyObject(DatabaseDialect.class),
EasyMock.eq(JdbcSourceConnectorConfig.CONNECTION_ATTEMPTS_DEFAULT),
EasyMock.eq(JdbcSourceConnectorConfig.CONNECTION_BACKOFF_DEFAULT))
.andReturn(mockCachedConnectionProvider);

// Should request a connection, then should close it on stop(). The background thread may also
// request connections any time it performs updates.
final Connection conn = PowerMock.createMock(Connection.class);
EasyMock.expect(mockCachedConnectionProvider.getConnection()).andReturn(conn).anyTimes();

// Since we're just testing start/stop, we don't worry about the value here but need to stub
// something since the background thread will be started and try to lookup metadata.
EasyMock.expect(conn.getMetaData()).andStubThrow(new SQLException());
// Close will be invoked both for the SQLExeption and when the connector is stopped
mockCachedConnectionProvider.close();
PowerMock.expectLastCall().atLeastOnce();

PowerMock.replayAll();

connector.start(connProps);
connector.stop();

PowerMock.verifyAll();
final Connection conn = mock(Connection.class);
when(conn.getMetaData()).thenThrow(new SQLException());
try (MockedConstruction<CachedConnectionProvider> mockCachedConnectionProvider =
mockConstruction(CachedConnectionProvider.class,
(mock, context) -> when(mock.getConnection()).thenReturn(conn))) {

connector.start(connProps);
connector.stop();
verify(mockCachedConnectionProvider.constructed().get(0), atLeastOnce()).close();
}
}

@Test
Expand Down
27 changes: 11 additions & 16 deletions src/test/java/io/aiven/connect/jdbc/sink/JdbcSinkTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@
import io.aiven.connect.jdbc.config.JdbcConfig;
import io.aiven.connect.jdbc.util.DateTimeUtils;

import org.easymock.EasyMockSupport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import static org.easymock.EasyMock.expectLastCall;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class JdbcSinkTaskTest extends EasyMockSupport {
public class JdbcSinkTaskTest {
private final SqliteHelper sqliteHelper = new SqliteHelper(getClass().getSimpleName());

private static final Schema SCHEMA = SchemaBuilder.struct().name("com.example.Person")
Expand Down Expand Up @@ -209,17 +211,10 @@ public void retries() throws SQLException {
final int retryBackoffMs = 1000;

final Set<SinkRecord> records = Collections.singleton(new SinkRecord("stub", 0, null, null, null, null, 0));
final JdbcDbWriter mockWriter = createMock(JdbcDbWriter.class);
final SinkTaskContext ctx = createMock(SinkTaskContext.class);
final JdbcDbWriter mockWriter = mock(JdbcDbWriter.class);
final SinkTaskContext ctx = mock(SinkTaskContext.class);

mockWriter.write(records);
expectLastCall().andThrow(new SQLException()).times(1 + maxRetries);

ctx.timeout(retryBackoffMs);
expectLastCall().times(maxRetries);

mockWriter.closeQuietly();
expectLastCall().times(maxRetries);
doThrow(new SQLException()).when(mockWriter).write(records);

final JdbcSinkTask task = new JdbcSinkTask() {
@Override
Expand All @@ -235,15 +230,15 @@ void initWriter() {
props.put(JdbcSinkConfig.RETRY_BACKOFF_MS, String.valueOf(retryBackoffMs));
task.start(props);

replayAll();

assertThatThrownBy(() -> task.put(records)).isInstanceOf(RetriableException.class);
assertThatThrownBy(() -> task.put(records)).isInstanceOf(RetriableException.class);
assertThatThrownBy(() -> task.put(records))
.isNotInstanceOf(RetriableException.class)
.isInstanceOf(ConnectException.class)
.hasCauseInstanceOf(SQLException.class);

verifyAll();
verify(ctx, times(maxRetries)).timeout(retryBackoffMs);
verify(mockWriter, times(maxRetries)).closeQuietly();
verify(mockWriter, times(maxRetries + 1)).write(records);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,19 @@
import io.aiven.connect.jdbc.source.JdbcSourceConnectorConfig.CachedRecommenderValues;
import io.aiven.connect.jdbc.source.JdbcSourceConnectorConfig.CachingRecommender;

import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.list;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Recommender.class})
@PowerMockIgnore("javax.management.*")
@RunWith(MockitoJUnitRunner.class)
public class JdbcSourceConnectorConfigTest {

private EmbeddedDerby db;
Expand Down Expand Up @@ -120,12 +116,9 @@ public void testCachingRecommender() {
final List<String> results1 = Collections.singletonList("xyz");
final List<String> results2 = Collections.singletonList("123");
// Set up the mock recommender to be called twice, returning different results each time
EasyMock.expect(mockRecommender.validValues(EasyMock.anyObject(String.class), EasyMock.anyObject(Map.class)))
.andReturn(results1);
EasyMock.expect(mockRecommender.validValues(EasyMock.anyObject(String.class), EasyMock.anyObject(Map.class)))
.andReturn(results2);

PowerMock.replayAll();
when(mockRecommender.validValues(any(String.class), any(Map.class)))
.thenReturn(results1)
.thenReturn(results2);

final CachingRecommender recommender = new CachingRecommender(mockRecommender, time, 1000L);

Expand All @@ -138,8 +131,6 @@ public void testCachingRecommender() {
// Wait for the cache to expire
time.sleep(2000L);
assertThat(recommender.validValues("x", config1)).isSameAs(results2);

PowerMock.verifyAll();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@
import org.apache.kafka.connect.source.SourceRecord;

import io.aiven.connect.jdbc.config.JdbcConfig;
import io.aiven.connect.jdbc.dialect.DatabaseDialect;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({JdbcSourceTask.class})
@PowerMockIgnore("javax.management.*")
@RunWith(MockitoJUnitRunner.class)
public class JdbcSourceTaskLifecycleTest extends JdbcSourceTaskTestBase {

@Mock
Expand All @@ -64,26 +60,14 @@ public void testMissingTables() {
}

@Test
public void testStartStop() throws Exception {
// Minimal start/stop functionality
PowerMock.expectNew(
SourceConnectionProvider.class,
EasyMock.anyObject(DatabaseDialect.class),
EasyMock.eq(JdbcSourceConnectorConfig.CONNECTION_ATTEMPTS_DEFAULT),
EasyMock.eq(JdbcSourceConnectorConfig.CONNECTION_BACKOFF_DEFAULT))
.andReturn(mockSourceConnectionProvider);

// Should request a connection, then should close it on stop()
EasyMock.expect(mockSourceConnectionProvider.getConnection()).andReturn(db.getConnection());

PowerMock.expectLastCall();

PowerMock.replayAll();

task.start(singleTableConfig());
task.stop();

PowerMock.verifyAll();
public void testStartStop() {
try (final var mock = mockConstruction(SourceConnectionProvider.class, (m, c) -> {
when(m.getConnection()).thenReturn(db.getConnection());
})) {
task.start(singleTableConfig());
task.stop();
verify(mock.constructed().get(0)).getConnection();
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
import io.aiven.connect.jdbc.config.JdbcConfig;
import io.aiven.connect.jdbc.util.TableId;

import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.powermock.api.easymock.annotation.Mock;
import org.mockito.Mock;

import static io.aiven.connect.jdbc.source.JdbcSourceConnectorConfig.NumericMapping;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

public class JdbcSourceTaskTestBase {

Expand Down Expand Up @@ -120,8 +121,8 @@ protected Map<String, String> twoTableConfig() {

protected <T> void expectInitialize(final Collection<Map<String, T>> partitions,
final Map<Map<String, T>, Map<String, Object>> offsets) {
EasyMock.expect(taskContext.offsetStorageReader()).andReturn(reader);
EasyMock.expect(reader.offsets(EasyMock.eq(partitions))).andReturn(offsets);
when(taskContext.offsetStorageReader()).thenReturn(reader);
when(reader.offsets(eq(partitions))).thenReturn(offsets);
}

protected <T> void expectInitializeNoOffsets(final Collection<Map<String, T>> partitions) {
Expand Down
Loading
Loading