check out my little experiment at http://code.google.com/p/jbehave-junit-monitor/
and give me some feedback!
@RunWith(MyTargetTestClass.TheRunner.class)
public class MyTargetTestClass {
static int count = 0;
public boolean doStuff() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
count ++;
if(count % 2 == 0) { throw new RuntimeException("A Failure"); }
return !(count % 3 == 0);
}
public static class TheRunner extends Runner {
Listdescriptions = new ArrayList ();
private final Class<? extends MyTargetTestClass> testClass;
private final MyTargetTestClass testContainingInstance;
private Description testSuiteDescription;
public TheRunner(Class<? extends MyTargetTestClass> testClass) {
this.testClass = testClass;
testContainingInstance = reflectMeATestContainingInstance(testClass);
testSuiteDescription = Description.createSuiteDescription("All my stuff is happening now dudes");
testSuiteDescription.addChild(createTestDescription("first bit happening"));
testSuiteDescription.addChild(createTestDescription("second bit happening"));
testSuiteDescription.addChild(createTestDescription("third bit happening"));
}
@Override
public Description getDescription() {
return testSuiteDescription;
}
@Override
public void run(RunNotifier notifier) {
for(Description description : testSuiteDescription.getChildren()) {
notifier.fireTestStarted(description);
try {
if(testContainingInstance.doStuff()) {
notifier.fireTestFinished(description);
}
else {
notifier.fireTestIgnored(description);
}
} catch (Exception e) {
notifier.fireTestFailure(new Failure(description, e));
}
}
}
private MyTargetTestClass reflectMeATestContainingInstance(Class<? extends MyTargetTestClass> testClass) {
try {
return testClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Description createTestDescription(String description) {
return Description.createTestDescription(testClass, description);
}
}
}
@RunWith(MyRunner.class)
public class UnusualAndRareProblemTest {
...
..
.
}
public static class MyRunner extends JUnit4ClassRunner {
public MyRunner(Class klass) throws InitializationError {
super(klass);
}
@Override
public void run(final RunNotifier notifier) {
for(int i=0; i<1000; i++) {
super.run(notifier);
}
}
}
class PageBeforeRefactor {
public Tag getBadgeTag() {
Tag badgeTag = getBadgeTag(getImpliedSeries());
if (badgeTag != null) {return badgeTag;}
badgeTag = getFirstBadgeTag(getImpliedKeywords());
if (badgeTag != null) {return badgeTag;}
badgeTag = getImpliedContributor();
if (badgeTag != null) {return badgeTag;}
badgeTag = getBadgeTag(getImpliedBookSection());
if (badgeTag != null) {return badgeTag;}
badgeTag = getBadgeTag(getImpliedBook());
if (badgeTag != null) {return badgeTag;}
badgeTag = getFirstBadgeTag(getImpliedBlogs());
return badgeTag;
}
private Tag getBadgeTag(Tag tag) {
if (tag != null) {
return tag.getBadge()==null?null:tag;
}
return null;
}
private Tag getFirstBadgeTag(List tags) {
for (Tag tag : tags) {
if (!keywordClassifier.isFootballClub(tag) && tag.getBadge() != null) {
return tag;
}
}
return null;
}
}
class PageAfterRefactor {
public Tag getBadgeTag() {
return new PriorityOrderedBadgeFinder()
.check(getImpliedBlogs())
.check(getImpliedSeries())
.check(getImpliedKeywords())
.check(getImpliedContributor())
.check(getImpliedBookSection())
.check(getImpliedBook())
.getBadgeTag();
}
class PriorityOrderedBadgeFinder {
Tag badgeTag;
public PriorityOrderedBadgeFinder check(Tag t) {
if(badgeTag == null && t!=null && !keywordClassifier.isFootballClub(t) && .getBadge()!=null) {
badgeTag = t;
}
return this;
}
public PriorityOrderedBadgeFinder check(List tags) {
for (Tag tag : tags) {
check(tag);
}
return this;
}
public Tag getBadgeTag() {
return badgeTag;
}
}
}