Verisoft Wiki

We saw previously how to use the built-in URL property. Now, we will demonstrate how to add your own property values.

In order to define additional properties you will need to do the following:

  1. Add properties to the application.properties file or create an additional property file (the best practice is to put all your properties in a single file)
  2. Create a class that will automatically load the property.
  3. Use this class in your test.

Add properties to the application.properties file

So, we will start by adding a new property to the application.properties file, and we will name it custom.property, and its value will be “Hello”:

image-20230527-182711.png

Create a class that will automatically load the property

Next, let’s define a class that will automatically hold the value. Note! The class’s package name MUST start with co.verisoft.

package co.verisoft.examples.property;

@Getter
@Component
public class CustomProperty {

    @Value("${custom.property}")
    private String customProperty;
}

Let’s understand this piece of code:

Use this class in your test

Finally, here is the complete code of your test:

@Slf4j
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {EnvConfig.class})
public class PropertyFileLoaderTest extends BaseTest{
    
    @Autowired
    CustomProperty customProperty;
    
    
    @Test
    @DisplayName("Custom Property")
    public void customProperty(VerisoftDriver driver) throws InterruptedException {
        Assertions.assertEquals(customProperty.getCustomProperty(), "Hello");
    }
}
   

We explained lines 1-4 on the previous page, so we will focus on lines - 6-7:

@Autowired- This will tell Spring that the next line should be injected using the Spring framework.

CustomProperty customProperty;- This is the class we created previously.

Now, all we have to do is to use it in our test, like this:

customProperty.getCustomProperty()