非Springboot项目使用powermockito单元测试 mock静态方法和ScriptEngineManager providers.next()...not a subtype报错解决

这篇具有很好参考价值的文章主要介绍了非Springboot项目使用powermockito单元测试 mock静态方法和ScriptEngineManager providers.next()...not a subtype报错解决。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

mock静态方法步骤

  1. 使用PowerMockito.mockStatic()
  2. 加上@RunWith和@PrepareForTest注释
  3. 如果报错下面的错则考虑加上@PowerMockIgnore
public class Demo {
	public static String setName(String name) {
		return name;
	}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(RateLimiterMetric.class)
@PowerMockIgnore({"javax.management.*", "javax.script.*"})
public class DemoTest {
	 @Test
    public void testSetName(){
		PowerMockito.mockStatic(Demo.class);
		String method = "hello";
		PowerMockito.when(Demo.setName(method)).thenReturn("hello");
	}
}

报错

  1. ERROR StatusLogger Unable to unregister MBeans
    java.lang.LinkageError: javax/management/MBeanServer(具体如下)
    解决:加上@PowerMockIgnore("javax.management.*)
D:\Java\java-1.8.0-openjdk-1.8.0.232-3\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:F:\Software\IntelliJ IDEA 2020.2.3\lib\idea_rt.jar=52348:F:\Software\IntelliJ IDEA 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath "F:\Software\IntelliJ IDEA 2020.2.3\lib\idea_rt.jar;F:\Software\IntelliJ IDEA 2020.2.3\plugins\junit\lib\junit5-rt.jar;F:\Software\IntelliJ IDEA 2020.2.3\plugins\junit\lib\junit-rt.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\charsets.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\access-bridge-64.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\cldrdata.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\dnsns.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\jaccess.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\localedata.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\nashorn.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\sunec.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\sunjce_provider.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\sunmscapi.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\sunpkcs11.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\ext\zipfs.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\jce.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\jsse.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\management-agent.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\resources.jar;D:\Java\java-1.8.0-openjdk-1.8.0.232-3\jre\lib\rt.jar;D:\UserData\admindesktop\课题\cloud_rpc_2.x_v1.0\cloud_rpc_2.x\cloud-rpc-core\target\test-classes;D:\UserData\admindesktop\课题\cloud_rpc_2.x_v1.0\cloud_rpc_2.x\cloud-rpc-core\target\classes;D:\UserData\admindesktop\课题\cloud_rpc_2.x_v1.0\cloud_rpc_2.x\cloud-rpc-common\target\classes;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-global-common-utils\2.1.0\cloud-global-common-utils-2.1.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\dropwizard\metrics\metrics-core\3.2.5\metrics-core-3.2.5.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\springframework\retry\spring-retry\1.2.5.RELEASE\spring-retry-1.2.5.RELEASE.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\springframework\spring-core\4.3.22.RELEASE\spring-core-4.3.22.RELEASE.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-global-common-checker\1.1.1\cloud-global-common-checker-1.1.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\softee\pojo-mbean\1.1\pojo-mbean-1.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\jolokia\jolokia-jvm\1.3.1\jolokia-jvm-1.3.1-agent.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\jolokia\jolokia-core\1.3.1\jolokia-core-1.3.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar;D:\UserData\admindesktop\课题\cloud_rpc_2.x_v1.0\cloud_rpc_2.x\cloud-rpc-remote\target\classes;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\logging\log4j\log4j-api\2.17.1\log4j-api-2.17.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\logging\log4j\log4j-core\2.17.0\log4j-core-2.17.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\logging\log4j\log4j-slf4j-impl\2.17.0\log4j-slf4j-impl-2.17.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-all\4.1.53.Final\netty-all-4.1.53.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\caucho\hessian\4.0.33\hessian-4.0.33.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\commons\commons-pool2\2.4.3\commons-pool2-2.4.3.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-global-api\1.9.3\cloud-global-api-1.9.3.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-global-common-errorcode\1.3.0\cloud-global-common-errorcode-1.3.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\thrift\libthrift\0.9.1\libthrift-0.9.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\json\json\20140107\json-20140107.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-dal-global-api\1.25.17\cloud-dal-global-api-1.25.17.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-dal-global-common\1.25.17\cloud-dal-global-common-1.25.17.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-global-common-rediscluster\1.4.0\cloud-global-common-rediscluster-1.4.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\redis\clients\jedis\2.9.3\jedis-2.9.3.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\netflix\hystrix\hystrix-core\1.5.12\hystrix-core-1.5.12.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\netflix\archaius\archaius-core\0.4.1\archaius-core-0.4.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\reactivex\rxjava\1.2.0\rxjava-1.2.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-global-common-kafka\3.1.0\cloud-global-common-kafka-3.1.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\kafka\kafka-clients\2.0.0\kafka-clients-2.0.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\lz4\lz4-java\1.4.1\lz4-java-1.4.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\xerial\snappy\snappy-java\1.1.7.1\snappy-java-1.1.7.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\kafka\kafka-streams\2.0.0\kafka-streams-2.0.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\kafka\connect-json\2.0.0\connect-json-2.0.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\kafka\connect-api\2.0.0\connect-api-2.0.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\javax\ws\rs\javax.ws.rs-api\2.1\javax.ws.rs-api-2.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\rocksdb\rocksdbjni\5.7.3\rocksdbjni-5.7.3.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\hibernate\hibernate-validator\4.2.0.Final\hibernate-validator-4.2.0.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\zookeeper\zookeeper\3.4.13\zookeeper-3.4.13.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\jline\jline\0.9.94\jline-0.9.94.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty\3.10.6.Final\netty-3.10.6.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\cloud-dubbo\1.1.0\cloud-dubbo-1.1.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\curator\curator-framework\2.12.0\curator-framework-2.12.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\curator\curator-client\2.12.0\curator-client-2.12.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\curator\curator-recipes\2.12.0\curator-recipes-2.12.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\netflix\hystrix\hystrix-javanica\1.5.12\hystrix-javanica-1.5.12.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\aspectj\aspectjrt\1.8.6\aspectjrt-1.8.6.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\aspectj\aspectjweaver\1.8.6\aspectjweaver-1.8.6.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\google\code\findbugs\jsr305\2.0.0\jsr305-2.0.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\netflix\hystrix\hystrix-metrics-event-stream\1.5.12\hystrix-metrics-event-stream-1.5.12.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\netflix\hystrix\hystrix-serialization\1.5.12\hystrix-serialization-1.5.12.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\fasterxml\jackson\module\jackson-module-afterburner\2.7.5\jackson-module-afterburner-2.7.5.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\zipkin\brave\brave-core\4.1.3\brave-core-4.1.3.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\zipkin\java\zipkin\1.19.2\zipkin-1.19.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\zipkin\reporter\zipkin-reporter\0.10.0\zipkin-reporter-0.10.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\zipkin\reporter\zipkin-sender-okhttp3\0.10.0\zipkin-sender-okhttp3-0.10.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\squareup\okhttp3\okhttp\3.8.0\okhttp-3.8.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\squareup\okio\okio\1.13.0\okio-1.13.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\dubbo\dubbo\2.7.11\dubbo-2.7.11.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\springframework\spring-context\5.2.8.RELEASE\spring-context-5.2.8.RELEASE.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\springframework\spring-aop\5.2.8.RELEASE\spring-aop-5.2.8.RELEASE.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\springframework\spring-beans\5.2.8.RELEASE\spring-beans-5.2.8.RELEASE.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\springframework\spring-expression\5.2.8.RELEASE\spring-expression-5.2.8.RELEASE.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\alibaba\spring\spring-context-support\1.0.10\spring-context-support-1.0.10.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\yaml\snakeyaml\1.20\snakeyaml-1.20.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\alibaba\fastjson\1.2.70\fastjson-1.2.70.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\nbu\common\pii\2.0.4\pii-2.0.4.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\bouncycastle\bcprov-jdk15on\1.68\bcprov-jdk15on-1.68.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\jayway\jsonpath\json-path\2.6.0\json-path-2.6.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\net\minidev\json-smart\2.4.7\json-smart-2.4.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\net\minidev\accessors-smart\2.4.7\accessors-smart-2.4.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\tplink\nbu\common\nbu-common-utils\2.0.4\nbu-common-utils-2.0.4.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\fasterxml\jackson\core\jackson-databind\2.9.10.6\jackson-databind-2.9.10.6.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\httpcomponents\httpcore\4.4.11\httpcore-4.4.11.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\ssm\2.13.51\ssm-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\aws-json-protocol\2.13.51\aws-json-protocol-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\fasterxml\jackson\core\jackson-core\2.10.4\jackson-core-2.10.4.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\protocol-core\2.13.51\protocol-core-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\sdk-core\2.13.51\sdk-core-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\profiles\2.13.51\profiles-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\reactivestreams\reactive-streams\1.0.2\reactive-streams-1.0.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\auth\2.13.51\auth-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\eventstream\eventstream\1.0.1\eventstream-1.0.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\http-client-spi\2.13.51\http-client-spi-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\regions\2.13.51\regions-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\fasterxml\jackson\core\jackson-annotations\2.10.4\jackson-annotations-2.10.4.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\annotations\2.13.51\annotations-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\utils\2.13.51\utils-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\aws-core\2.13.51\aws-core-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\apache-client\2.13.51\apache-client-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\apache\httpcomponents\httpclient\4.5.9\httpclient-4.5.9.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\software\amazon\awssdk\netty-nio-client\2.13.51\netty-nio-client-2.13.51.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-codec-http\4.1.46.Final\netty-codec-http-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-codec-http2\4.1.46.Final\netty-codec-http2-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-codec\4.1.46.Final\netty-codec-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-transport\4.1.46.Final\netty-transport-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-resolver\4.1.46.Final\netty-resolver-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-common\4.1.46.Final\netty-common-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-buffer\4.1.46.Final\netty-buffer-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-handler\4.1.46.Final\netty-handler-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-transport-native-epoll\4.1.46.Final\netty-transport-native-epoll-4.1.46.Final-linux-x86_64.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\netty\netty-transport-native-unix-common\4.1.46.Final\netty-transport-native-unix-common-4.1.46.Final.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\typesafe\netty\netty-reactive-streams-http\2.0.4\netty-reactive-streams-http-2.0.4.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\typesafe\netty\netty-reactive-streams\2.0.4\netty-reactive-streams-2.0.4.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\github\resilience4j\resilience4j-circuitbreaker\1.7.1\resilience4j-circuitbreaker-1.7.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\vavr\vavr\0.10.2\vavr-0.10.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\vavr\vavr-match\0.10.2\vavr-match-0.10.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\github\resilience4j\resilience4j-core\1.7.1\resilience4j-core-1.7.1.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\com\google\guava\guava\18.0\guava-18.0.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\easymock\easymock\3.2\easymock-3.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\cglib\cglib-nodep\2.2.2\cglib-nodep-2.2.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\easymock\easymockclassextension\3.2\easymockclassextension-3.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\powermock\powermock-module-junit4\2.0.7\powermock-module-junit4-2.0.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\powermock\powermock-module-junit4-common\2.0.7\powermock-module-junit4-common-2.0.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\powermock\powermock-reflect\2.0.7\powermock-reflect-2.0.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\net\bytebuddy\byte-buddy\1.9.10\byte-buddy-1.9.10.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\net\bytebuddy\byte-buddy-agent\1.9.10\byte-buddy-agent-1.9.10.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\powermock\powermock-core\2.0.7\powermock-core-2.0.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\powermock\powermock-api-mockito2\2.0.7\powermock-api-mockito2-2.0.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\powermock\powermock-api-support\2.0.7\powermock-api-support-2.0.7.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\mockito\mockito-core\2.28.2\mockito-core-2.28.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\javassist\javassist\3.27.0-GA\javassist-3.27.0-GA.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\net\sourceforge\findbugs\annotations\1.3.2\annotations-1.3.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\junit\junit\4.13\junit-4.13.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\org\slf4j\slf4j-api\1.7.2\slf4j-api-1.7.2.jar;D:\tools\Maven\apache-maven-3.5.3\maven_respository\io\prometheus\jmx\jmx_prometheus_javaagent\1.0.3\jmx_prometheus_javaagent-1.0.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.tplink.cloud.rpc.core.ratelimiter.RateLimiterFilterTest,testRateLimiterInvoke
ERROR StatusLogger Could not reconfigure JMX
 java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/javassist/JavassistMockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at org.powermock.core.classloader.javassist.JavassistMockClassLoader.loadUnmockedClass(JavassistMockClassLoader.java:90)
	at org.powermock.core.classloader.MockClassLoader.loadClassByThisClassLoader(MockClassLoader.java:104)
	at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.java:147)
	at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:98)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.apache.logging.log4j.core.jmx.Server.unregisterAllMatching(Server.java:337)
	at org.apache.logging.log4j.core.jmx.Server.unregisterLoggerContext(Server.java:261)
	at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:165)
	at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:141)
	at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:637)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:699)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:716)
	at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:270)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
	at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:137)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:55)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:33)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:270)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
	at com.tplink.cloud.rpc.core.invoker.AbstractClientInvoker.<clinit>(AbstractClientInvoker.java:27)
	at sun.reflect.GeneratedSerializationConstructorAccessor5.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
	at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
	at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:19)
	at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:47)
	at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
	at org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:41)
	at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
	at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
	at org.mockito.Mockito.mock(Mockito.java:1908)
	at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:108)
	at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.doCreateMock(DefaultMockCreator.java:61)
	at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:53)
	at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:40)
	at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:126)
	at com.tplink.cloud.rpc.core.ratelimiter.RateLimiterFilterTest.testRateLimiterInvoke(RateLimiterFilterTest.java:58)
	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.internal.runners.TestMethod.invoke(TestMethod.java:68)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
	at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
	at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
	at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
	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.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
