1

I am using System.console and readPassword() method to read password from the user. The problem is that I cannot really initialize System.console object when running from IntelliJ (in my case it throws NullPointerException during JUnit tests). How should I approach that problem? I've seen that people are using Scanner or BufferedReader to overcome that problem, but neither of these two can not nicely hide entered password like readPassword(). Generally speaking my program is intended to run from command line (JAR) and it does nicely but these unit tests are failing from IntelliJ.

Abra
  • 19,142
  • 7
  • 29
  • 41
kmat
  • 21
  • 4
  • Does this help? https://stackoverflow.com/questions/29431697/java-i-o-simulate-input-for-system-console – Abra Mar 17 '23 at 09:02
  • Why not compile it differently for unit testing? Perhaps read from the environment or a file, in this situation. – Rohit Gupta Mar 22 '23 at 19:43

1 Answers1

1

If you don't care if the password is visible when you run your code in the IDE or during unit tests, then the Console with Fallback library may be a sufficient solution for you. If the environment provides a console (in the command line), this is used. If the environment does not provide a console (unit tests), a fallback based on System.out and System.in is used.

Petra Minaler
  • 91
  • 1
  • 8