Mockito异常:基础异常:java.lang.IollegalArgumentException:无法创建类型



我要做的事情

我正在尝试使用cucumber测试我的类,并对其进行扩展报告。我在其中得到了2个错误,1个与Mockito有关,这是高优先级的,2个与扩展报告有关。

我的代码

public class ClassA{
private DataService dataService;
public int methodA(String dataA, String dataB, String dataC){
int x;
//do Something
return x;
}
}
public class ClassATest{
@InjectMock
private ClassA classA;
@Mock
private DataService dataService;
@BeforeClass
@io.cucumber.java.Before
public void setUp() throws NoSuchFieldException, SecurityException, Exception {
System.out.println("---- Before ----");
MockitoAnnotations.initMocks(this);

int value = 10;
Mockito.when(dataService.methodA(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(value);


}
}

我的pom.xml

<!-- cucumber dependencies -->

<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>



<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.13.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Mockito -->
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.2.7</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy-agent -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.0.3</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.objenesis/objenesis -->
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>4.8.1</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports-cucumber4-adapter -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports-cucumber4-adapter</artifactId>
<version>1.0.12</version>
<exclusions>
<exclusion>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
</exclusion>
<exclusion>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>

<!-- CUCUMBER PLUGINS -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>test-ng</projectName>
<!-- optional, per documentation set this to "true" to bypass generation 
of Cucumber Reports entirely, defaults to false if not specified -->
<skip>false</skip>
<!-- output directory for the generated report -->
<outputDirectory>target/cucumber-reports/advanced-reports</outputDirectory>
<cucumberOutput>target/cucumber-reports/cucumber.json</cucumberOutput>
<buildNumber>1</buildNumber>
<parallelTesting>false</parallelTesting>
<!-- optional, defaults to outputDirectory if not specified -->
<inputDirectory>${project.build.directory}/cucumber-reports</inputDirectory>
<jsonFiles>
<!-- supports wildcard or name pattern -->
<param>**/*.json</param>
</jsonFiles>
<!-- optional, defaults to outputDirectory if not specified -->
<classificationDirectory>${project.build.directory}/cucumber-reports</classificationDirectory>
<checkBuildResult>false</checkBuildResult>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

我尝试过的东西

  1. 使用Mockito.mock()而不是注释更改mock的方式
  2. 更改Mockito依赖关系版本
  3. 正在添加Byte Buddy依赖项
  4. 正在更改字节伙伴依赖关系版本

我关于Mockito的错误##


Mockito只能模拟非私有&非期末课程。如果你不是请向邮件列表报告您收到此错误的原因。

Java:1.8 JVM供应商名称:Oracle Corporation JVM供应商版本:25.231-b11 JVM名称:Java HotSpot(TM)64位服务器虚拟机JVM版本:1.8.0_231-b11 JVM信息
:混合模式操作系统名称:Windows 10操作系统版本:10.0

基础异常:java.lang.IollegalArgumentException:无法在创建类型com.persivia.ptf.notes.customer.test.NotesProcessThreadTest.setUp(NotesProcessThreadTest.java:56)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)黄瓜.runtime.Utils$1.call(Utils.java:26)黄瓜.runtime.Timeout(Timeout.java:16)黄瓜.runtime.Utils.invoke(Utils.java:20)cucument.runtime.java。JavaHookDefinition.execute(JavaHookDefinition.java:65)在黄瓜.runner.HookDefinitionMatch.runStep(HookDefinitionMatch.java:16)在黄瓜.runner.TestStep.executeStep(TestStep.java:65)黄瓜.runner.TestStep.run(TestStep.java:50)黄瓜.runner.TestCase.run(TestCase.java:42)黄瓜.runner.runPickle(runner.java:50)io.cuckle.testng.TestNGCucumberRunner.runScenario(TestNGCucumber Runner.java:79)在io.cuckle.testng.AbstractTestNGCucumberTests.runScenario(AbstractTestNGcucumberTest.java:24)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelpr.java:133)位于org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)在org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)在org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)在org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)在org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)位于java.util.ArrayList.forEach(ArrayList.java:1257)org.testng.TestRunner.privateRun(TestRunner.java:794)org.testng.TestRunner.run(TestRunner.java:596)org.testng.SuiteRunner.runTest(SuiteRunner.java:377)org.testng.SuiteRunner.runSequencely(SuiteRunner.java:371)org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)org.testng.SuiteRunner.run(SuiteRunner.java:276)org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)org.testng.testng.runSuitesSequencely(testng.java:1212)org.testng.testng.runSuitesLocally(testng.java:1134)org.testng.testng.runSuites(testng.java:1063)org.testng.testng.run(testng.java:1031)org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)位于org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)引起发件人:java.lang.IollegalArgumentException:无法在处创建类型net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:140)位于net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)位于的net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)在org.mockito.internal.create.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachinyBytecodeGenerator.java:32)在org.mockito.internal.create.bytebuddy.Subclass ByteBuddyMockMaker.createMockType(Subclass Byte巴迪MockMaker.java:71)在org.mockito.internal.create.bytebuddy.Subclass ByteBuddyMockMaker.createMock(Subclass Byte巴迪MockMaker.java:42)在org.mockito.internal.create.bytebuddy.ByteBuddyMockMaker.createMock(bytebuddy-MockMaker.java:25)网址:org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)网址:org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)org.mockito.Mokkito.mokk(mockito.java:1908)org.mockito.internal.configuration.MockAnnotationProcessor.processAnnotationForMock(MockAnnuationProcessor.java:44)在org.mockito.internal.configuration.MockAnnotationProcesser.process(MockAnnuationProcesser.java:19)在org.mockito.internal.configuration.MockAnnotationProcesser.process(MockAnnuationProcesser.java:16)在org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:38)在org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:62)在org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:49)在org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)在org.mockito.MockitoAnnotations.initMocks(MockitoAnnotation.java:69)…44更多原因:java.lang.NoSuchMethodError:net.bytebuddy.dynamic.loading.MultipleParentClassLoader$Builder.appendMostSpecific(Ljava/util/Collection;)Lnet/bytebuddy/dynamic/loading/MultipleParentClassLoader$Builder;在org.mockito.internal.create.bytebuddy.Subclass BytecodeGenerator.mockClass(Subclass ByTecodeGenerator.java:83)在org.mockito.internal.create.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachinyBytecodeGenerator.java:37)在org.mockito.internal.create.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachinyBytecodeGenerator.java:34)在net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)。。。62更多


我关于扩展区报告的错误


信息:意外错误net.masterthought.ucumber.ValidationException:未添加任何报告文件!在net.masterthing.cucump.ReportParser.parseJsonFiles(ReportParser.java:62)在net.masterthink.cucump.ReportBuilder.generateReports(ReportBuilder.java:88)在net.masterthink.cucumber.CCucumberReportGeneratorMojo.execute(CucumberReportGeneratorMojo.java:209)在org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)在org.apache.maven.hifecycle.internal.MojoExecution.execute(MojoExecutitor.java:210)在org.apache.maven.hifecycle.internal.MojoExecution.execute(MojoExecutitor.java:156)在org.apache.maven.hifecycle.internal.MojoExecution.execute(MojoExecutitor.java:148)在org.apache.maven.hifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModelBuilder.java:117)在org.apache.maven.hifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModelBuilder.java:81)在org.apache.maven.hifecycle.internal.builder.ssinglethreaded.SingleThreaddBuilder.build(SingleThreaddBBuilder.java:56)在org.apache.maven.hifecycle.internal.LifecycleStarter.execute(LifecycleSstarter.java:128)在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)org.apache.maven.cli.MavenCli.main(MavenCli.java:193)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)org.codehaus.cluster.classworlds.launcher.launcher.launchEnhanced(launcher.java:282)在org.codehaus.cluster.classworlds.launcher.launcher.launch(launcher.java:225)在org.codehaus.cluster.classworlds.launcher.launcher.mainWithExitCode(launcher.java:406)在org.codehaus.cluster.classworlds.launcher.launcher.main(launcher.java:347)


我不熟悉cucumber,所以我不确定这个解决方案是否可以转移到您的用例中。我的示例是使用JUnit5编写的,因此有一些不同的导入
我错过了您的DataService类以及它如何依赖ClassA,所以我不得不进行假设/简化。

public class DataService {
public int methodA(String a, String b, String c) {
return Integer.MIN_VALUE;
}
}
public class ClassA {
private DataService dataService;
public int methodA(String a, String b, String c) {
int x;
x = dataService.methodA(a, b, c);
return x;
}
}

最后测试

public class ClassATest {
@InjectMock
private ClassA classA;
@Mock
private DataService dataService;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
Mockito.when(dataService.methodA(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(1);
}
@Test
void test() {
Assertions.assertEquals(1, dataService.methodA("", "", ""));
}
}

因此,与您的版本的主要区别在于,mocking是在每个方法之前初始化的,而不是在类之前初始化的。

最新更新