Monday, 15 April 2013

java - Junit - cannot read from std in -



java - Junit - cannot read from std in -

when seek run unit tests , e.g. add together system.in.read() , run them via intellij, console can type not take input... if junit redirecting stdin somehow... want test wait key press - there alternative way or reset stdin?

unit tests meant automated. means no user intervention shouldn't require user type console test.

if need test code reads console input system.in test should temporarily replace system.in else.

system.setin( myinputstream);

just create sure set when done. it's best in @before , set in @after this:

private inputstream oldin = null; @before public void replacestdin(){ //save reference console stdin can set later oldin = system.in; } @after public void restorestdin(){ system.setin(oldin); } @test public void testconsoleinput(){ inputstream mockstdin = ... system.setin(mockstdin); //now test usual }

i've utilize junit's rule create own rule me automatically scheme streams when need to.

java junit

No comments:

Post a Comment