我正在使用 @value(“$ {}”)从src / main / resources文件夹中的application.properties文件中读取属性。当我运行应用程序时,这很有效。但是,当我运行测试用例时,不会设置此值。 我已经尝试了以下内容:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@TestPropertySource("classpath:/application.properties")
这是我的完整代码。
Application.java
@SpringBootApplication
@ComponentScan ({"acn.*"})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
}
EntApiUtil.java 这是设置@value的地方
@Component
public class EntApiUtil implements IEntApiUtil{
Logger log = LoggerFactory.getLogger(this.getClass());
@Value("${entapi.url}")
private String entApiUrl;
private static final String STATUS_VALID = "1";
private static final String STATUS_INVALID ="0" ;
private static final String STATUS_ERROR ="0" ;
private static final int TIMEOUT = 60000;
private static final String SERVICE_NAME_ADDRESS = "AddressService";
..............
..............
private AddressBindingStub getAddressBindingStub() throws Exception {
AddressBindingStub binding = null;
try {
AddressServiceLocator addressServiceLocator = new AddressServiceLocator();
System.out.println("Ent-API URL: " + entApiUrl);
addressServiceLocator.setAddressPortEndpointAddress(entApiUrl + "/" + SERVICE_NAME_ADDRESS);
binding = (AddressBindingStub) addressServiceLocator.getAddressPort();
//Time out after a minute
binding.setTimeout(TIMEOUT);
} catch (javax.xml.rpc.ServiceException jre) {
log.error("Error occurred attempting to establish address validation API.", jre);
System.out.println(" ERROR");
}
return binding;
}
TEST CLASS
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@TestPropertySource("classpath:/application.properties")
public class EntApiUtilTest {
IEntApiUtil entApiUtil;
Address address;
StatusVO statusVO;
@Before
public void setUp(){
entApiUtil = new EntApiUtil();
address = new Address();
address.setStAddress("2120 Mission Rd");
address.setUnitNum("250");
address.setUnitType("Suite");
address.setCity("ESCCONDIDO");
address.setState("CA");
address.setZipcode("92029");
}
@Test
public void validateAddress_statusVO_not_null(){
statusVO = entApiUtil.validateAddress(address);
assertNotNull(statusVO);
}
@Test
public void validateAddress_valid_streetName(){
statusVO = entApiUtil.validateAddress(address);
System.out.println(statusVO.getValidatedAddress().getStreetName() + " =======");
assertTrue("Valid St reet Name", statusVO.getValidatedAddress().getStreetName().equalsIgnoreCase("Mission"));
}
}
我得到Nullpointer异常,因为在运行测试用例时没有设置值。我已经在类中使用了所有可能的注释,并尝试将其添加到我的Config类Application.java中(尝试将其删除)
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
我现在缺少什么?
答案 0 :(得分:2)
使用SpringBoot时不需要添加PropertySourcesPlaceholderConfigurer,默认情况下已添加它。
为什么没有设置EntApiUtil属性值?
你正在自己实例化新的EntApiUtil(),所以春天不会知道这一点。
你需要连接这个
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@TestPropertySource("classpath:/application.properties")
public class EntApiUtilTest
@Autowire
private EntApiUtil entApiUtil
@Test
public void validateAddress_statusVO_not_null(){
///..address etc
statusVO = entApiUtil.validateAddress(address);
assertNotNull(statusVO);
}
}