How to simulate System.in testing in JUnit?

Posted by Jennifer | Updated on

Want to simulate System.in testing in JUnit? It is technically possible to switch System.in, but in general, it would be more robust not to call it directly in your code, but add a layer of indirection so the input source is controlled from one point in your application. Exactly how you do that is an implementation detail – the suggestions of dependency injection are fine, but you don’t necessarily need to introduce 3rd party frameworks; you could pass round an I/O context from the calling code, for example.

Reference:
https://stackoverflow.com/questions/1647907/junit-how-to-simulate-system-in-testing


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers