Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I cant use It #156

Open
YAGAMIL opened this issue Sep 22, 2022 · 9 comments
Open

I cant use It #156

YAGAMIL opened this issue Sep 22, 2022 · 9 comments
Labels
question Further information is requested.

Comments

@YAGAMIL
Copy link

YAGAMIL commented Sep 22, 2022

SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

java.lang.ClassCastException: ch.qos.logback.classic.Logger cannot be cast to org.simplify4u.slf4jmock.ProxyMock

	at org.simplify4u.slf4jmock.mockito.LoggerAnnotationEngine.findLoggersMocks(LoggerAnnotationEngine.java:166)
	at org.simplify4u.slf4jmock.mockito.LoggerAnnotationEngine.process(LoggerAnnotationEngine.java:59)
	at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:81)
	at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:50)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:100)
	at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:107)
	at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:41)
	at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)


java.lang.NullPointerException
	at com.alibaba.cloudgame.MockTest.releaseMocks(MockTest.java:25)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.RunAfters.invokeMethod(RunAfters.java:46)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:100)
	at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:107)
	at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:41)
	at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
@slawekjaranowski
Copy link
Member

Can you provide a simple project which reproduce such issue?

@YAGAMIL
Copy link
Author

YAGAMIL commented Sep 22, 2022

Can you provide a simple project which reproduce such issue?

It's almost identical to your case

@Component
@Slf4j
public class OssManager {
	@Resource
	private OssCopyUtil ossCopyUtil;

	public void asyncCopyFile() {
		new ForkJoinPool().execute(() -> {
				try {
					ossPartCopyUtil.copy();
				}
				catch (Exception e) {
					log.error("copy has an oss exception, message={}",e.getMessage());
			}
		});
	}

}
@RunWith(MockitoJUnitRunner.class)
public class OssManagerTest extends MockTest {

	@Mock
	Logger log;

	@Mock
	OssCopyUtil ossCopyUtil;

	@InjectMocks
	@Spy
	OssManager ossManager;

	@SneakyThrows
	@Test
	public void testAsyncCopyFile() {
		when(ossCopyUtil.copy()).thenReturn("").thenThrow(new RuntimeException());
		ossManager.asyncCopyFile();
		verify(log, timeout(1000).times(1)).error(anyString(), anyString(), anyLong(), anyString());
	}
}

@slawekjaranowski
Copy link
Member

To reproduce I need a complete project example code na pom.xml ... sometime dependencies in project are important

@YAGAMIL
Copy link
Author

YAGAMIL commented Sep 22, 2022

To reproduce I need a complete project example code na pom.xml ... sometime dependencies in project are important

I can only provide you with partial dependence The SpringBoot version is 2.2.4.RELEASE

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!-- 因为uic、switchcenter、sentinel强依赖log4j,所以要排掉log4j-over-slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>999-not-exist-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>4.8.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-inline -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>4.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.simplify4u</groupId>
            <artifactId>slf4j-mock</artifactId>
            <version>2.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy-agent</artifactId>
            <version>1.12.10</version>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.12.10</version>
        </dependency>

@slawekjaranowski
Copy link
Member

I need a simple project which can I build and show your issue - without it is difficult to debug and fix.

Try creating new Maven project with pom and simple test, next please attach in issue or provide link to repository.

@YAGAMIL
Copy link
Author

YAGAMIL commented Sep 23, 2022

ok,But this is a problem I have at work, and I need time to sort out the dependencies

@vitalijr2
Copy link
Contributor

My two cents:

ch.qos.logback

Logback is in the classpath. I think you should exclude it, something like that:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>surefire or failsafe</artifactId>
        <version>2.22.2</version>
        <configuration>
          ...
          <classpathDependencyExcludes>
            <classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
            <classpathDependencyExclude>ch.qos.logback:logback-core</classpathDependencyExclude>
          </classpathDependencyExcludes>
        </configuration>
      </plugin>

@YAGAMIL
Copy link
Author

YAGAMIL commented Sep 28, 2022

I also think it's the dependency conflict

But I am really busy recently and have no time to test
I can provide you with a list of related 'log' dependencies
image

@YAGAMIL
Copy link
Author

YAGAMIL commented Sep 28, 2022

My two cents:

ch.qos.logback

Logback is in the classpath. I think you should exclude it, something like that:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>surefire or failsafe</artifactId>
        <version>2.22.2</version>
        <configuration>
          ...
          <classpathDependencyExcludes>
            <classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
            <classpathDependencyExclude>ch.qos.logback:logback-core</classpathDependencyExclude>
          </classpathDependencyExcludes>
        </configuration>
      </plugin>

I eliminated dependency the way you did, but

		verify(log, timeout(1000).times(1)).error(anyString(), anyString());

This verification is invalid, but in fact it executes log.error(anyString(), any(Object[].class)) During the debug process, I found that the log is indeed an object of mock.

on the contrary,Such verification is successful

		verifyNoInteractions(log);
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <classpathDependencyExcludes>
                        <classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
                        <classpathDependencyExclude>ch.qos.logback:logback-core</classpathDependencyExclude>
                    </classpathDependencyExcludes>
                </configuration>
            </plugin>

@slawekjaranowski slawekjaranowski added the question Further information is requested. label Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested.
Development

No branches or pull requests

3 participants