44import com .lemick .assertions .HibernateStatementAssertionResult ;
55import com .lemick .assertions .HibernateStatementAssertionsProvider ;
66import com .lemick .integration .hibernate .HibernateStatistics ;
7+ import jakarta .persistence .EntityManager ;
8+ import jakarta .transaction .Transactional ;
79import org .junit .jupiter .api .Test ;
810import org .junit .jupiter .api .extension .ExtendWith ;
11+ import org .mockito .Answers ;
912import org .mockito .InjectMocks ;
1013import org .mockito .Mock ;
1114import org .mockito .Spy ;
1922@ ExtendWith (MockitoExtension .class )
2023class HibernateStatementCountTestListenerTest {
2124
22- public class FakeClass {
25+ public static class FakeClass {
2326 @ AssertHibernateSQLStatementCount (inserts = 1 , deletes = 2 , selects = 3 , updates = 4 )
2427 public void annotatedMethod () {
2528
@@ -40,6 +43,9 @@ public void notAnnotatedMethod() {
4043 Supplier <HibernateStatistics > statisticsSupplier ;
4144
4245 @ Mock
46+ Supplier <Boolean > transactionAvailabilitySupplier ;
47+
48+ @ Mock (answer = Answers .RETURNS_DEEP_STUBS )
4349 TestContext testContext ;
4450
4551 @ Test
@@ -61,7 +67,7 @@ public void _beforeTestMethod_with_annotation() {
6167 }
6268
6369 @ Test
64- public void _afterTestMethod_no_annotation () throws NoSuchMethodException {
70+ public void _afterTestMethod_without_annotation () throws NoSuchMethodException {
6571 when (testContext .getTestMethod ()).thenReturn (FakeClass .class .getMethod ("notAnnotatedMethod" ));
6672
6773 model .afterTestMethod (testContext );
@@ -73,8 +79,31 @@ public void _afterTestMethod_no_annotation() throws NoSuchMethodException {
7379 }
7480
7581 @ Test
76- public void _afterTestMethod_with_annotation () throws NoSuchMethodException {
82+ public void _afterTestMethod_with_annotation_without_transaction () throws NoSuchMethodException {
83+ when (testContext .getTestMethod ()).thenReturn (FakeClass .class .getMethod ("annotatedMethod" ));
84+ when (transactionAvailabilitySupplier .get ()).thenReturn (false );
85+
86+ HibernateStatementAssertionResult statementAssertionResult = mock (HibernateStatementAssertionResult .class );
87+ when (hibernateStatementAssertUtils .generateInsertStatementAssertion (anyInt (), any ())).thenReturn (statementAssertionResult );
88+ when (hibernateStatementAssertUtils .generateSelectStatementAssertion (anyInt (), any ())).thenReturn (statementAssertionResult );
89+ when (hibernateStatementAssertUtils .generateUpdateStatementAssertion (anyInt (), any ())).thenReturn (statementAssertionResult );
90+ when (hibernateStatementAssertUtils .generateDeleteStatementAssertion (anyInt (), any ())).thenReturn (statementAssertionResult );
91+
92+ model .afterTestMethod (testContext );
93+
94+ verify (testContext , times (0 ).description ("EntityManager is NOT flushed" )).getApplicationContext ();
95+ verify (hibernateStatementAssertUtils , description ("assert method is called" )).generateSelectStatementAssertion (3 , statisticsSupplier );
96+ verify (hibernateStatementAssertUtils , description ("assert method is called" )).generateUpdateStatementAssertion (4 , statisticsSupplier );
97+ verify (hibernateStatementAssertUtils , description ("assert method is called" )).generateInsertStatementAssertion (1 , statisticsSupplier );
98+ verify (hibernateStatementAssertUtils , description ("assert method is called" )).generateDeleteStatementAssertion (2 , statisticsSupplier );
99+ }
100+
101+ @ Test
102+ public void _afterTestMethod_with_annotation_with_transaction () throws NoSuchMethodException {
103+ EntityManager entityManager = mock (EntityManager .class );
77104 when (testContext .getTestMethod ()).thenReturn (FakeClass .class .getMethod ("annotatedMethod" ));
105+ when (testContext .getApplicationContext ().getAutowireCapableBeanFactory ().getBean (EntityManager .class )).thenReturn (entityManager );
106+ when (transactionAvailabilitySupplier .get ()).thenReturn (true );
78107
79108 HibernateStatementAssertionResult statementAssertionResult = mock (HibernateStatementAssertionResult .class );
80109 when (hibernateStatementAssertUtils .generateInsertStatementAssertion (anyInt (), any ())).thenReturn (statementAssertionResult );
@@ -84,6 +113,7 @@ public void _afterTestMethod_with_annotation() throws NoSuchMethodException {
84113
85114 model .afterTestMethod (testContext );
86115
116+ verify (entityManager , description ("EntityManager is flushed" )).flush ();
87117 verify (hibernateStatementAssertUtils , description ("assert method is called" )).generateSelectStatementAssertion (3 , statisticsSupplier );
88118 verify (hibernateStatementAssertUtils , description ("assert method is called" )).generateUpdateStatementAssertion (4 , statisticsSupplier );
89119 verify (hibernateStatementAssertUtils , description ("assert method is called" )).generateInsertStatementAssertion (1 , statisticsSupplier );
0 commit comments