stat.ratelimit.service.hello1
ERROR StatusLogger Unable to unregister MBeans
 java.lang.LinkageError: javax/management/MBeanServer
	at org.apache.logging.log4j.core.jmx.Server.unregisterAllMatching(Server.java:337)
	at org.apache.logging.log4j.core.jmx.Server.unregisterLoggerContext(Server.java:261)
	at org.apache.logging.log4j.core.jmx.Server.unregisterLoggerContext(Server.java:249)
	at org.apache.logging.log4j.core.LoggerContext.stop(LoggerContext.java:379)
	at org.apache.logging.log4j.core.LoggerContext$1.run(LoggerContext.java:320)
	at org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry$RegisteredCancellable.run(DefaultShutdownCallbackRegistry.java:119)
	at org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry.run(DefaultShutdownCallbackRegistry.java:78)
	at java.lang.Thread.run(Thread.java:748)

Process finished with exit code 0

  1. ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider jdk.nashorn.api.scripting.NashornScriptEngineFactory not a subtype(具体如下)
    解决:加上@PowerMockIgnore("javax.script.*)
    scriptenginemanager providers.next(): javax.script.scriptenginefactory: prov,问题,测试,spring boot,单元测试,java

文章来源地址https://www.toymoban.com/news/detail-554909.html

到了这里,关于非Springboot项目使用powermockito单元测试 mock静态方法和ScriptEngineManager providers.next()...not a subtype报错解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Junit】单元测试Mock静态方法

    开发依赖 版本 Spring Boot 3.0.6 JDK 20 如果没有引入 mockito-inline 这个依赖,使用mock静态方法,则会抛这个异常

    2024年02月04日
    浏览(35)
  • 在Java微服务项目中,如何使用Mock来进行单元测试?

    摘要: 在系统开发的过程中,单元测试是其中的一个重要环节。在Java微服务项目中,Spring框架本身就为我们提供了一套单元测试的框架SpringBootTest。如果我们在学校完成课堂作业或出于兴趣爱好自学,是可以使用Spring自带的单元测试框架进行单测的。 工作中,这种通过Spri

    2024年02月16日
    浏览(36)
  • junit单元测试mock私有private方法和静态static方法

    我们知道org.mockito.Mockito功能有限,不能mock 私有private、受保护的protected方法 org.powermock.api.mockito.PowerMockito更强大,支持对private和protected和static方法的mock 别忘记,首先要引入maven依赖 有如下私有方法需要mock 这时候可以利用PowerMockito的spy方法mock出方法所在的对象,然后利用

    2024年02月12日
    浏览(31)
  • junit单元测试mock常用写法-springboot与springmvc

    做单元测试时候,经常会mock一些方法,得到期望的返回值,这里记录一下常见的public、private、static方法,以及设计到的内部调用的方法 这里采用maven,引入powermock即可 所有单元测试的测试类都继成BaseTest即可 (1)springboot的示例 (2)springmvc的示例 有如下类需要编写单元测

    2024年02月02日
    浏览(40)
  • Java通过PowerMockito和Mokito进行单元测试

    PowerMockito和Mokito的概念 PowerMockito和Mockito都是Java语言中的测试框架,用于进行单元测试和集成测试。它们中的每一个都有不同的功能和应用。 Mockito 是一个基于模拟的测试框架。它允许你模拟对象,在测试中隔离被测代码的依赖项。使用Mockito,您可以轻松地设置模拟对象的行

    2024年02月11日
    浏览(32)
  • 【Java】使用PowerMockito mock static方法/new对象/mock对象的public或private方法的简单示例

    1.1 打桩类的public static方法 测试用例中如果需要对public静态方法的打桩,针对测试类增加注解@RunWith(PowerMockRunner.class)同时针对静态方法所在的类增加注解@PrepareForTest({StaticMethod.class}),接着在测试用例调用方法之前增加 PowerMockito.mockStatic(StaticMethod.class); PowerMockito.when(StaticMet

    2024年01月24日
    浏览(41)
  • 【PowerMockito:编写单元测试过程中采用when打桩失效的问题】

    正如上图所示,采用when打桩了,但是,实际执行的时候还是返回null。 打桩时直接用any() 但是这样可能出现一个mybatisplus的异常,所以在测试类中需要加入以下代码片段:

    2024年02月02日
    浏览(34)
  • 测试人必会的Python内置库:unittest.mock(单元测试mock的基础使用)

    unittest.mock是用于在单元测试中模拟和替换指定的对象及行为,以便测试用例更加准确地进行测试运行。例如对于以下代码,想要针对函数func_a写一个简单的单元测试: 但是这样的话,函数func_b和func_c的逻辑都需要一起测试,在单元测试中这明显是不合理的,对于想要测试的

    2024年02月01日
    浏览(44)
  • C++类对象单元测试中的Mock使用

      在进行单元测试时,我们想要测试自己缩写 函数A ,但是 函数A 却依赖于 函数B ,当 函数B 无法满足预期时就无法对 函数A 进行测试,主要由于下面几个原因: 函数B 依赖于硬件设备 真实的 函数B 的返回值无法满足我们的预期 团队开发中 函数B 尚未实现   这时就需要

    2023年04月15日
    浏览(41)
  • python技术栈 之 单元测试中mock的使用

    一、什么是mock? mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。 二、mock的作用 特别是开发过程中上下游未完成的工序导致当前无法测试,需要虚拟某些特定对象以便测试。 unittest是python内置的单元测

    2024年02月13日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包