Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use PowerMockRunner with JUnit versions including characters #401

Open
johanhaleby opened this issue Jul 24, 2015 · 12 comments
Open

Comments

@johanhaleby
Copy link
Collaborator

From johan.ha...@gmail.com on April 06, 2012 09:31:22

E.g. for JUnit 4.9b2:

ava.lang.NumberFormatException: For input string: "9b2"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:
48)
at java.lang.Integer.parseInt(Integer.java:458)
at java.lang.Short.parseShort(Short.java:120)
at java.lang.Short.parseShort(Short.java:78)
at
org.powermock.modules.junit4.common.internal.impl.VersionCompatibility.getJUnitVersion(VersionCompatibility.java:
38)
at
org.powermock.modules.junit4.PowerMockRunner.(PowerMockRunner.java:
31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:
39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:
27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at
org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:
31)
at
org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:
24)
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
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)

Original issue: http://code.google.com/p/powermock/issues/detail?id=381

@johanhaleby
Copy link
Collaborator Author

From mahoney...@gmail.com on November 06, 2012 17:12:18

For obvious reasons this also affects junit 4.11-beta-1, which I am keen to use as it is the first version that does not include the hamcrest classes in the jar, which introduces all sorts of potential nasty runtime issues.

@johanhaleby
Copy link
Collaborator Author

From mahoney...@gmail.com on November 06, 2012 17:15:53

Incidentally the code in VersionCompatibility.getJUnitVersion makes no sense at all - there's a try block around a simple return statement with no logic in it! It cannot possibly throw an exception.

@johanhaleby
Copy link
Collaborator Author

From mahoney...@gmail.com on November 06, 2012 17:37:24

A simple solution would be to not try and parse this yourself, and instead use the following maven dependency:

\<dependency>
  \<groupId>org.apache.maven</groupId>
  \<artifactId>maven-artifact</artifactId>
  \<version>3.0.4</version>
\</dependency>

You can then get the major & minor versions as follows:
ArtifactVersion artifactVersion = new DefaultArtifactVersion( Version.id() );
int majorVersion = artifactVersion.getMajorVersion();
int minorVersion = artifactVersion.getMinorVersion();

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on November 06, 2012 21:07:24

It seems rather overkill to bring in a dependency just to parse the version. Please help out and provide a patch and I'll include it in the next release that will be released soon!

@johanhaleby
Copy link
Collaborator Author

From mahoney...@gmail.com on November 12, 2012 16:23:33

Patch attached. It seems to me much more sensible to use well tested, working code that does not have to be maintained in this project.

Attachment: allow_junit_4_11_beta_1.patch

@johanhaleby
Copy link
Collaborator Author

From mahoney...@gmail.com on November 12, 2012 16:45:20

Better patch - fixed my broken indentation of a pom & mildly cleaned up PowerMockRunner.

Attachment: allow_junit_4_11_beta_1.2.patch

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on November 12, 2012 22:17:47

I'm sorry but I cannot accept this patch since it uses the "maven-artifact" dependency. I don't think it's appropriate to depend on a new framework just for parsing a version number. This, I believe, has to be fixed without an additional dependency.

@johanhaleby
Copy link
Collaborator Author

From mahoney...@gmail.com on November 12, 2012 23:08:19

FWIW I hadn't seen your comment before producing the first patch.

"I don't think it's appropriate to depend on a new framework"

Why not? What's the downside? Just about everyone uses a dependency management system now, so no-one will ever even notice.

The upside is that there are now fewer lines of untested & provenly not working code in powermock that are actually preventing powermock working for people using the latest release of JUnit right now.

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on November 13, 2012 01:16:59

Well one of the downsides is that you may end up with conflicting dependencies. So let's say that you're already using a dependency of another version then you may end up with different API depending which one you put first in classpath and stuff may go really bad. Spring, for example, has troubles with this since they (used) to depend on junit (with dependencies) which pulled in transitive dependencies that could conflict with the things you already were using. This is things you want to avoid. I've ran into similar situation many times with frameworks depending on different versions of HTTP Client or Hamcrest. Now I wouldn't suggest that you write your own HTTP Client framework instead of using it as a dependency but for smaller things (and parsing a version string is relatively small thing) I think it's better to have control over it yourself and you avoid classpath collisions.

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on November 13, 2012 01:18:40

There's for example a lot of exemples on http://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java on how to parse versions in Java.

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on November 13, 2012 01:20:49

A third option would perhaps be to incorporate Mavens code in PowerMock if it's not too big and its licence allows for it.

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on November 13, 2012 01:55:35

Ok so I've no given this a go myself. Please check out and build PowerMock from trunk (ignore tests if using Java 7) and give it a spin and see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant