コマンド プロンプト ソリューションを求めていることは承知していますが、実際には JUnit を検討する必要があります。これは、さまざまなテスト条件に対して、はるかに保守しやすいソリューションになります。コマンド ラインを使用する場合は、すべての入力と出力を解析する必要があります。コードを真にテストするために、コマンド ライン (stdin、stdout) の中間を通過する必要はありません。変数にアクセスできるため、Java コードを実行する Java コードが必要です。
本当にコマンド ラインを使用することに設定されている場合は、プログラムの実行方法に関する詳細を提供する必要があります。これは暗闇の中でのショットです。
inputs.txt
(これらは、プログラムに供給される入力です)
Add
4
6
test.bat
@ECHO OFF
:: I just assume that your program prompts for input and waits for stdin
java YourMainClass < inputs.txt > testOutput.txt
:: Program prompts for an operation; "Add" is read from inputs.txt
:: Program prompts for values; "4" and "6" are read from inputs.txt
:: Program prints out the result, which is redirected to testOutput.txt
:: Now you have to read in the testOutput.txt file and look at the last line
FOR /F %%i IN (testOutput.txt) DO SET RESULT=%%i
:: The variable RESULT now contains your program's answer
:: Create a variable to compare your actual result with the expected result
SET EXPECTED=10
IF %RESULT% == %EXPECTED% ECHO "You were correct"
IF NOT %RESULT% == %EXPECTED% ECHO "You were not correct"