• SpringBootTest


    @SpringBootTest注解 --基于SpringBoot2.5.7版本

    可以在运行基于Spring Boot的测试的测试类上指定的注释。在常规的Spring TestContext框架之上提供了以下特性:

    • 默认提供SpringBootContextLoader作为ContextLoader,也通过 @ContextConfiguration(loader=…)来自定义

    • 若没有显示指定,将查找嵌套的@Configuration类,然后返回到SpringBootConfiguration搜索配置Spring boot 官方说明

    • 允许使用properties属性定义自定义环境属性。

    • 允许使用args属性定义应用程序参数。

    • 支持不同webEnvironment模式,包括自定义运行web服务器监听或默认为随机端口

    • web服务模式下,自动注册一个TestRestTemplate和/或WebTestClient bean用于web测试

    配置名称备注
    value配置属性
    properties配置属性
    args应用启动参数
    classes指定加载容器上下文配置类,等同于@ContextConfiguration中的class,若没有显示指定,将查找嵌套的@Configuration类,然后返回到SpringBootConfiguration搜索配置

    关于 aliasFor可以参考 spring 官方

    SpringBootTest源码

    1. @Target(ElementType.TYPE)//注解只能用于Class, interface (including annotation type), or enum declaration
    2. @Retention(RetentionPolicy.RUNTIME)//注释将由编译器记录在类文件中,并在运行时由VM保留,因此可以反射性地读取它们。
    3. @Documented
    4. @Inherited //允许子类继承
    5. @BootstrapWith(SpringBootTestContextBootstrapper.class)
    6. @ExtendWith(SpringExtension.class)
    7. public @interface SpringBootTest {
    8. /**
    9. * Alias for {@link #properties()}.
    10. * @return the properties to apply
    11. */
    12. @AliasFor("properties")
    13. String[] value() default {};
    14. /**
    15. * Properties in form {@literal key=value} that should be added to the Spring
    16. * {@link Environment} before the test runs.
    17. * @return the properties to add
    18. */
    19. @AliasFor("value")
    20. String[] properties() default {};
    21. /**
    22. * Application arguments that should be passed to the application under test.
    23. * @return the application arguments to pass to the application under test.
    24. * @see ApplicationArguments
    25. * @see SpringApplication#run(String...)
    26. * @since 2.2.0
    27. */
    28. String[] args() default {};
    29. /**
    30. * The component classes to use for loading an
    31. * {@link org.springframework.context.ApplicationContext ApplicationContext}. Can also
    32. * be specified using
    33. * {@link ContextConfiguration#classes() @ContextConfiguration(classes=...)}. If no
    34. * explicit classes are defined the test will look for nested
    35. * {@link Configuration @Configuration} classes, before falling back to a
    36. * {@link SpringBootConfiguration @SpringBootConfiguration} search.
    37. * @see ContextConfiguration#classes()
    38. * @return the component classes used to load the application context
    39. */
    40. Class[] classes() default {};
    41. /**
    42. * The type of web environment to create when applicable. Defaults to
    43. * {@link WebEnvironment#MOCK}.
    44. * @return the type of web environment
    45. */
    46. WebEnvironment webEnvironment() default WebEnvironment.MOCK;
    47. /**
    48. * An enumeration web environment modes.
    49. */
    50. enum WebEnvironment {
    51. /**
    52. * Creates a {@link WebApplicationContext} with a mock servlet environment if
    53. * servlet APIs are on the classpath, a {@link ReactiveWebApplicationContext} if
    54. * Spring WebFlux is on the classpath or a regular {@link ApplicationContext}
    55. * otherwise.
    56. */
    57. MOCK(false),
    58. /**
    59. * Creates a web application context (reactive or servlet based) and sets a
    60. * {@code server.port=0} {@link Environment} property (which usually triggers
    61. * listening on a random port). Often used in conjunction with a
    62. * {@link LocalServerPort @LocalServerPort} injected field on the test.
    63. */
    64. RANDOM_PORT(true),
    65. /**
    66. * Creates a (reactive) web application context without defining any
    67. * {@code server.port=0} {@link Environment} property.
    68. */
    69. DEFINED_PORT(true),
    70. /**
    71. * Creates an {@link ApplicationContext} and sets
    72. * {@link SpringApplication#setWebApplicationType(WebApplicationType)} to
    73. * {@link WebApplicationType#NONE}.
    74. */
    75. NONE(false);
    76. private final boolean embedded;
    77. WebEnvironment(boolean embedded) {
    78. this.embedded = embedded;
    79. }
    80. /**
    81. * Return if the environment uses an {@link ServletWebServerApplicationContext}.
    82. * @return if an {@link ServletWebServerApplicationContext} is used.
    83. */
    84. public boolean isEmbedded() {
    85. return this.embedded;
    86. }
    87. }
    88. }

    demo案例

    1. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
    2. properties = "server.port=8081", args = "--spring.application.name=demoTest",
    3. classes = AppApplication.class)
    4. @Import(MyConfig.class)
    5. public class SpringBootDemoTest {
    6. @Autowired
    7. private ApplicationContext applicationContext;
    8. @Value("${spring.application.name}")
    9. private String appName;
    10. @Autowired(required = false)
    11. private MyConfig myConfig;
    12. @Test
    13. void AppApplicationTest() {
    14. assert ("demoTest".equals(appName));
    15. assertThat(myConfig).isNotNull().extracting("cfgName").isEqualTo("11");
    16. assertThat(applicationContext).isNotNull();
    17. }
    18. }

    1. import lombok.Getter;
    2. import lombok.Setter;
    3. @Getter
    4. @Setter
    5. public class MyConfig {
    6. private String cfgName = "11";
    7. }
    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. /**
    4. * @author bearboy80
    5. */
    6. @SpringBootApplication
    7. public class AppApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(AppApplication.class, args);
    10. }
    11. }
  • 相关阅读:
    一个前端大佬的十年回顾 | 漫画前端的前世今生
    Ansible自动化运维工具
    相亲交友小程序源码 同城相亲交友小程序源码
    Word控件Spire.Doc 【表单域】教程(五):如何在 C# 中更新 Ask 字段
    从源码观测STL-std::vector
    CCC数字钥匙设计【NFC】--通过NFC进行车主配对Phase3
    目标检测YOLO实战应用案例100讲-基于无人机航拍图像的目标检测
    Flask--登录页面应用案例
    Java面试题之Java集合面试题 50道(带答案)
    xshell连接服务器把密钥登陆改为密码登陆
  • 原文地址:https://blog.csdn.net/Lzfnemo2009/article/details/132945492