Skip to content

Commit 9524985

Browse files
committed
Refactor resetStatistics()
1 parent 6dadf28 commit 9524985

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

src/main/java/com/lemick/assertions/HibernateStatementAssertionsProvider.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,4 @@ public HibernateStatementAssertionResult generateSelectStatementAssertion(int ex
2525
public HibernateStatementAssertionResult generateDeleteStatementAssertion(int expectedDeleteStatementCount, Supplier<HibernateStatistics> statisticsSupplier) {
2626
return new HibernateStatementAssertionResult(DELETE, statisticsSupplier.get().getDeleteStatementCount(), expectedDeleteStatementCount);
2727
}
28-
29-
public void resetStatistics() {
30-
HibernateStatementCountInspector.resetStatistics();
31-
}
3228
}

src/main/java/com/lemick/integration/hibernate/HibernateStatementCountInspector.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ public String inspect(String sql) {
1414
return sql;
1515
}
1616

17-
public static void resetStatistics() {
18-
statisticsStore.remove();
19-
statisticsStore.set(new HibernateStatistics());
20-
}
21-
2217
public static HibernateStatistics getStatistics() {
2318
return statisticsStore.get();
2419
}

src/main/java/com/lemick/integration/hibernate/HibernateStatistics.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ public class HibernateStatistics implements HibernateStatementCountListener{
77
private int insertStatementCount = 0;
88
private int deleteStatementCount = 0;
99

10+
public void resetStatistics() {
11+
selectStatementCount = 0;
12+
updateStatementCount = 0;
13+
insertStatementCount = 0;
14+
deleteStatementCount = 0;
15+
}
16+
1017
@Override
1118
public void notifySelectStatement() {
1219
selectStatementCount++;

src/main/java/com/lemick/integration/spring/HibernateStatementCountTestListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class HibernateStatementCountTestListener implements TestExecutionListene
2020

2121
@Override
2222
public void beforeTestMethod(TestContext testContext) {
23-
hibernateStatementAssertionsProvider.resetStatistics();
23+
statisticsSupplier.get().resetStatistics();
2424
}
2525

2626
@Override

src/test/java/com/lemick/integration/hibernate/HibernateStatementCountInspectorTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,4 @@ public void _inspect() {
2525
assertEquals("SELECT * FROM Post", actual, "output is the same as the input");
2626
verify(statementParser, description("parser is called")).parseSqlStatement("SELECT * FROM Post", HibernateStatementCountInspector.getStatistics());
2727
}
28-
29-
@Test
30-
public void _resetStatistics() {
31-
HibernateStatistics beforeStatistics = HibernateStatementCountInspector.getStatistics();
32-
33-
HibernateStatementCountInspector.resetStatistics();
34-
35-
assertNotSame(beforeStatistics, HibernateStatementCountInspector.getStatistics(), "the statistics has been recreated");
36-
}
3728
}

src/test/java/com/lemick/integration/hibernate/HibernateStatisticsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ public void _default_state() {
2121
assertEquals(0, model.getDeleteStatementCount(), "the count is initialized to 0");
2222
}
2323

24+
@Test
25+
public void _reset() {
26+
model.notifySelectStatement();
27+
model.notifyInsertStatement();
28+
model.notifyUpdateStatement();
29+
model.notifyDeleteStatement();
30+
31+
model.resetStatistics();
32+
33+
assertEquals(0, model.getSelectStatementCount(), "the count is reset to 0");
34+
assertEquals(0, model.getInsertStatementCount(), "the count is reset to 0");
35+
assertEquals(0, model.getUpdateStatementCount(), "the count is reset to 0");
36+
assertEquals(0, model.getDeleteStatementCount(), "the count is reset to 0");
37+
}
38+
2439
@Test
2540
public void _notify_increments() {
2641
model.notifySelectStatement();

src/test/java/com/lemick/integration/spring/HibernateStatementCountTestListenerTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.jupiter.api.extension.ExtendWith;
99
import org.mockito.InjectMocks;
1010
import org.mockito.Mock;
11+
import org.mockito.Spy;
1112
import org.mockito.junit.jupiter.MockitoExtension;
1213
import org.springframework.test.context.TestContext;
1314

@@ -43,16 +44,20 @@ public void notAnnotatedMethod() {
4344

4445
@Test
4546
public void _beforeTestMethod_no_annotation() {
47+
when(statisticsSupplier.get()).thenReturn(mock(HibernateStatistics.class));
48+
4649
model.beforeTestMethod(testContext);
4750

48-
verify(hibernateStatementAssertUtils, description("the reset method is called")).resetStatistics();
51+
verify(statisticsSupplier.get(), description("the reset method is called")).resetStatistics();
4952
}
5053

5154
@Test
5255
public void _beforeTestMethod_with_annotation() {
56+
when(statisticsSupplier.get()).thenReturn(mock(HibernateStatistics.class));
57+
5358
model.beforeTestMethod(testContext);
5459

55-
verify(hibernateStatementAssertUtils, description("the reset method is called")).resetStatistics();
60+
verify(statisticsSupplier.get(), description("the reset method is called")).resetStatistics();
5661
}
5762

5863
@Test

0 commit comments

Comments
 (0)