spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

Test properties for nested tests are ignored

Open pklink opened this issue 8 months ago • 0 comments
trafficstars

Reproduce

Given is the following properties class

package com.bosch.modulith.foo;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "app.foo")
public class FooProperties {

    private String prop1;
    private String prop2;

    public String getProp1() {
        return prop1;
    }

    public void setProp1(String prop1) {
        this.prop1 = prop1;
    }

    public String getProp2() {
        return prop2;
    }

    public void setProp2(String prop2) {
        this.prop2 = prop2;
    }
}

and a regarding test

package com.bosch.modulith.foo;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.modulith.test.ApplicationModuleTest;
import org.springframework.test.context.TestPropertySource;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ApplicationModuleTest
@TestPropertySource(properties = {
        "app.foo.prop1=foo1",
        "app.foo.prop2=foo2",
})
class FooPropertiesTest {

    @Autowired
    FooProperties underTest;

    @Test
    void propertiesShouldSet() {
        assertEquals("foo1", underTest.getProp1());
        assertEquals("foo2", underTest.getProp2());
    }

    @Nested
    @TestPropertySource(properties = {
            "app.foo.prop1=nested-foo1",
            "app.foo.prop2=nested-foo2",
    })
    class FooNestedTest {

        @Test
        void propertiesShouldSet() {
            assertEquals("nested-foo1", underTest.getProp1());
            assertEquals("nested-foo2", underTest.getProp2());
        }

    }

}

Example

I prepared an example project with two failing tests (one is using a "standard data class" as properties class and another is using a record instead) : https://github.com/pklink/spring-modulith-nested-test-properties

Expected behavior

All tests should pass.

Actual Result

The nested test fails because the properties of the "outer test" are used.

[ERROR]   FooPropertiesTest$FooNestedTest.propertiesShouldSet:36 expected: <nested-foo1> but was: <foo1>

pklink avatar Mar 20 '25 09:03 pklink