This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Testing and the fly likes Test class should have exactly one public constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Test class should have exactly one public constructor" Watch "Test class should have exactly one public constructor" New topic
Author

Test class should have exactly one public constructor

Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

Hi,
I'm having a strange issue when running one of my tests, I get the exception:

java.lang.Exception: Test class should have exactly one public constructor

which is odd because my test has just one:



so I can only assume that something else is either stripping out the constructor or adding another in at compile time, but what and why, and is there anything i can do about it?

Willy
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That is indeed strange. Can you show the stacktrace, please?

As an aside, your test method could simply throw the SqlException.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

Here you go...

java.lang.Exception: Test class should have exactly one public constructor at org.junit.runners.BlockJUnit4ClassRunner.validateOnlyOneConstructor(BlockJUnit4ClassRunner.java:123) at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:116) at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:111) at org.junit.runners.ParentRunner.validate(ParentRunner.java:253) at org.junit.runners.ParentRunner.<init>(ParentRunner.java:55) at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:56) at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24) at junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:31) at junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:24) at sun.reflect.GeneratedConstructorAccessor17.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:386) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:1339) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:831) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1785) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:785) at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288) at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.Target.execute(Target.java:357) at org.apache.tools.ant.Target.performTasks(Target.java:385) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337) at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38) at org.eclipse.ant.internal.ui.antsupport.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30) at org.apache.tools.ant.Project.executeTargets(Project.java:1189) at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:416) at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.Target.execute(Target.java:357) at org.apache.tools.ant.Target.performTasks(Target.java:385) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337) at org.apache.tools.ant.Project.executeTarget(Project.java:1306) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) at org.apache.tools.ant.Project.executeTargets(Project.java:1189) at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423) at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You seem to be using Ant? Can you show us the Ant task?

What happens if you run the test using the Eclipse test runner?
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

It runs fine through Eclipse which is strange..

here's the Ant target

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
How do you know that it's that class that's causing the problem?
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

the tests print out a report of all the failures
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

Actually something very odd is happening, it looks like it is interpretting the Expectations as seperate tests and failing them because they don't have a constructor, is this normal??






[Thumbnail for test.JPG]

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Oh right - look at your include:

include name="**/Test*.class"

That includes the anonymous inner classes.

I've never seen that happening before, because our coding convention is to have test classes *end* with "Test" - which elegantly circumvents this problem

You should probably add an

exclude name="**/*$*.class"

to your fileset.
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

Doh!

Thanks for your help, this has been driving me mad all day!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You're welcome! I know how these things can drive you insane...
 
Consider Paul's rocket mass heater.
 
subject: Test class should have exactly one public constructor
 
Similar Threads
Accessing variable from another method
jmock query
Beginning JMock. does this test anything?
Help needed for setting JMock
Doubt with JMock + JUnit [finding a better way to do it]