2

Could someone please help me understand why after adding a new constructor to a class which takes two parameters, the project compiles but my test now cannot create a new instance of this class?

I get this exception:

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

at pidac.workflow.SomeTests.shouldAuditCompletedIdentityRequestItemsAgainstConnectedSources(SomeClass.java:115) 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:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I am using IntelliJ 2018 professional.

More Info Dec 29: I have deleted the maven local snapshot for this project and rebuilt.

When I debug the test, I can see that the class has this new constructor. However, there is obviously a mismatch between what the debugger is executing and what I see in IntelliJ.

Klaus Nji
  • 18,107
  • 29
  • 105
  • 185

3 Answers3

1

NoSuchMethodError occurs when a method that was present at compile time is no longer present at runtime.

Your compiled class files are out of date. Rebuild your project.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • I have run clean-assemble-install on the whole solution. Also, my tests are in the same project (module) as source. We are using Maven convention which should not require project be rebuilt from my experience. – Klaus Nji Dec 29 '18 at 15:55
1

I had to manually delete the gradle output folder currently configured in [module]/out/production/classes.

My project has this folder configured for output so when I looked at the compiled classes in there, the new changes were not reflected. So I figured this compiled classes were not being updated and just deleted them.

Re-assembled the project and my tests are now able to see the changes even though I do not see the compiled classes in the location above....

Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
0

Your class loses its default constructor (no args) when you declare one with 2 args. The test you are writing probably expects a default constructor to get an instance.

Build another explicit no-args constructor or if you're using the excellent Lombok library, annotate the class with @NoArgsConstructor to get your tests working. Alternatively, change your test to initialize the class with 2 attributes in the constructor.

As mentioned by Nicholas, a code sample from you will help.

Default constructor is explained in another SO question here: Java default constructor

swapz83
  • 422
  • 4
  • 11