Reputation: 1
Hi I'm new to Java and Spring, and currently have a situation that is similar to this:
In Message.java
public class Message {
final String text;
public Message(@Value("${message.text}") String text) {
this.text = text;
}
}
In application.properties:
message.text = "This is some text."
Now, I have a test file where I want to write a test that checks the value of the String text, akin to something like
assertEquals(text, "This is some text.");
How would I go about doing this? It seems I can't manually invoke the constructor by doing new Message("...")
because that overrides the @Value injection.
Upvotes: 0
Views: 408
Reputation: 77187
The @Value
annotation here is an instruction to the Spring container, so if you're wanting to test that it operates as expected, the only way to do that is to use SpringRunner
or equivalent to actually start a context and feed it the value in question. Typically, this annotation isn't directly explicitly tested; rather, when the bean is covered as part of a medium-scale test like @SpringBootTest
, its value will be set somewhere and is expected to be used in some business manner, the effect of which is tested.
Upvotes: 0