在java测试中不会读取application.properties值

问题描述 投票:0回答:1

我有以下服务,我在其中使用 application.properties 文件的属性:

@Service
public class TesoSunSystemsServiceImpl implements TesoSunSystemsService {       
     
    @Value("${spring.sesame.proxy}")
    private String proxySesame;
     
    @Value("${spring.sesame.proxy.port}")
    private Integer proxyPortSesame;
     
    @Value("${sunSystem.usuario}")
    private String usuario;
        
    @Value("${sunSystem.contrasenia}")
    private String contrasenia;             
     
     
    public String obtenerToken() throws Exception {
            
        String body = "<soapenv:Envelope xmlns:soapenv=\"web=\"/\">\r\n" +
                    "   <soapenv:Body>\r\n" +
                    "      <web:SecurityProviderAuthenticateRequest>\r\n" +
                    "         <web:name>" + usuario + "</web:name>\r\n" +
                    "         <web:password>" + contrasenia + "</web:password>\r\n" +
                    "      </web:SecurityProviderAuthenticateRequest>\r\n" +
                    "   </soapenv:Body>\r\n" +
                    "</soapenv:Envelope>";
     
            
        // Realiza la conexión al servicio SOAP
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxySesame, proxyPortSesame));
     
    ....
    ....

我的下一个测试证明了该方法:

@SpringBootTest
public class TesoSunSystemsServiceImplTest {    
    
    @InjectMocks
    private TesoSunSystemsServiceImpl tesoSunSystemsServiceImpl;
     
    @Test
    void testGenerarFacturas(){
     
        String Token = asfsaasfTokenTest;
            
        String response = tesoSunSystemsServiceImpl.obtenerToken();
     
        //Mas consideraciones, implementaciones, etc  
     
        assertNotNull(response);
        
    }
  • 我的问题是,当我运行我的应用程序时,这会毫无问题地使用属性的属性,但是当我运行测试时,由于未读取测试属性,因此会出现错误,因此要运行属性为 null 的方法,这失败了,我该如何让我的服务在运行测试时读取属性的属性?

如果您能给我一些如何继续的想法,提前致以问候和感谢。

java unit-testing junit application.properties
1个回答
0
投票

对于单元测试,属性文件也必须放在

src/test/resources/
下。

相反,如果在测试过程中您需要使用另一个属性文件覆盖应用程序的属性,您可以在测试类之前使用注释

@TestPropertySource

© www.soinside.com 2019 - 2024. All rights reserved.