Skip to content

Test properties for nested tests are ignored #1114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pklink opened this issue Mar 20, 2025 · 0 comments
Open

Test properties for nested tests are ignored #1114

pklink opened this issue Mar 20, 2025 · 0 comments

Comments

@pklink
Copy link

pklink commented Mar 20, 2025

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant