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.
In Java when you open a curly brace it means that you open a new scope (usually it's a nested scope). One important thing to understand is that a variable that you'll define inside this scope (which end where you put the closing curly brace) will not be recognized outside of the scope.
Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.
As mentioned previously, curly braces are unsafe characters, and are not valid in URIs (see RFC 3986). For the purposes of API documentation, and other similar uses, they should not be percent encoded, however, because they still aren't URIs - they don't identify a resource.
Java has a "feature" which allows you to omit curly brackets when writing if statements, for loops, or while loops containing only a single statement. You should never use this feature – always include curly brackets. The reason for this is because omitting curly brackets increases your chances of writing buggy code.
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" };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With