Let’s assume we have some paramaters in application.properties that we want to use in our Spring Boot application/tests. With Spring Boot we can do that easily using @Value annotation. Here is an example how to get parameters from application.properties in TestNG tests.
Parameters in application.properties
testData.users.file=test-users.json
site.url=https://www.lenar.io
@Value Spring Boot annotation
@Value("${testData.users.file}")
private String usersFile;
@Value("${site.url}")
private String siteUrl;
Using parameters from Spring Boot application.properties in TestNG tests.
package io.lenar.examples.spring;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;
@SpringBootTest(classes = TestNGWithSpringApplication.class)
public class ApplicationPropertiesExampleIT extends AbstractTestNGSpringContextTests {
@Value("${testData.users.file}")
private String usersFile;
@Value("${site.url}")
private String siteUrl;
@Test
public void testUserFileTest() throws IOException {
Assert.assertEquals(usersFile, "test-users.json");
}
@Test
public void siteUrlTest() throws IOException {
Assert.assertEquals(siteUrl, "https://www.lenar.io");
}
}
You may also find these posts interesting: