I'm using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have:
@Value("#{myProps['default.url']}")
private String defaultUrl;
@Value("#{myProps['default.password']}")
private String defaultrPassword;
// ...
From my JUnit test, which I currently have set up like so:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class MyTest
{
I would like to mock a value for my "defaultUrl" field. Note that I don't want to mock values for the other fields — I'd like to keep those as they are, only the "defaultUrl" field. Also note that I have no explicit "setter" methods (e.g. setDefaultUrl
) in my class and I don't want to create any just for the purposes of testing.
Given this, how can I mock a value for that one field?
You can use this magic Spring Test annotation :
see org.springframework.test.context.TestPropertySource
For example, this is the test class :
And this is the class with the property :
You can also mock your property configuration into your test class
You can use the magic of Spring's
ReflectionTestUtils.setField
in order to avoid making any modifications whatsoever to your code.Check out this tutorial for even more information, although you probably won't need it since the method is very easy to use
UPDATE
Since the introduction of Spring 4.2.RC1 it is now possible to set a static field without having to supply an instance of the class. See this part of the documentation and this commit.
It was now the third time I googled myself to this SO post as I always forget how to mock an @Value field. Though the accepted answer is correct, I always need some time to get the "setField" call right, so at least for myself I paste an example snippet here:
Production class:
Test class:
I'd like to suggest a related solution, which is to pass the
@Value
-annotated fields as parameters to the constructor, instead of using theReflectionTestUtils
class.Instead of this:
and
Do this:
and
Benefits of this approach: 1) we can instantiate the Foo class without a dependency container (it's just a constructor), and 2) we're not coupling our test to our implementation details (reflection ties us to the field name using a string, which could cause a problem if we change the field name).
One way to resolve this is change your class to use Constructor Injection, that is used for testing and Spring injection. No more reflection :)
So, you can pass any String using the constructor:
And in your test, just use it: