简体   繁体   English

SpringBoot 单元测试配置

[英]SpringBoot unit test configuration

I created a spring-boot 1.4.0 application and I would like to internationlize it using yaml file.我创建了一个 spring-boot 1.4.0 应用程序,我想使用 yaml 文件将它国际化。

I created a class for loading the configuration from the yaml file like it is explained in the documentation here http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties .我创建了一个用于从 yaml 文件加载配置的类,就像在http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config的文档中解释的那样。 html#boot-features-external-config-typesafe-configuration-properties

I would like to create a test to check that my class has correctly loaded the properties from the yaml file.我想创建一个测试来检查我的类是否正确加载了 yaml 文件中的属性。

If we keep the exemple from the documentation how to create a unit test that will load a yaml file (with a different name that application.yml) and check that the method getUsername() will return the value from the yaml file ?如果我们保留文档中的示例,如何创建一个单元测试来加载一个 yaml 文件(与 application.yml 不同)并检查方法getUsername()是否会从 yaml 文件返回值?


Here is the code I have but still can't load the username :这是我的代码,但仍然无法加载用户名:

@Component
@ConfigurationProperties(locations = "classpath:mylocalizedprops.yml", prefix="connection")
public class ConnectionProperties {

    private String username;

    // ... getters and setters

}

and the test class和测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class InternationalizationTest {
    @Autowired
    private ConnectionProperties connectionProperties;

    public void propsShouldBeNotNull() {
        assertNotNull(connectionProperties);
    }

    public void userNameShouldBeCorrect() {
        assertEquals(connectionProperties.getUsername(), expectedUserName);
    }
}

I have failed the userNameShouldBeCorrect test.我没有通过 userNameShouldBeCorrect 测试。 The file mylocalizedprops.yml is located in the src/main/resources folder of a Maven structured application.文件 mylocalizedprops.yml 位于 Maven 结构化应用程序的 src/main/resources 文件夹中。

I would consider this an integration test, not a unit-test because you are testing the interaction between various components. 我认为这是一个集成测试,而不是单元测试,因为您正在测试各种组件之间的交互。 Regardless, here is how I would do it. 无论如何,这是我的做法。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = YourApplication.class)
public class InternationalizationTests() {

  @Autowired
  ConnectionProperties connectionProperties;

  @Test
  public void testCorrectTranslationLoaded() {
    Assert.assertEquals("english-username", connectionProperties.getUsername());
  }    

}

You can also create a test configuration if you would like to, which you can specify which translation to load. 如果愿意,您还可以创建测试配置,可以指定要加载的翻译。 You would then need different classes to test different configurations. 然后,您将需要不同的类来测试不同的配置。 See the docs: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html 参见文档: http : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

Unit test can be done easily with Jmockit使用 Jmockit 可以轻松完成单元测试

    import org.junit.jupiter.api.Test;
    import org.springframework.boot.SpringApplication;
    import org.springframework.context.ConfigurableApplicationContext;

    import mockit.Mock;
    import mockit.MockUp;
    import mockit.Mocked;
    import mockit.Verifications;

    class RuleApiApplicationTest {
        
        @Mocked
        private ConfigurableApplicationContext mockedContext;

        @Test
        void testApplicationRun() {
            new MockUp<SpringApplication>() {
                @Mock
                public ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
                    return mockedContext;
                }
            };

            RuleApiApplication.main(new String[]{});
            
            new Verifications() {{
                SpringApplication.run(RuleApiApplication.class, new String[]{});
            }};
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM