相关疑难解决方法(0)

覆盖Junit Test中的默认Spring-Boot application.properties设置

我有一个Spring-Boot应用程序,其中默认属性设置在application.properties类路径中的文件中(src/main/resources/application.properties).

我想在我的JUnit测试中覆盖一些默认设置,其中包含在test.properties文件中声明的属性(src/test/resources/test.properties)

我通常会为我的Junit测试提供专用的Config类,例如

package foo.bar.test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

}
Run Code Online (Sandbox Code Playgroud)

我首先想到@PropertySource("classpath:test.properties")在TestConfig类中使用可以解决这个问题,但这些属性不会覆盖application.properties设置(请参阅Spring-Boot参考文档 - 23.外部化配置).

然后我尝试-Dspring.config.location=classpath:test.properties在调用测试时使用.这很成功 - 但我不想为每次测试执行设置此系统属性.因此我把它放在代码中

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

  static {
    System.setProperty("spring.config.location", "classpath:test.properties");
  }

}
Run Code Online (Sandbox Code Playgroud)

不幸的是,再次没有成功.

必须有一个关于如何application.properties在JUnit测试中覆盖设置的简单解决方案test.properties,我必须忽略它.

java unit-testing spring-boot

174
推荐指数
7
解决办法
18万
查看次数

如何在 spring-test-junit5 中使用嵌套测试?

我一直在玩JUnit 5spring-test-junit5。然后我尝试使用嵌套测试功能,但我的测试失败了。当我使用 gradle 直接从命令行运行时,也会发生这种情况。

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = TestConfig.class)
@DisplayName("In cat club")
public class NestedCatTests {
    @Autowired
    Cat cat;

    @Autowired
    List<Cat> cats;

    @Test
    @DisplayName("Catbert is the cat")
    void catWasAutowired() {
        assertEquals(
                "Catbert",
                cat.getName()
        );
    }

    @Nested
    @DisplayName("Catbert")
    class IsMemberOfClub {
        @Test
        @DisplayName("is a member")
        void isMemberOf() {
            assertTrue(cats.contains(cat));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:91)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$null$1(ClassTestDescriptor.java:196)
at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.executeAndMaskThrowable(JupiterTestDescriptor.java:102)
...
at …
Run Code Online (Sandbox Code Playgroud)

spring-test junit5

8
推荐指数
1
解决办法
2722
查看次数

使用动态值覆盖Junit Test中的默认Spring-Boot application.properties设置

我想覆盖测试中application.properties中定义的属性,但@TestPropertySource只允许提供预定义的值.

我需要的是在随机端口N上启动服务器,然后将此端口传递给spring-boot应用程序.端口必须是短暂的,以允许同时在同一主机上运行多个测试.

我不是指嵌入式http服务器(jetty),而是在测试开始时启动的一些不同的服务器(例如zookeeper)和被测试的应用程序必须连接到它.

实现这一目标的最佳方法是什么?

(这里是一个类似的问题,但答案没有提到临时端口的解决方案 - 在Junit Test中覆盖默认的Spring-Boot application.properties设置)

java junit spring automated-tests spring-boot

6
推荐指数
3
解决办法
1580
查看次数