Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parameterize property values for spring boot test

I am working on an aggregation framework using spring boot, which can be used to aggregate multiple kinds of I/O operations' results. Additionally, it has support for both non-blocking (reactive) & blocking aggregation layer, which is controlled by a property, which is in-turn used for conditional beans. For integration testing, I have been using this setup:

abstract class HttpUpstreamTests {
    // Setup & test logic
}

@SpringBootTest(properties = {
        "aggregation.reactive.enabled=false",
})
class BlockingAggregationIT extends HttpUpstreamTests {}

@SpringBootTest(properties = {
        "aggregation.reactive.enabled=true",
})
class NonBlockingAggregationIT extends HttpUpstreamTests {}

Now I have to add a couple more base classes:

abstract class MongodbUpstreamTests {
    // Setup & test logic
}
abstract class SqlUpstreamTests {
    // Setup & test logic
}

But of course, I can't make the IT classes extend from multiple abstract classes. One way would be to make all the upstrteam tests parameterized tests, but I can't seem to find a way to use that approach with different values of the property in @SpringBootTest(properties="aggregation.reactive.enabled=false"). Is there a way to use different properties as a parameter for parameterized tests?

like image 411
aksh1618 Avatar asked Jul 04 '26 19:07

aksh1618


1 Answers

Rather than using class hierarchy, use composition. Restructure your HttpUpstreamTests, MongodbUpstreamTests and SqlUpstreamTests classes to act as self-contained concrete helper classes that you can instantiate and call methods on them to do the setup and tests you need.

This will help with modularity and probably simplify how each of those classes works.

For example, it might look something like this:

class HttpUpstreamTests {
  public HttpUpstreamTests(some params...)...

  public void setup()...

  public void testSomething(...)
}

class BlockingAggregationIT {

  private HttpUpstreamTests httpUpstreamTests;

  @Before
  public void setup(){
    httpUpstreamTests = new HttpUpstreamTests(...);
    httpUpstreamTests.setup();
  }

  @Test
  public void testOne(){
    // ...
    httpUpstreamTests.testSomething();
    // ...
  }
}

I'm guessing you have a controller, or some other class that you are trying to test here. You could pass a reference to that controller as a parameter to the constructor, setup method or even the testSomething method. Likewise for any other mocks and things that might need to be shared between your test class and these helper classes.

like image 177
GreenGiant Avatar answered Jul 06 '26 09:07

GreenGiant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!