Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NoSuchMethodError:在类中添加构造函数后_Java_Intellij Idea - Fatal编程技术网

java.lang.NoSuchMethodError:在类中添加构造函数后

java.lang.NoSuchMethodError:在类中添加构造函数后,java,intellij-idea,Java,Intellij Idea,有人能帮我理解为什么在向一个包含两个参数的类添加新构造函数后,项目会编译,但我的测试现在无法创建这个类的新实例 我得到一个例外: java.lang.NoSuchMethodError: TestClass.(Lcom/pidac/infrastructure/core/service/CrudService;Lcom/pidac/infrastructure/core/service/AuditService;)V 在 pidac.workflow.SomeTests.shouldAuditC

有人能帮我理解为什么在向一个包含两个参数的类添加新构造函数后,项目会编译,但我的测试现在无法创建这个类的新实例

我得到一个例外:

java.lang.NoSuchMethodError: TestClass.(Lcom/pidac/infrastructure/core/service/CrudService;Lcom/pidac/infrastructure/core/service/AuditService;)V

在 pidac.workflow.SomeTests.shouldAuditCompletedIdentityRequestItemsAgainstConnectedSources(SomeClass.java:115) 位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.lang.reflect.Method.invoke(Method.java:498) org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)位于 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)位于 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)位于 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)位于 org.junit.runners.ParentRunner.run(ParentRunner.java:363)位于 org.junit.runner.JUnitCore.run(JUnitCore.java:137)位于 JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 在 intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我正在使用IntelliJ 2018 professional

更多信息12月29日: 我已经删除了这个项目的maven本地快照并重建了它


当我调试测试时,我可以看到这个类有这个新的构造函数。然而,调试器正在执行的内容与我在IntelliJ中看到的内容之间显然存在不匹配

当您用2个参数声明一个构造函数时,您的类将丢失其默认构造函数(无参数)。您正在编写的测试可能需要一个默认构造函数来获取实例

构建另一个显式无参数构造函数,或者如果您正在使用优秀的Lombok库,请使用
@noargsconstuctor
注释该类以使测试正常工作。或者,将测试更改为使用构造函数中的2个属性初始化类

正如Nicholas所提到的,来自您的代码示例将有所帮助


这里的另一个SO问题解释了默认构造函数:

NoSuchMethodError发生在编译时存在的方法在运行时不再存在时


您编译的类文件已过期。重建项目。

我必须手动删除当前在
[module]/out/production/classes
中配置的gradle输出文件夹

我的项目为输出配置了这个文件夹,所以当我查看其中编译的类时,没有反映出新的更改。所以我认为这些编译过的类没有被更新,只是被删除了


重新组装了项目,我的测试现在可以看到更改,即使我在上面的位置没有看到编译的类……

你能分享你的代码吗?我不能分享代码。然而,问题的解释是准确地表示问题。我需要弄清楚IntelliJ从哪里运行测试,因为我已经清理了。我已经在整个解决方案上运行了clean Assembly install。此外,我的测试与源代码在同一个项目(模块)中。我们使用的是Maven约定,不需要根据我的经验重建项目。默认构造函数没有被删除。