java - wrong junit test names -
java - wrong junit test names -
i seek write own junit runner , stuck @ returning proper test description.
public class parameterizedwrapper extends suite { private list<runner> frunners; /** * @throws throwable * */ public parameterizedwrapper(final class<?> clazz) throws throwable { super(clazz, collections.<runner>emptylist()); frunners = constructrunners(getparametersmethod()); } protected list<runner> constructrunners(final frameworkmethod method) throws exception, throwable { @suppresswarnings("unchecked") iterable<object[]> parameters = (iterable<object[]>) getparametersmethod().invokeexplosively(null); arraylist<runner> runners = new arraylist<runner>(); int index = 0; (object[] parameter : parameters) { class<?> testclass = gettestclass().getjavaclass(); wrappedrunner wrappedrunner = testclass.getannotation(wrappedrunner.class); runner runner = wrappedrunner.value().getconstructor(class.class).newinstance(gettestclass().getjavaclass()); runners.add(new wrappingrunner(runner, parameter, testclass, index++)); } homecoming runners; } private frameworkmethod getparametersmethod() throws exception { list<frameworkmethod> methods = gettestclass().getannotatedmethods(parameters.class); (frameworkmethod each : methods) { if (each.isstatic() && each.ispublic()) { homecoming each; } } throw new exception("no public static parameters method on class " + gettestclass().getname()); } @override protected list<runner> getchildren() { homecoming frunners; } @retention(retentionpolicy.runtime) @target({elementtype.type}) public static @interface wrappedrunner { class<? extends runner> value(); } @retention(retentionpolicy.runtime) @target({elementtype.method}) public static @interface parametersetter { } } class wrappingrunner extends runner { private runner wrappedrunner; private object[] parameters; private class<?> testclass; private int testposition; public wrappingrunner(final runner runner, final object[] params, final class<?> clazz, final int position) { wrappedrunner = runner; parameters = params; testclass = clazz; testposition = position; } @override public description getdescription() { description originaldescription = wrappedrunner.getdescription(); description newdescription = description.createsuitedescription(namefor(""), new annotation[0]); (description kid : originaldescription.getchildren()) { newdescription.addchild(decoratechilddescription(child)); } homecoming newdescription; } private string namefor(string name) { homecoming string.format("%1$s[%2$s]", name, testposition); } protected description decoratechilddescription(final description originalchilddescription) { description d = description.createtestdescription(originalchilddescription.gettestclass(), namefor(originalchilddescription.getmethodname()), originalchilddescription.getannotations().toarray(new annotation[0])); homecoming d; } @override public void run(final runnotifier notifier) { seek { parameterstorage.storeparameters(testclass, parameters); wrappedrunner.run(notifier); } { parameterstorage.clearparameters(testclass); } } }
i have test class check if runner works. runner works fine except tests named weirdly. in eclipse displays tests , adds unrooted tests category
and surefire not utilize naming @ all:
i compared description objects generated in runner , in parameterized
runner, there seems no difference.
it's bit ugly, it's safer pass list of kid runners parent constructor:
public parameterizedwrapper(final class<?> clazz) throws throwable { super(clazz, constructrunners(getparametersmethod()); } private static list<runner> constructrunners(final frameworkmethod method) throws throwable { ...
you shouldn't need override suite.getchildren()
java junit junit4
Comments
Post a Comment