如何使用 @WebMvcTest 从上下文中排除包

Ale*_*sev 5 java testing spring spring-boot

我想测试应用程序切片,但有一个我想排除的包,因为它与这些测试根本不相关。

我试图以这种方式排除该包:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = MyController.class,
        excludeFilters = {@ComponentScan.Filter(
                type = FilterType.REGEX, pattern = "com.foo.bar.*")})
public class MyControllerTest {
    // ... list of test methods goes here ...
}
Run Code Online (Sandbox Code Playgroud)

无论如何,该包中的类都包含在上下文中。如何修复它?

Saš*_*jak 0

我认为您在模式属性中缺少 * 符号,如下所示:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = MyController.class,
        excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.foo.bar.*")})
public class MyControllerTest {
    // ... list of test methods goes here ...
}
Run Code Online (Sandbox Code Playgroud)