Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringBoot测试中获取配置属性类bean_Java_Spring_Spring Boot_Spring Test_Spring Boot Test - Fatal编程技术网

Java 在SpringBoot测试中获取配置属性类bean

Java 在SpringBoot测试中获取配置属性类bean,java,spring,spring-boot,spring-test,spring-boot-test,Java,Spring,Spring Boot,Spring Test,Spring Boot Test,我定义了一个类,用于自动从应用程序加载属性-*。属性位于我的sprintboot应用程序中: @Component @ConfigurationProperties("my-app") @EnableConfigurationProperties @Data public class MyAppProperties { private String propertyX; private String propertyY; .. } 下面是我的测试课: @SpringBootTes

我定义了一个类,用于自动从应用程序加载属性-*。属性位于我的sprintboot应用程序中:

@Component
@ConfigurationProperties("my-app")
@EnableConfigurationProperties
@Data
public class MyAppProperties {

  private String propertyX;
  private String propertyY;

..

}
下面是我的测试课:

@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MessageProcessorApplicationTests {

  @Autowired
  private static RestTemplate restTemplate;

  @Autowired
  public static MyAppProperties myAppProperties;

  @Test
  public void testSomething(){
     doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@EnableConfigurationProperties(value = MyAppProperties.class)
public class MessageProcessorApplicationTests {

  @Autowired
  private RestTemplate restTemplate;

  @Autowired
  public MyAppProperties myAppProperties;

  @Test
  public void testSomething(){
     doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}

在我的测试中,myAppProperties始终为null。如何在测试中获取此实例???

@EnableConfigurationProperties(value=MyAppProperties.class)
添加到测试类:

@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MessageProcessorApplicationTests {

  @Autowired
  private static RestTemplate restTemplate;

  @Autowired
  public static MyAppProperties myAppProperties;

  @Test
  public void testSomething(){
     doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@EnableConfigurationProperties(value = MyAppProperties.class)
public class MessageProcessorApplicationTests {

  @Autowired
  private RestTemplate restTemplate;

  @Autowired
  public MyAppProperties myAppProperties;

  @Test
  public void testSomething(){
     doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}

@RunWith(SpringRunner.class)
注释
MessageProcessorApplicationTests
,以自动加载application.properties文件

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MessageProcessorApplicationTests {

@Autowired
  private static RestTemplate restTemplate;

  @Autowired
  public static MyAppProperties myAppProperties;

  @Test
  public void testSomething(){
     doSomeSeetup(myAppProperties.getPropertyX());

}

这不起作用。在\@Test和\@BeforeClass方法中,myAppProperties仍然为null。我向类中添加了RunWith(SpringRunner.class)注释,它成功了。这是正确的方法吗?@RunWith(SpringRunner.class)用于在Spring启动测试功能和JUnit之间提供桥梁。每当我们在JUnit测试中使用任何Spring引导测试功能时,都需要此注释。如果您使用JUnit平台(Junit5)
SpringBootTests
包括
@ExtendWith(SpringExtension.class)
,这使得添加
@RunWith(SpringRunner.class)成为可能
不必要,我只是在使用标准的springboot web flux启动器和spring boot启动器测试。我如何知道它使用的是JUnit4还是JUnit5?使用命令
mvn dependency:tree | grepJUnit
检查junit版本