KentBeck/junit — Issue #335: Bug: Categorized test methods inherit categories declared on type of a test.
Author: Tibor17
Created: 2011-10-10 01:24:15+00:00
State: open

The problem is in method
categories(:Description): List
which makes a join of both child and parent.

The Fix should be like this:
If parent has @Category annotation and the child does not, then inherit it;
Otherwise use the annotation on the child only.

https://github.com/KentBeck/junit/blob/master/src/main/java/org/junit/experimental/categories/Categories.java

private List> categories(Description description) {
ArrayList> categories= new ArrayList>();
categories.addAll(Arrays.asList(directCategories(description)));
categories.addAll(Arrays.asList(directCategories(parentDescription(description))));
return categories;
}

should be modified to:

private List> categories(Description description) {
ArrayList> categories= new ArrayList>();
categories.addAll(Arrays.asList(directCategories(description)));
if (categories.isEmpty())
categories.addAll(Arrays.asList(directCategories(parentDescription(description))));
return categories;
}

This can happen in a scenario, where you have e.g. a test annotated by both {Cars, Buses} as a public transport. This test has a test-case ourBus() annotated by a category Bus, and the test is launched by a suite annotated by category Cars.
What will happen is that the suite which has only an interrest to cars will also test ourBus() test case which is of course unwanted. This has happened due to the ArrayList joined categories on type of test and method.
There must be XOR and not the today's OR.