Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Micronaut: Injecting a list of values with @Property to a test class does not work as expected

Tags:

micronaut

How can I define some specific property for testing that is a list and not just a string?

The documentation explains, how to do with string, but I can't set the value to a list of strings.

application.yml

items:
  - "Item 1"
  - "Item 2"

Test file:

@MicronautTest(environments = ["test"])
class MyTest {

    @Test
    @Property(name = "items", value = "Item 1,Item 2") // this does not work
    fun justWithOneItem() {
        // ...
    }
}

On the actual code, this works (as documented here)

Project file:

@Singleton
class SomeClass {
    @set:Inject
    @setparam:Property(name = "items")
    var items: List<String>? = null

    // ...
}

I know that I can create an application-test.yml and do

@MicronautTest(environments = ["test"])
class MyTest {
    // ...
}

But I would rather prefer to set it programmatically instead of creating a new env/yaml file.

like image 596
caiolopes Avatar asked Oct 24 '25 02:10

caiolopes


1 Answers

I think you have 2 options:

  1. Use @Property(name = "items[0]", value = "Item1") and @Property(name = "items[1]", value = "Item2")

  2. Change your test to implement TestPropertyProvider and provide the config via the returned map

like image 148
James Kleeh Avatar answered Oct 27 '25 01:10

James Kleeh