Sam's Notes | Sam Blog

梦想还是要有的,万一实现了呢

0%

SpringTesting (一) Annotations

Annotations

Spring Testing Annotations

  • @ContextConfiguration
    类级别注解,用来声明如何加载和配置ApplicationContext。可以使用XML配置文件和被@Configuration注解的类。
    例如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    // 1) xml
    @ContextConfiguration("/test-config.xml")
    public class XmlApplicationContextTests {
    // class body...
    }

    // 2) @Configuration Class
    @ContextConfiguration(classes = TestConfig.class)
    public class ConfigClassApplicationContextTests {
    // class body...
    }

    // 3) `ApplicationContextInitializer` Class
    @ContextConfiguration(initializers = CustomContextIntializer.class)
    public class ContextInitializerTests {
    // class body...
    }

    // 4) ContextLoader
    @ContextConfiguration(locations = "/test-context.xml", loader = CustomContextLoader.class)
    public class CustomLoaderXmlApplicationContextTests {
    // class body...
    }
  • @WebAppConfiguration
    类级别注解,用来声明如何加载WebApplicationContext。默认"file:src/main/webapp"为web App根目录。必须协同 @ContextConfiguration才生效

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @ContextConfiguration
    @WebAppConfiguration
    public class WebAppTests {
    // class body...
    }




    @ContextConfiguration
    @WebAppConfiguration("classpath:test-web-resources")
    public class WebAppTests {
    // class body...
    }
  • @ContextHierarchy
    类级别注解,声明多层级的@ContextConfiguration

    1
    2
    3
    4
    5
    6
    7
    @ContextHierarchy({
    @ContextConfiguration("/parent-config.xml"),
    @ContextConfiguration("/child-config.xml")
    })
    public class ContextHierarchyTests {
    // class body...
    }
    1
    2
    3
    4
    5
    6
    7
    8
    @WebAppConfiguration
    @ContextHierarchy({
    @ContextConfiguration(classes = AppConfig.class),
    @ContextConfiguration(classes = WebConfig.class)
    })
    public class WebIntegrationTests {
    // class body...
    }
  • @ActiveProfiles
    类级别注解,声明ApplicationContext使用哪些profiles。

    1
    2
    3
    4
    5
    @ContextConfiguration
    @ActiveProfiles("dev")
    public class DeveloperTests {
    // class body...
    }
    1
    2
    3
    4
    5
    @ContextConfiguration
    @ActiveProfiles({"dev", "integration"})
    public class DeveloperIntegrationTests {
    // class body...
    }
  • @TestPropertySource
    类级别注解,指定加载properties文件或手动增加PropertySources的内容

    1
    2
    3
    4
    5
    @ContextConfiguration
    @TestPropertySource("/test.properties")
    public class MyIntegrationTests {
    // class body...
    }
    1
    2
    3
    4
    5
    @ContextConfiguration
    @TestPropertySource(properties = { "timezone = GMT", "port: 4242" })
    public class MyIntegrationTests {
    // class body...
    }
  • @DirtiesContext
    类或方法级别注解,根据不同策略,Spring TestContext会刷新Spring的上下文(就是重新创建ApplicationContext)
    有各种策略

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    // BEFORE_CLASS
    @DirtiesContext(classMode = BEFORE_CLASS)
    public class FreshContextTests {
    // some tests that require a new Spring container
    }



    //default class mode : `AFTER_CLASS`
    @DirtiesContext
    public class ContextDirtyingTests {
    // some tests that result in the Spring container being dirtied
    }



    // BEFORE_EACH_TEST_METHOD
    @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)
    public class FreshContextTests {
    // some tests that require a new Spring container
    }



    // AFTER_EACH_TEST_METHOD
    @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
    public class ContextDirtyingTests {
    // some tests that result in the Spring container being dirtied
    }



    // BEFORE_METHOD
    @DirtiesContext(methodMode = BEFORE_METHOD)
    @Test
    public void testProcessWhichRequiresFreshAppCtx() {
    // some logic that requires a new Spring container
    }



    //default method mode `AFTER_METHOD`
    @DirtiesContext
    @Test
    public void testProcessWhichDirtiesAppCtx() {
    // some logic that results in the Spring container being dirtied
    }
  • @TestExecutionListeners

  • @Commit

  • @Rollback

  • @BeforeTransaction

  • @AfterTransaction

  • @Sql

  • @SqlConfig

  • @SqlGroup

Standard Annotation Support

Spring TestContext Framework 支持下列标准的注解

  • @Autowired
    
  • @Qualifier
  • @Resource (javax.annotation) if JSR-250 is present
  • @Inject (javax.inject) if JSR-330 is present
  • @Named (javax.inject) if JSR-330 is present
  • @PersistenceContext (javax.persistence) if JPA is present
  • @PersistenceUnit (javax.persistence) if JPA is present
  • @Required
  • @Transactional

Spring JUnit Testing Annotations

组合SpringJUnit4ClassRunner, Spring’s JUnit rules, 或 Spring’s JUnit support classes使用的时候,Spring TestContext Framework 还支持下列标准的注解:

  • @IfProfileValue
    类级别或方法级别注解,校验具体的环境变量,匹配才进行测试,否则就会忽略。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @IfProfileValue(name="java.vendor", value="Oracle Corporation")
    @Test
    public void testProcessWhichRunsOnlyOnOracleJvm() {
    // some logic that should run only on Java VMs from Oracle Corporation
    }


    @IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"})
    @Test
    public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
    // some logic that should run only for unit and integration test groups
    }
  • @ProfileValueSourceConfiguration

  • @Timed

    1
    2
    3
    4
    @Timed(millis=1000)
    public void testProcessWithOneSecondTimeout() {
    // some logic that should not take longer than 1 second to execute
    }
  • @Repeat

    1
    2
    3
    4
    5
    @Repeat(10)
    @Test
    public void testProcessRepeatedly() {
    // ...
    }