divijvaidya commented on code in PR #12459:
URL: https://github.com/apache/kafka/pull/12459#discussion_r952364522


##########
streams/src/test/java/org/apache/kafka/streams/state/internals/metrics/RocksDBMetricsRecorderTest.java:
##########
@@ -70,105 +73,133 @@ public class RocksDBMetricsRecorderTest {
     private final Statistics statisticsToAdd2 = mock(Statistics.class);
     private final Statistics statisticsToAdd3 = mock(Statistics.class);
 
-    private final Sensor bytesWrittenToDatabaseSensor = 
createMock(Sensor.class);
-    private final Sensor bytesReadFromDatabaseSensor = 
createMock(Sensor.class);
-    private final Sensor memtableBytesFlushedSensor = createMock(Sensor.class);
-    private final Sensor memtableHitRatioSensor = createMock(Sensor.class);
-    private final Sensor memtableAvgFlushTimeSensor = createMock(Sensor.class);
-    private final Sensor memtableMinFlushTimeSensor = createMock(Sensor.class);
-    private final Sensor memtableMaxFlushTimeSensor = createMock(Sensor.class);
-    private final Sensor writeStallDurationSensor = createMock(Sensor.class);
-    private final Sensor blockCacheDataHitRatioSensor = 
createMock(Sensor.class);
-    private final Sensor blockCacheIndexHitRatioSensor = 
createMock(Sensor.class);
-    private final Sensor blockCacheFilterHitRatioSensor = 
createMock(Sensor.class);
-    private final Sensor bytesReadDuringCompactionSensor = 
createMock(Sensor.class);
-    private final Sensor bytesWrittenDuringCompactionSensor = 
createMock(Sensor.class);
-    private final Sensor compactionTimeAvgSensor = createMock(Sensor.class);
-    private final Sensor compactionTimeMinSensor = createMock(Sensor.class);
-    private final Sensor compactionTimeMaxSensor = createMock(Sensor.class);
-    private final Sensor numberOfOpenFilesSensor = createMock(Sensor.class);
-    private final Sensor numberOfFileErrorsSensor = createMock(Sensor.class);
-
-    private final StreamsMetricsImpl streamsMetrics = 
niceMock(StreamsMetricsImpl.class);
+    private final Sensor bytesWrittenToDatabaseSensor = mock(Sensor.class);
+    private final Sensor bytesReadFromDatabaseSensor = mock(Sensor.class);
+    private final Sensor memtableBytesFlushedSensor = mock(Sensor.class);
+    private final Sensor memtableHitRatioSensor = mock(Sensor.class);
+    private final Sensor memtableAvgFlushTimeSensor = mock(Sensor.class);
+    private final Sensor memtableMinFlushTimeSensor = mock(Sensor.class);
+    private final Sensor memtableMaxFlushTimeSensor = mock(Sensor.class);
+    private final Sensor writeStallDurationSensor = mock(Sensor.class);
+    private final Sensor blockCacheDataHitRatioSensor = mock(Sensor.class);
+    private final Sensor blockCacheIndexHitRatioSensor = mock(Sensor.class);
+    private final Sensor blockCacheFilterHitRatioSensor = mock(Sensor.class);
+    private final Sensor bytesReadDuringCompactionSensor = mock(Sensor.class);
+    private final Sensor bytesWrittenDuringCompactionSensor = 
mock(Sensor.class);
+    private final Sensor compactionTimeAvgSensor = mock(Sensor.class);
+    private final Sensor compactionTimeMinSensor = mock(Sensor.class);
+    private final Sensor compactionTimeMaxSensor = mock(Sensor.class);
+    private final Sensor numberOfOpenFilesSensor = mock(Sensor.class);
+    private final Sensor numberOfFileErrorsSensor = mock(Sensor.class);
+
+    private final StreamsMetricsImpl streamsMetrics = 
mock(StreamsMetricsImpl.class);
     private final RocksDBMetricsRecordingTrigger recordingTrigger = 
mock(RocksDBMetricsRecordingTrigger.class);
 
     private final RocksDBMetricsRecorder recorder = new 
RocksDBMetricsRecorder(METRICS_SCOPE, STORE_NAME);
 
+    private MockedStatic<RocksDBMetrics> dbMetrics;
+
     @Before
     public void setUp() {
-        setUpMetricsStubMock();
-        
expect(streamsMetrics.rocksDBMetricsRecordingTrigger()).andStubReturn(recordingTrigger);
-        replay(streamsMetrics);
+        setUpMetricsMock();
+        
when(streamsMetrics.rocksDBMetricsRecordingTrigger()).thenReturn(recordingTrigger);
         recorder.init(streamsMetrics, TASK_ID1);
     }
 
+    @After
+    public void cleanUpMocks() {
+        dbMetrics.close();

Review Comment:
   The current change is robust as well since we are closing the object in the 
lifecycle hook of a unit test which will guarantee that close is always called 
at the end of a unit test. Hence, I don't think that using try-with-resource is 
improving code quality here.
   
   On the other hand, the disadvantage of using try-with-resource is that we 
will have to duplicate same code (or same function) for initialising 
`dbMetrics`, initializing mocks and calling init() locally across all unit 
tests, which is superfluous given that lifecycle hooks can do that you in JUnit.
   
   What do you think? (I don't have strong opinion on this one and if you say 
we still want to go with try-with-resource, I will be happy to)
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to