Is it possible to use AssertJ in Spring Boot production code ? I managed to use it in unit test, but 'import static org.assertj.core.api.Assertions.*;' could not be resolved in java file of src/main/java folder even though I tried to fix it by changing build path and change the of junit. Or it is a bad practice to use AssertJ in production code ?
Asked
Active
Viewed 787 times
1
-
2AssertJ is not meant to be used in production, but nothing prevents you to do so. If the productive code cannot resolve the AssertJ package, you might need to change the scope of the AssertJ dependency in your dependency management tool. What kind of use case are you trying to address with AssertJ in production code? – Stefano Cordio Nov 01 '21 at 08:17
-
1To throw exception when there is invalid input. I guess I know the mistake I made, it should be the scope of 'starter-test' which include the AssertJ instead of 'junit'. It's fine now, I change to Spring Boot Assertion. – Constantine Lee Nov 01 '21 at 09:02
-
1FYI: [A good Design-by-Contract library for Java?](https://stackoverflow.com/q/1075719/4506703) – DEWA Kazuyuki - 出羽和之 Nov 01 '21 at 12:09
1 Answers
7
AssertJ is not designed to be used in production code. In case of input validation with a Spring Boot application, Assert
from the Spring Framework, Validate
from Apache Commons Lang, or Preconditions
from Google Guava may be good alternatives.

Stefano Cordio
- 1,687
- 10
- 20
-
When you say "AssertJ is not designed to be used in production code" is there any design decision in particular that makes it unsuitable for production use? Personally I find the API very elegant to work with, also for `Preconditions` like checks. – aioobe Feb 06 '22 at 11:37
-
Nothing specific prevents using AssertJ in production code. However, production code is not the main focus of the library, so production aspects have not been closely scrutinized, such as security or scaling. – Stefano Cordio Feb 06 '22 at 21:03