From c322cf04188b8e930bba3fc96461c5667ea14ba2 Mon Sep 17 00:00:00 2001 From: leaderli <429243408@qq.com> Date: Thu, 2 Jan 2025 16:07:04 +0800 Subject: [PATCH] =?UTF-8?q?limock=E6=B7=BB=E5=8A=A0skip=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/leaderli/litool/test/LiMock.java | 12 ++++++++++ .../java/io/leaderli/litool/test/LiTest.java | 2 +- .../litool/test/LiTestInstanceFactory.java | 22 +++++++++++++------ .../io/leaderli/litool/test/LiMockTest.java | 9 ++++++++ .../io/leaderli/litool/test/LiTestTest.java | 17 +++++++++----- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java b/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java index dd9a56e8..105b6adc 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java @@ -504,6 +504,7 @@ public static T simpleInterface(Class mockClass, Object... returns) { return mockInterface.build(); } + public static void simple(Class mockClass, Object... returns) { if (mockClass.isInterface()) { throw new IllegalArgumentException("not support interface " + mockClass); @@ -523,6 +524,17 @@ public static void simple(Class mockClass, Object... returns) { mock(mockClass, MethodFilter.of(returnMap::containsKey), (m, args) -> returnMap.get(m)); } + public static T skipInterface(Class mockClass) { + return new MockInterface<>(mockClass).build(); + } + + public static void skip(Class mockClass) { + if (mockClass.isInterface()) { + throw new IllegalArgumentException("not support interface " + mockClass); + } + mock(mockClass, MethodFilter.isMethod(), MethodProxy.NONE); + } + public static IMocker mocker(Class mockClass) { return new Mocker(mockClass, true); } diff --git a/litool-test/src/main/java/io/leaderli/litool/test/LiTest.java b/litool-test/src/main/java/io/leaderli/litool/test/LiTest.java index d1796b44..9c829ec7 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/LiTest.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/LiTest.java @@ -11,7 +11,7 @@ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) -@ExtendWith({LiTestInstanceFactory.class, LiMockBeforeEachCallback.class, LiMockAfterEachCallback.class}) +@ExtendWith({LiMockBeforeEachCallback.class, LiMockAfterEachCallback.class}) @Test public @interface LiTest { } diff --git a/litool-test/src/main/java/io/leaderli/litool/test/LiTestInstanceFactory.java b/litool-test/src/main/java/io/leaderli/litool/test/LiTestInstanceFactory.java index d740d33a..204fea80 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/LiTestInstanceFactory.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/LiTestInstanceFactory.java @@ -6,22 +6,25 @@ import java.lang.reflect.Field; -public class LiTestInstanceFactory implements TestInstanceFactory, ParameterResolver { - BeanCreator.MockBeanBuilder beanBuilder = BeanCreator.create(Void.class); +public class LiTestInstanceFactory implements TestInstanceFactory, ParameterResolver, BeforeEachCallback { + private BeanCreator.MockBeanBuilder beanBuilder = BeanCreator.create(Void.class); + + private void resetBeanBuilder(Object testInstance) { + if (testInstance instanceof MockBeanBuilderConfig) { + ((MockBeanBuilderConfig) testInstance).init(beanBuilder); + this.beanBuilder = BeanCreator.create(Void.class); + } + } @Override public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) throws TestInstantiationException { Class testClass = factoryContext.getTestClass(); Object testInstance = ReflectUtil.newInstance(testClass).get(); - - if (testInstance instanceof MockBeanBuilderConfig) { - ((MockBeanBuilderConfig) testInstance).init(beanBuilder); - } + resetBeanBuilder(testInstance); for (Field field : ReflectUtil.getFields(testClass)) { setMockField(testInstance, field); } - return testInstance; } @@ -42,4 +45,9 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return beanBuilder.type(parameterContext.getParameter().getType()).build().create(); } + + @Override + public void beforeEach(ExtensionContext context) { + context.getTestInstance().ifPresent(this::resetBeanBuilder); + } } diff --git a/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java b/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java index fa59c406..a273e906 100644 --- a/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java +++ b/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java @@ -37,6 +37,15 @@ void testSimple() { Assertions.assertEquals(10, supplier.get()); } + + @Test + void testSkip() { + Assertions.assertEquals(1, new Bean11().m1(1)); + LiMock.skip(Bean11.class); + Assertions.assertEquals(0, new Bean11().m1(1)); + Supplier supplier = LiMock.skipInterface(Supplier.class); + Assertions.assertNull(supplier.get()); + } @LiTest void skipClassConstructors() { diff --git a/litool-test/src/test/java/io/leaderli/litool/test/LiTestTest.java b/litool-test/src/test/java/io/leaderli/litool/test/LiTestTest.java index 71e3708e..39ee9cad 100644 --- a/litool-test/src/test/java/io/leaderli/litool/test/LiTestTest.java +++ b/litool-test/src/test/java/io/leaderli/litool/test/LiTestTest.java @@ -12,9 +12,13 @@ public class LiTestTest implements MockBeanBuilderConfig { @Mock Bean2 bean2; + BeanCreator.MockBeanBuilder beanBuilder; + @Override - public void init(BeanCreator.MockBeanBuilder mockBeanBuilder) { - mockBeanBuilder.cache(LiTestTest.Bean2.class, new LiTestTest.Bean2()); + public void init(BeanCreator.MockBeanBuilder beanBuilder) { + beanBuilder.cache(LiTestTest.Bean2.class, new LiTestTest.Bean2()); + Assertions.assertTrue(this.beanBuilder == null || this.beanBuilder != beanBuilder); + this.beanBuilder = beanBuilder; } @@ -25,12 +29,13 @@ void testParameter(int a, Bean bean, Bean2 bean2) { Assertions.assertNotNull(this.bean); Assertions.assertSame(bean, bean.bean); Assertions.assertSame(this.bean, this.bean.bean); - System.out.println(bean2); - System.out.println(this.bean2); - - Assertions.assertSame(bean2, this.bean2); + Assertions.assertNotSame(bean2, this.bean2); } + @LiTest + void testParameter2() { + Assertions.assertNotNull(this.beanBuilder); + } static class Bean {