如何在@springBootTest类中使用@componentScan
我正在为我的 Spring 应用程序创建一个测试类。主应用程序具有以下配置,
@SpringBootApplication(scanBasePackages = {"com.graphql.demo"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
我需要扫描 graphql 包进行测试。我尝试在我的测试类中添加 @ComponentScan 。
@ExtendWith({SpringExtension.class,MockitoExtension.class})
@ComponentScan(basePackages = {"com.graphql.demo"})
@SpringBootTest
public class SampleTestClass {
// my code
}
但它似乎没有读取我需要的 graphql 包。如何在 Spring Boot 测试中扫描基础包?
I am creating a test class for my spring application. The main application has following configurations
@SpringBootApplication(scanBasePackages = {"com.graphql.demo"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
I need to scan that graphql package for my tests. I tried by added the @ComponentScan in my test class.
@ExtendWith({SpringExtension.class,MockitoExtension.class})
@ComponentScan(basePackages = {"com.graphql.demo"})
@SpringBootTest
public class SampleTestClass {
// my code
}
But it seems it is not reading that graphql packages which I need. How can I scan the base packages in spring boot test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单个
@SpringBootTest
就足够了。@SpringBootTest
将找到最近的@SpringBootApplication
(在包层次结构中查找),并使用它的所有配置。确保您的测试位于主类 (
MyApplication
) 包的子包中。如果您的
MyApplication
位于com.graphql.demo
中,则您的测试应位于com.graphql.demo.**
中,否则@SpringBootTest
将找不到主类及其配置。A single
@SpringBootTest
should be enough.@SpringBootTest
will find the nearest@SpringBootApplication
(looking up in the packages hierarchy), and use all of it's configuration.Make sure that your test resides in the sub-package of the main class (
MyApplication
) package.If your
MyApplication
resides incom.graphql.demo
, your test should be incom.graphql.demo.**
, otherwise the@SpringBootTest
won't find the main class and it's configuration.