Skip to content

Commit

Permalink
Merge pull request #10 from cstamas/junit-412
Browse files Browse the repository at this point in the history
Fix for JUnit 4.12 that renamed internal fields
  • Loading branch information
lucaspouzac committed Apr 15, 2015
2 parents 7d75a6c + fe2e781 commit bc605ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</developers>

<properties>
<version.junit>4.11</version.junit>
<version.junit>4.12</version.junit>
<version.wagon-ftp>1.0-alpha-6</version.wagon-ftp>
<version.maven-resources-plugin>2.6</version.maven-resources-plugin>
<version.maven-site-plugin>3.3</version.maven-site-plugin>
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/org/databene/contiperf/junit/ContiPerfRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ public Statement apply(Statement base, FrameworkMethod method, Object target) {
try {
if (base instanceof RunAfters) {
runAfters = (RunAfters) base;
Field fNext = base.getClass().getDeclaredField("fNext");
Field fNext = getFieldNextWithJunit412Fallback(base);
fNext.setAccessible(true);
base = (Statement) fNext.get(base);
} else if (base instanceof RunBefores) {
runBefores = (RunBefores) base;
Field fNext = base.getClass().getDeclaredField("fNext");
Field fNext = getFieldNextWithJunit412Fallback(base);
fNext.setAccessible(true);
base = (Statement) fNext.get(base);
}
Expand All @@ -221,14 +221,14 @@ public Statement apply(Statement base, FrameworkMethod method, Object target) {
try {
// if runBefores has been removed, reapply it
if (runBefores != null) {
Field fNext = runBefores.getClass().getDeclaredField("fNext");
Field fNext = getFieldNextWithJunit412Fallback(runBefores);
fNext.setAccessible(true);
fNext.set(runBefores, base);
base = runBefores;
}
// if runAfters has been removed, reapply it
if (runAfters != null) {
Field fNext = runAfters.getClass().getDeclaredField("fNext");
Field fNext = getFieldNextWithJunit412Fallback(runAfters);
fNext.setAccessible(true);
fNext.set(runAfters, base);
base = runAfters;
Expand All @@ -239,6 +239,20 @@ public Statement apply(Statement base, FrameworkMethod method, Object target) {
return base;
}

/**
* Helper that uses reflection to get a field, but applies a fallback as field name changed in JUnit 4.12
* from legacy named "fNext" to "next".
*
* @see <a href="https://github.com/junit-team/junit/commit/df00d5eced3a7737b88de0f6f9e3673f0cf88f88">https://github.com/junit-team/junit/commit/df00d5eced3a7737b88de0f6f9e3673f0cf88f88</a>
*/
private Field getFieldNextWithJunit412Fallback(final Statement base) throws NoSuchFieldException{
try {
return base.getClass().getDeclaredField("fNext");
} catch (NoSuchFieldException e) {
return base.getClass().getDeclaredField("next");
}
}

public ReportContext getContext() {
return context;
}
Expand Down

0 comments on commit bc605ed

Please sign in to comment.