5

I am somewhat new to Java and know almost nothing about Spring / SpringBoot. I saw this line of code today:

@SpringBootTest(classes = {TestRepositoryConfig.class})

I am not really clear what's happening here. From my existing knowledge I infer that I'm looking at something like a key-value parameter, where classes is a parameter passed to the constructor or function called SpringBootTest, and its value is {TestRepositoryConfig.class}.

But I have no idea what those curly braces are doing. Why are they needed? And how could I have answered this question for myself? I didn't know what to search for. "Curly braces java annotation" gave me a deluge of results without any clear answers, unless I wanted to commit to extended reading.

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • 6
    Maybe this will help clear it up: [How to set String Array in Java Annotation](https://stackoverflow.com/questions/20632940/how-to-set-string-array-in-java-annotation) – Jacob G. Sep 06 '19 at 19:54
  • Note that when this is used in cases less closed-ended than `@SpringBootTest`, it's commonly recommended to put the values on separate lines and include a trailing comma to reduce commit noise when values are added or removed. – chrylis -cautiouslyoptimistic- Sep 06 '19 at 20:12

2 Answers2

4

This is a syntax construction to add a few parameters, like:

classes = {TestRepositoryConfig.class, Another.class...}
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
Jackkobec
  • 5,889
  • 34
  • 34
4

It means you can supply multiple values as an array:

@SpringBootTest(classes = {TestRepositoryConfig.class, Class2.class, Class3.class})

Think of it as the same syntax as for initializing an array:

String[] array = { "one", "two", "three" };
Krystian G
  • 2,842
  • 3
  • 11
  • 25