From 18b5478c223d21c1d6de062f067b2ffdce178375 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 20 Dec 2021 23:36:28 +0800 Subject: [PATCH] Add verify for test cases of #13958 (#14179) --- ...poseClassStaticMethodAroundAdviceTest.java | 54 +++++++++++-------- .../advice/ComposeConstructorAdviceTest.java | 31 +++++++---- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeClassStaticMethodAroundAdviceTest.java b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeClassStaticMethodAroundAdviceTest.java index 30c13f8e36669..9adfc02fbe74e 100644 --- a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeClassStaticMethodAroundAdviceTest.java +++ b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeClassStaticMethodAroundAdviceTest.java @@ -19,48 +19,56 @@ import org.apache.shardingsphere.agent.api.advice.ClassStaticMethodAroundAdvice; import org.apache.shardingsphere.agent.api.result.MethodInvocationResult; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.Collections; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) -public class ComposeClassStaticMethodAroundAdviceTest { - - private ComposeClassStaticMethodAroundAdvice composeClassStaticMethodAroundAdvice; - +public final class ComposeClassStaticMethodAroundAdviceTest { + + @Mock + private ClassStaticMethodAroundAdvice classStaticMethodAroundAdvice; + + private ComposeClassStaticMethodAroundAdvice actual; + + @Before + public void setUp() { + actual = new ComposeClassStaticMethodAroundAdvice(new ArrayList<>(Collections.singletonList(classStaticMethodAroundAdvice))); + } + @Test - public void beforeMethodTest() { - ClassStaticMethodAroundAdvice advice = mock(ClassStaticMethodAroundAdvice.class); + public void assertBeforeMethod() { Method method = mock(Method.class); + Object[] args = new Object[2]; MethodInvocationResult methodInvocationResult = mock(MethodInvocationResult.class); - List adviceList = new ArrayList<>(Arrays.asList(advice)); - composeClassStaticMethodAroundAdvice = new ComposeClassStaticMethodAroundAdvice(adviceList); - composeClassStaticMethodAroundAdvice.beforeMethod(String.class, method, new Object[2], methodInvocationResult); + actual.beforeMethod(String.class, method, args, methodInvocationResult); + verify(classStaticMethodAroundAdvice).beforeMethod(String.class, method, args, methodInvocationResult); } - + @Test - public void afterMethodTest() { - ClassStaticMethodAroundAdvice advice = mock(ClassStaticMethodAroundAdvice.class); + public void assertAfterMethod() { Method method = mock(Method.class); + Object[] args = new Object[2]; MethodInvocationResult methodInvocationResult = mock(MethodInvocationResult.class); - List adviceList = new ArrayList<>(Arrays.asList(advice)); - composeClassStaticMethodAroundAdvice = new ComposeClassStaticMethodAroundAdvice(adviceList); - composeClassStaticMethodAroundAdvice.afterMethod(String.class, method, new Object[2], methodInvocationResult); + actual.afterMethod(String.class, method, args, methodInvocationResult); + verify(classStaticMethodAroundAdvice).afterMethod(String.class, method, args, methodInvocationResult); } - + @Test - public void onThrowingTest() { - ClassStaticMethodAroundAdvice advice = mock(ClassStaticMethodAroundAdvice.class); + public void assertOnThrowing() { Method method = mock(Method.class); - List adviceList = new ArrayList<>(Arrays.asList(advice)); - composeClassStaticMethodAroundAdvice = new ComposeClassStaticMethodAroundAdvice(adviceList); - composeClassStaticMethodAroundAdvice.onThrowing(String.class, method, new Object[2], new NullPointerException("Null Pointer")); + Object[] args = new Object[2]; + NullPointerException exception = new NullPointerException(""); + actual.onThrowing(String.class, method, args, exception); + verify(classStaticMethodAroundAdvice).onThrowing(String.class, method, args, exception); } } diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeConstructorAdviceTest.java b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeConstructorAdviceTest.java index 0fca644ca5555..333b49abde1e7 100644 --- a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeConstructorAdviceTest.java +++ b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/advice/ComposeConstructorAdviceTest.java @@ -19,27 +19,36 @@ import org.apache.shardingsphere.agent.api.advice.AdviceTargetObject; import org.apache.shardingsphere.agent.api.advice.ConstructorAdvice; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.Collections; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) -public class ComposeConstructorAdviceTest { - - private ComposeConstructorAdvice composeConstructorAdvice; - +public final class ComposeConstructorAdviceTest { + + @Mock + private ConstructorAdvice constructorAdvice; + + private ComposeConstructorAdvice actual; + + @Before + public void setUp() { + actual = new ComposeConstructorAdvice(new ArrayList<>(Collections.singletonList(constructorAdvice))); + } + @Test - public void onConstructorTest() { - ConstructorAdvice advice = mock(ConstructorAdvice.class); + public void assertOnConstructor() { AdviceTargetObject adviceTargetObject = mock(AdviceTargetObject.class); - List adviceList = new ArrayList<>(Arrays.asList(advice)); - composeConstructorAdvice = new ComposeConstructorAdvice(adviceList); - composeConstructorAdvice.onConstructor(adviceTargetObject, new Object[2]); + Object[] args = new Object[2]; + actual.onConstructor(adviceTargetObject, args); + verify(constructorAdvice).onConstructor(adviceTargetObject, args); } }