有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    问题是你使用的是不同的跑步者。通常,人们使用SpringRunner.class来处理创建测试上下文等功能

    Construct a new SpringRunner and initialize a TestContextManager to provide Spring testing functionality to standard JUnit 4 tests.

    要使用不同的runner和spring功能,可以使用ClassRuleRule的组合

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
    
    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();
    

    SpringClassRule is a custom JUnit TestRule that supports class-level features of the Spring TestContext Framework in standard JUnit tests by means of the TestContextManager and associated support classes and annotations.

    In contrast to the SpringJUnit4ClassRunner, Spring's rule-based JUnit support has the advantage that it is independent of any Runner and can therefore be combined with existing alternative runners like JUnit's Parameterized or third-party runners such as the MockitoJUnitRunner.

    In order to achieve the same functionality as the SpringJUnit4ClassRunner, however, a SpringClassRule must be combined with a SpringMethodRule, since SpringClassRule only supports the class-level features of the SpringJUnit4ClassRunner.

    如果没有这些,就无法在从属类中自动连线

    我在你的项目中添加了一个PR,并通过了测试