spring-projects / spring-projects/spring-modulith

Test properties for nested tests are ignored

Open
#1,114 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.2k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

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>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked example project and the interaction between @ApplicationModuleTest, @Nested, and @TestPropertySource shown in FooPropertiesTest. Reproduce both failing nested tests, then trace the test-context setup to identify why outer properties are retained; done means nested tests use their own values while the outer test remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.