I tried to find unused steps and ended up with overriding a method of 
PerformableTree.

This is quita a hack but found 250 unused steps in our code base ;-)

Is there any better method of doing this?

    @Override
    public void addStories(RunContext context, List<Story> stories) {
        super.addStories(context, stories);

        Set<Method> usedMethods = new HashSet<>();
        Set<Method> existingMethods = new HashSet<>();

        List<CandidateSteps> candidateSteps = (List<CandidateSteps>) 
ReflectionTestUtils.getField(context, "candidateSteps");

        List<StepCandidate> collectCandidates = context.configuration().
stepFinder().collectCandidates(candidateSteps);
        for (StepCandidate stepCandidate : collectCandidates) {
            existingMethods.add(stepCandidate.getMethod());
        }

        for (PerformableStory story : getRoot().getStories()) {
            for (PerformableScenario scenario : story.getScenarios()) {
                NormalPerformableScenario sc = (NormalPerformableScenario) 
ReflectionTestUtils.getField(scenario, "normalScenario");
                if (sc != null) {
                    PerformableSteps ps = (PerformableSteps) 
ReflectionTestUtils.getField(sc, "steps");
                    List<Step> steps = (List<Step>) ReflectionTestUtils.
getField(ps, "steps");
                    for (Step step : steps) {
                        if (step instanceof StepCreator.ParametrisedStep) {
                            usedMethods.add((Method) ReflectionTestUtils.
getField(step, "method"));
                        }
                    }
                }
                List<ExamplePerformableScenario> ec = (List<
ExamplePerformableScenario>) ReflectionTestUtils.getField(scenario, 
"exampleScenarios");
                if (ec != null) {
                    for (ExamplePerformableScenario eps : ec) {
                        PerformableSteps ps = (PerformableSteps) 
ReflectionTestUtils.getField(eps, "steps");
                        List<Step> steps = (List<Step>) ReflectionTestUtils.
getField(ps, "steps");
                        for (Step step : steps) {
                            if (step instanceof StepCreator.ParametrisedStep
) {
                                usedMethods.add((Method) ReflectionTestUtils
.getField(step, "method"));
                            }
                        }
                    }
                }
            }
        }

        System.out.println(
"#########################################################");
        System.out.println("existing " + existingMethods.size());

        existingMethods.removeAll(usedMethods);

        System.out.println("leftover " + existingMethods.size());

        for (Method leftover : existingMethods) {
            System.out.println(leftover.toString());
        }

        System.out.println(
"#########################################################");
   
    }



-- 
You received this message because you are subscribed to the Google Groups 
"JBehave User" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send an email to [email protected].
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/jbehave-user/b84cc7a5-ba6a-4e1e-b07c-0b3df8385e32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to