这里的其他答案都很棒,但并不完全符合我的喜好。我最终结合了JUnit @RunWith(@Parameterized)(运行两个服务的测试)Spring 的新规则(保持@RunWith(SpringRunner.class)容器、appcontext、webmvc 和注入功能)以下是片段:@RunWith(Parameterized.class)...@Parameters(name = "Cached = {0}")public static Boolean[] data() { return new Boolean[] { Boolean.TRUE, Boolean.FALSE };}@ClassRulepublic static final SpringClassRule springClassRule = new SpringClassRule();@Rulepublic final SpringMethodRule springMethodRule = new SpringMethodRule();@Beforepublic void setUp() { // logic to choose the injected service based on the true/false param}