2

In a nutshell, Spring auto wiring failed on one field of my Java class.

I have a class which has a field set by Spring auto wiring. This field is of boolean type (not Boolean!). However, when unit testing it, I got an error saying,

Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type [java.lang.String] to required type [boolean]; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${enable.pipe}] at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77)

The code is here,

@Value("${enable.pipe}")
private boolean isPipeEnabled;

This class runs well when loaded in a container, but failed and reports the error when running in a unit test. I've tried but failed to find a way to dump the value wired into.

Number945
  • 4,631
  • 8
  • 45
  • 83
CCNA
  • 388
  • 7
  • 17
  • the property file where "enable.pipe" is defined is not in the scope when the unit test runs. – Alexei Kaigorodov Jan 24 '19 at 17:34
  • I got another case where the field values are wired into via bean declaration. ` ` I got a similar issue of field value null, and resolved it by adding the field in a property file. `pipe1.pw=password` However, in the case I did the same but did not work. One more difference is that the resolved one is from String to String I figure. – CCNA Jan 24 '19 at 18:24
  • 1
    Does this answer your question? [Evaluating spring @value annotation as primitive boolean](https://stackoverflow.com/questions/13585259/evaluating-spring-value-annotation-as-primitive-boolean) – dbaltor Sep 29 '20 at 15:13

0 Answers0