Skip to content

Commit

Permalink
limock添加skip方法
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Jan 2, 2025
1 parent 866dcc7 commit c322cf0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
12 changes: 12 additions & 0 deletions litool-test/src/main/java/io/leaderli/litool/test/LiMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ public static <T> T simpleInterface(Class<T> mockClass, Object... returns) {
return mockInterface.build();
}


public static void simple(Class<?> mockClass, Object... returns) {
if (mockClass.isInterface()) {
throw new IllegalArgumentException("not support interface " + mockClass);
Expand All @@ -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> T skipInterface(Class<T> 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<Void, Object> mocker(Class<?> mockClass) {
return new Mocker(mockClass, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

import java.lang.reflect.Field;

public class LiTestInstanceFactory implements TestInstanceFactory, ParameterResolver {
BeanCreator.MockBeanBuilder<Void> beanBuilder = BeanCreator.create(Void.class);
public class LiTestInstanceFactory implements TestInstanceFactory, ParameterResolver, BeforeEachCallback {
private BeanCreator.MockBeanBuilder<Void> 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;
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
17 changes: 11 additions & 6 deletions litool-test/src/test/java/io/leaderli/litool/test/LiTestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public class LiTestTest implements MockBeanBuilderConfig {
@Mock
Bean2 bean2;

BeanCreator.MockBeanBuilder<Void> beanBuilder;

@Override
public void init(BeanCreator.MockBeanBuilder<Void> mockBeanBuilder) {
mockBeanBuilder.cache(LiTestTest.Bean2.class, new LiTestTest.Bean2());
public void init(BeanCreator.MockBeanBuilder<Void> beanBuilder) {
beanBuilder.cache(LiTestTest.Bean2.class, new LiTestTest.Bean2());
Assertions.assertTrue(this.beanBuilder == null || this.beanBuilder != beanBuilder);
this.beanBuilder = beanBuilder;
}


Expand All @@ -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 {

Expand Down

0 comments on commit c322cf0

Please sign in to comment.