Java 未读取junit spring启动配置文件属性
我正在尝试使用SpringBoot从junit设置中的属性文件中读取一个值。 我看不出这个值。以下是我的内容:- 应用程序测试属性Java 未读取junit spring启动配置文件属性,java,spring,spring-boot,junit,Java,Spring,Spring Boot,Junit,我正在尝试使用SpringBoot从junit设置中的属性文件中读取一个值。 我看不出这个值。以下是我的内容:- 应用程序测试属性 my.user.name=Amar 用于创建bean的配置文件: @Configuration @ActiveProfiles("test") @Profile("test") public class RdbmsTestConfig { @Value("${my.user.name}") private String name; @B
my.user.name=Amar
用于创建bean的配置文件:
@Configuration
@ActiveProfiles("test")
@Profile("test")
public class RdbmsTestConfig {
@Value("${my.user.name}")
private String name;
@Bean
public String myString(){
return "Amar";
}
@Bean
public PropsHolder propsHolder() {
PropsHolder propsHolder = new PropsHolder();
propsHolder.setUserName(name);
return propsHolder;
}
}
我的测试班:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RdbmsTestConfig.class)
@ActiveProfiles("test")
public class TestRoomService {
@Autowired
@Qualifier("myString")
private String myString;
@Autowired
private PropsHolder propsHolder;
@Autowired
private Environment env;
@Test
public void userTest() {
Arrays.stream(env.getActiveProfiles()).forEach(System.out::println);
System.out.println(propsHolder.getUserName());
Assert.assertNotNull(myString);
Assert.assertEquals("Amar",myString);
}
}
propsHolder.getUserName的值为${my.user.name}首先从类中删除
@ActiveProfiles(“test”)
。然后您的测试将RdbmsTestConfig
定义为spring上下文。正如我所看到的,您没有运行真正的spring启动测试。问题是您没有在spring配置中配置任何PropertyPlaceHolderConfigure
。因此,如果您有任何SpringBootApplication,请配置一个属性PlaceHolderConfiger
或将@SpringBootTest
添加到测试中。首先从类中删除@ActiveProfiles(“test”)
。然后您的测试将RdbmsTestConfig
定义为spring上下文。正如我所看到的,您没有运行真正的spring启动测试。问题是您没有在spring配置中配置任何PropertyPlaceHolderConfigure
。因此,如果您有任何SpringBootApplication,请配置一个PropertyPlaceHolderConfigure
,或者将@SpringBootTest
添加到您的测试中。我从未使用过@Profile(),因此我不确定这是否符合您的要求。但是我总是使用@PropertySources(),因为否则,代码怎么知道在哪里查找属性呢
@Configuration
@PropertySources(value = { @PropertySource("classpath:core-
test.properties") })
我从来没有使用过@Profile(),所以我不确定它是否应该实现您希望它实现的功能。但是我总是使用@PropertySources(),因为否则,代码怎么知道在哪里查找属性呢
@Configuration
@PropertySources(value = { @PropertySource("classpath:core-
test.properties") })
我创建了一个具有所需注释的基本测试类:-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RdbmsTestConfig.class)
@ActiveProfiles("test")
@SpringBootTest
public abstract class BastTest { }
@ActiveProfiles设置要使用的配置文件,我不必在application.properties文件中提及它
我的测试类现在扩展了以下内容:-
public class TestRoomService extends BastTest
在我的RdbmsTestConfig中,删除@ActiveProfiles注释。我创建了一个具有所需注释的基本测试类:-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RdbmsTestConfig.class)
@ActiveProfiles("test")
@SpringBootTest
public abstract class BastTest { }
@ActiveProfiles设置要使用的配置文件,我不必在application.properties文件中提及它
我的测试类现在扩展了以下内容:-
public class TestRoomService extends BastTest
在我的RdbmsTestConfig中,删除@ActiveProfiles注释。我想使用spring配置文件来设置一些bean和配置,以便在测试应用程序时运行。谢谢你提出的方法。我已经试过了,它可以自己工作,但不能和配置文件一起工作。我想使用spring配置文件来设置一些bean和配置,以便在测试我的应用程序时运行。谢谢你提出的方法。我已经试过了,它可以独立工作,但不能与配置文件一起工作。投票给这个答案,因为@SpringBootTest是缺少的链接投票给这个答案,因为@SpringBootTest是缺少的链接