0

We have inherited a large suite of code that has minimal testing. I'm looking to update and create tests.

We have a simple bean that has a private field that uses @value to inject a constant. This bean is constructed and passed around by numerous pieces of code which use the private @value. I want to set up a test suit to always inject some constant into the @value for any instantiated version of the bean.

I know how to inject a @value to a single bean, but considering how often the bean will be instantiated by spy in my test I don't want to have to inject mocks and inject @value into that mock for every case, I'd rather do this on a class level.

I'm aware that it's not good to abuse @value on private variables. We will hopefully fix this at some point, but at this point I don't want to mess with complicated number of constructors for an untestable behemoth of a class if I can avoid it. I'd like a way to test @value on a private field for now, and I'll look to moving how @value is utilized later once we have more stable/testable code base.

Can I configure this injection so it happens to all instantiated instances of the class automatically?

dsollen
  • 6,046
  • 6
  • 43
  • 84

1 Answers1

1

Create a custom test configuration that includes your normal configuration and define the spy as a bean in there with @Primary with the custom @Value value injected in. You can include this as a class file directly in your test folder. That way, anywhere it's being autowired by the spring context, it will get the one from the test configuration instead of the one defined in your normal context.

Taugenichts
  • 1,307
  • 12
  • 19