Skip to content

Commit 8651b21

Browse files
chore: drop test dep on mrunit and use mockito instead (#4257)
mrunit is EOL Change-Id: Icb9abbd5c6e50f511e79f106ae9a99ed519f588f
1 parent ce9e53b commit 8651b21

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

bigtable-hbase-1.x-parent/bigtable-hbase-1.x-mapreduce/pom.xml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ limitations under the License.
3434
in order to work with Bigtable.
3535
</description>
3636

37-
<properties>
38-
<mrunit.version>1.1.0</mrunit.version>
39-
</properties>
40-
4137
<!-- NOTE: this artifact is designed to be used alongside hbase-server via
4238
the bin/hbase script. Its primary intention is to produce a single jar that can
4339
be added to hbase's classpath to kick off mapreduce jobs targeted at bigtable.
@@ -91,10 +87,15 @@ limitations under the License.
9187
<scope>test</scope>
9288
</dependency>
9389
<dependency>
94-
<groupId>org.apache.mrunit</groupId>
95-
<artifactId>mrunit</artifactId>
96-
<version>${mrunit.version}</version>
97-
<classifier>hadoop2</classifier>
90+
<groupId>com.google.truth</groupId>
91+
<artifactId>truth</artifactId>
92+
<version>${truth.version}</version>
93+
<scope>test</scope>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.mockito</groupId>
97+
<artifactId>mockito-core</artifactId>
98+
<version>${mockito.version}</version>
9899
<scope>test</scope>
99100
</dependency>
100101
<dependency>

bigtable-hbase-1.x-parent/bigtable-hbase-1.x-mapreduce/src/test/java/com/google/cloud/bigtable/mapreduce/hbasesnapshots/TestSnapshotMapper.java

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,65 @@
1515
*/
1616
package com.google.cloud.bigtable.mapreduce.hbasesnapshots;
1717

18+
import static org.mockito.Mockito.doAnswer;
19+
import static org.mockito.Mockito.when;
20+
1821
import com.google.cloud.bigtable.mapreduce.hbasesnapshots.ImportHBaseSnapshotJob.ScanCounter;
1922
import com.google.cloud.bigtable.mapreduce.hbasesnapshots.ImportHBaseSnapshotJob.SnapshotMapper;
20-
import java.io.IOException;
2123
import java.util.ArrayList;
2224
import java.util.List;
23-
import org.apache.hadoop.conf.Configuration;
2425
import org.apache.hadoop.hbase.Cell;
2526
import org.apache.hadoop.hbase.CellScanner;
2627
import org.apache.hadoop.hbase.CellUtil;
2728
import org.apache.hadoop.hbase.KeyValue;
2829
import org.apache.hadoop.hbase.client.Put;
2930
import org.apache.hadoop.hbase.client.Result;
3031
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
31-
import org.apache.hadoop.hbase.mapreduce.MutationSerialization;
32-
import org.apache.hadoop.hbase.mapreduce.ResultSerialization;
3332
import org.apache.hadoop.hbase.util.Bytes;
34-
import org.apache.hadoop.mapreduce.Counters;
35-
import org.apache.hadoop.mrunit.mapreduce.MapDriver;
36-
import org.apache.hadoop.mrunit.types.Pair;
33+
import org.apache.hadoop.hbase.util.Pair;
34+
import org.apache.hadoop.mapreduce.Counter;
35+
import org.apache.hadoop.mapreduce.Mapper;
3736
import org.junit.Assert;
3837
import org.junit.Before;
38+
import org.junit.Rule;
3939
import org.junit.Test;
40+
import org.mockito.Mock;
41+
import org.mockito.Mockito;
42+
import org.mockito.junit.MockitoJUnit;
43+
import org.mockito.junit.MockitoRule;
4044

4145
/** test mapper for snapshot import */
4246
public class TestSnapshotMapper {
47+
@Rule public final MockitoRule mockitoRule = MockitoJUnit.rule();
48+
private SnapshotMapper mappUnderTest;
4349

44-
private MapDriver<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put> mapDriver;
50+
@Mock private Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put>.Context mockCtx;
51+
@Mock private Counter rowCounter;
52+
@Mock private Counter cellCounter;
53+
private List<Pair<ImmutableBytesWritable, Put>> resultList;
4554

4655
@Before
47-
public void setup() {
48-
SnapshotMapper snapshotMapper = new SnapshotMapper();
49-
mapDriver = MapDriver.newMapDriver(snapshotMapper);
50-
Configuration conf = new Configuration();
51-
mapDriver
52-
.getConfiguration()
53-
.setStrings(
54-
"io.serializations",
55-
conf.get("io.serializations"),
56-
MutationSerialization.class.getName(),
57-
ResultSerialization.class.getName());
56+
public void setup() throws Exception {
57+
mappUnderTest = new SnapshotMapper();
58+
59+
when(mockCtx.getCounter(Mockito.eq(ScanCounter.NUM_ROWS))).thenReturn(rowCounter);
60+
when(mockCtx.getCounter(Mockito.eq(ScanCounter.NUM_CELLS))).thenReturn(cellCounter);
61+
62+
resultList = new ArrayList<>();
63+
doAnswer(
64+
invocationOnMock -> {
65+
resultList.add(
66+
Pair.newPair(
67+
(ImmutableBytesWritable) invocationOnMock.getArguments()[0],
68+
(Put) invocationOnMock.getArguments()[1]));
69+
return null;
70+
})
71+
.when(mockCtx)
72+
.write(Mockito.any(), Mockito.any());
5873
}
5974

6075
@Test
61-
public void testSnapshotMapper() throws IOException {
76+
public void testSnapshotMapper() throws Exception {
6277
int rowCount = 20;
6378
int cellCount = 10;
6479
byte[] row = Bytes.toBytes("row");
@@ -68,7 +83,6 @@ public void testSnapshotMapper() throws IOException {
6883
long ts = 123L;
6984

7085
ImmutableBytesWritable key = new ImmutableBytesWritable(row);
71-
7286
for (int h = 0; h < rowCount; h++) {
7387
List<Cell> cellList = new ArrayList<>();
7488
for (int i = 0; i < cellCount; i++) {
@@ -77,11 +91,10 @@ public void testSnapshotMapper() throws IOException {
7791
}
7892

7993
Result res = Result.create(cellList);
80-
mapDriver.addInput(new Pair<>(key, res));
94+
mappUnderTest.map(key, res, mockCtx);
8195
}
8296

83-
List<Pair<ImmutableBytesWritable, Put>> resultList = mapDriver.run();
84-
Assert.assertEquals(rowCount, resultList.size());
97+
Mockito.verify(mockCtx, Mockito.times(rowCount)).write(Mockito.any(), Mockito.any());
8598

8699
int cellResCount = 0;
87100
for (Pair<ImmutableBytesWritable, Put> r : resultList) {
@@ -100,15 +113,12 @@ public void testSnapshotMapper() throws IOException {
100113
Assert.assertEquals((rowCount * cellCount), cellResCount);
101114

102115
// verify counters
103-
Counters counters = mapDriver.getCounters();
104-
long numRows = counters.findCounter(ScanCounter.NUM_ROWS).getValue();
105-
long numCells = counters.findCounter(ScanCounter.NUM_CELLS).getValue();
106-
Assert.assertEquals(rowCount, numRows);
107-
Assert.assertEquals((rowCount * cellCount), numCells);
116+
Mockito.verify(rowCounter, Mockito.times(rowCount)).increment(Mockito.eq(1L));
117+
Mockito.verify(cellCounter, Mockito.times(rowCount)).increment(Mockito.eq((long) cellCount));
108118
}
109119

110120
@Test
111-
public void testRowExceedingMaxCells() throws IOException {
121+
public void testRowExceedingMaxCells() throws Exception {
112122
int cellCount = SnapshotMapper.MAX_CELLS + 100;
113123
byte[] row = Bytes.toBytes("row");
114124
ImmutableBytesWritable key = new ImmutableBytesWritable(row);
@@ -121,10 +131,9 @@ public void testRowExceedingMaxCells() throws IOException {
121131
}
122132

123133
Result res = Result.create(cellList);
124-
Pair<ImmutableBytesWritable, Result> input = new Pair<>(key, res);
125-
mapDriver.addInput(input);
126134

127-
List<Pair<ImmutableBytesWritable, Put>> resultList = mapDriver.run();
135+
mappUnderTest.map(key, res, mockCtx);
136+
128137
Assert.assertEquals(2, resultList.size());
129138

130139
int cellResCount = 0;
@@ -137,10 +146,7 @@ public void testRowExceedingMaxCells() throws IOException {
137146
Assert.assertEquals(cellCount, cellResCount);
138147

139148
// verify counters
140-
Counters counters = mapDriver.getCounters();
141-
long numRows = counters.findCounter(ScanCounter.NUM_ROWS).getValue();
142-
long numCells = counters.findCounter(ScanCounter.NUM_CELLS).getValue();
143-
Assert.assertEquals(1, numRows);
144-
Assert.assertEquals(cellCount, numCells);
149+
Mockito.verify(rowCounter, Mockito.times(1)).increment(Mockito.eq(1L));
150+
Mockito.verify(cellCounter, Mockito.times(1)).increment(Mockito.eq((long) cellCount));
145151
}
146152
}

0 commit comments

Comments
 (0)