Junit test Scanner input issue Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!JUnit: How to simulate System.in testing?Scanner cannot be resolved to a typeHow do you assert that a certain exception is thrown in JUnit 4 tests?JUnit test for System.out.println()Scanner vs. BufferedReaderHow to run test methods in specific order in JUnit4?Maven does not find JUnit tests to runScanner is skipping nextLine() after using next() or nextFoo()?Getting a NoSuchElementsException in Java?I/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWould it make any difference giving arguments using scanner class instead of command line arguments?
When do you get frequent flier miles - when you buy, or when you fly?
What is Arya's weapon design?
Fundamental Solution of the Pell Equation
List of Python versions
How to deal with a team lead who never gives me credit?
What does the "x" in "x86" represent?
Error "illegal generic type for instanceof" when using local classes
How can I make names more distinctive without making them longer?
Understanding Ceva's Theorem
How to find all the available tools in mac terminal?
How much time will it take to get my passport back if I am applying for multiple Schengen visa countries?
Using et al. for a last / senior author rather than for a first author
How to react to hostile behavior from a senior developer?
Sci-Fi book where patients in a coma ward all live in a subconscious world linked together
Apollo command module space walk?
What causes the vertical darker bands in my photo?
Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?
What is the logic behind the Maharil's explanation of why we don't say שעשה ניסים on Pesach?
Can I cast Passwall to drop an enemy into a 20-foot pit?
How do I stop a creek from eroding my steep embankment?
English words in a non-english sci-fi novel
Why did the IBM 650 use bi-quinary?
What is known about the Ubaid lizard-people figurines?
51k Euros annually for a family of 4 in Berlin: Is it enough?
Junit test Scanner input issue
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!JUnit: How to simulate System.in testing?Scanner cannot be resolved to a typeHow do you assert that a certain exception is thrown in JUnit 4 tests?JUnit test for System.out.println()Scanner vs. BufferedReaderHow to run test methods in specific order in JUnit4?Maven does not find JUnit tests to runScanner is skipping nextLine() after using next() or nextFoo()?Getting a NoSuchElementsException in Java?I/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWould it make any difference giving arguments using scanner class instead of command line arguments?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am currently trying to Junit test a game for a Uni assignment. Below is an example of the kind of methods I am trying to test.
public static int choosePlayers(int num)
while (validPlayerNumber == false)
try
System.out.print("Please enter Number of players (2-4)n> ");
num = in.nextInt();
switch (num)
case 2:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 3:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 4:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
default:
throw new IllegalArgumentException("Sorry, that is not a valid selection.");
// System.out.println("Sorry, that is not a valid selection.");
catch (InputMismatchException ex)
// log the exception
System.out.println("Problem with input : " + ex.toString());
continue;
return numberPlayers;
I am using the following test class to test this:
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
num1 =1;
num2= 2;
num4 = 4;
num3 = 3;
num5 = 5;
game= new Game();
@Test
public void testchoosePlayers2()
System.out.println("Testing choosingPlayers 2");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num2);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers3()
System.out.println("Testing choosingPlayers 3");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num3);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers4()
System.out.println("Testing choosingPlayers 4");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num4);
assertEquals(expected, actual);
System.out.println("Test finsihed");
Every time I try and run this test only the first test will run and the scanner is not re-prompted for the subsequent 2. Is there any way around this? I would greatly appreciate any advice concerning this issue or for a better/more efficient way to test the above method .
java java.util.scanner junit4
add a comment |
I am currently trying to Junit test a game for a Uni assignment. Below is an example of the kind of methods I am trying to test.
public static int choosePlayers(int num)
while (validPlayerNumber == false)
try
System.out.print("Please enter Number of players (2-4)n> ");
num = in.nextInt();
switch (num)
case 2:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 3:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 4:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
default:
throw new IllegalArgumentException("Sorry, that is not a valid selection.");
// System.out.println("Sorry, that is not a valid selection.");
catch (InputMismatchException ex)
// log the exception
System.out.println("Problem with input : " + ex.toString());
continue;
return numberPlayers;
I am using the following test class to test this:
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
num1 =1;
num2= 2;
num4 = 4;
num3 = 3;
num5 = 5;
game= new Game();
@Test
public void testchoosePlayers2()
System.out.println("Testing choosingPlayers 2");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num2);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers3()
System.out.println("Testing choosingPlayers 3");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num3);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers4()
System.out.println("Testing choosingPlayers 4");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num4);
assertEquals(expected, actual);
System.out.println("Test finsihed");
Every time I try and run this test only the first test will run and the scanner is not re-prompted for the subsequent 2. Is there any way around this? I would greatly appreciate any advice concerning this issue or for a better/more efficient way to test the above method .
java java.util.scanner junit4
add a comment |
I am currently trying to Junit test a game for a Uni assignment. Below is an example of the kind of methods I am trying to test.
public static int choosePlayers(int num)
while (validPlayerNumber == false)
try
System.out.print("Please enter Number of players (2-4)n> ");
num = in.nextInt();
switch (num)
case 2:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 3:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 4:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
default:
throw new IllegalArgumentException("Sorry, that is not a valid selection.");
// System.out.println("Sorry, that is not a valid selection.");
catch (InputMismatchException ex)
// log the exception
System.out.println("Problem with input : " + ex.toString());
continue;
return numberPlayers;
I am using the following test class to test this:
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
num1 =1;
num2= 2;
num4 = 4;
num3 = 3;
num5 = 5;
game= new Game();
@Test
public void testchoosePlayers2()
System.out.println("Testing choosingPlayers 2");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num2);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers3()
System.out.println("Testing choosingPlayers 3");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num3);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers4()
System.out.println("Testing choosingPlayers 4");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num4);
assertEquals(expected, actual);
System.out.println("Test finsihed");
Every time I try and run this test only the first test will run and the scanner is not re-prompted for the subsequent 2. Is there any way around this? I would greatly appreciate any advice concerning this issue or for a better/more efficient way to test the above method .
java java.util.scanner junit4
I am currently trying to Junit test a game for a Uni assignment. Below is an example of the kind of methods I am trying to test.
public static int choosePlayers(int num)
while (validPlayerNumber == false)
try
System.out.print("Please enter Number of players (2-4)n> ");
num = in.nextInt();
switch (num)
case 2:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 3:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 4:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
default:
throw new IllegalArgumentException("Sorry, that is not a valid selection.");
// System.out.println("Sorry, that is not a valid selection.");
catch (InputMismatchException ex)
// log the exception
System.out.println("Problem with input : " + ex.toString());
continue;
return numberPlayers;
I am using the following test class to test this:
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
num1 =1;
num2= 2;
num4 = 4;
num3 = 3;
num5 = 5;
game= new Game();
@Test
public void testchoosePlayers2()
System.out.println("Testing choosingPlayers 2");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num2);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers3()
System.out.println("Testing choosingPlayers 3");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num3);
assertEquals(expected, actual);
System.out.println("Test finsihed");
@Test
public void testchoosePlayers4()
System.out.println("Testing choosingPlayers 4");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num4);
assertEquals(expected, actual);
System.out.println("Test finsihed");
Every time I try and run this test only the first test will run and the scanner is not re-prompted for the subsequent 2. Is there any way around this? I would greatly appreciate any advice concerning this issue or for a better/more efficient way to test the above method .
java java.util.scanner junit4
java java.util.scanner junit4
asked Mar 8 at 17:38
Emma Cox Emma Cox
24
24
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your purpose here is to test that method returns correct values with provided input parameters. If you scan incorrect value test will fail. So instead of scanning expected value just put expected value to the assertion. Also you don't need separate tests here, since you test the same functionality.
To simulate input you will need to replace System.in with your own input stream. See JUnit: How to simulate System.in testing? for details.
private final InputStream systemIn = System.in;
private ByteArrayInputStream testIn;
@After
public void resetSystemIn()
System.setIn(systemIn);
private void inputData(String data)
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
@Test
public void testchoosePlayers()
System.out.println("Testing choosingPlayers 2");
inputData("2");
int actual = game.choosePlayers(num2);
assertEquals(num2, actual);
System.out.println("Testing choosingPlayers 3");
inputData("3");
actual = game.choosePlayers(num3);
assertEquals(num3, actual);
System.out.println("Testing choosingPlayers 4");
inputData("4");
actual = game.choosePlayers(num4);
assertEquals(num4, actual);
System.out.println("Test finsihed");
Thank you for your advice, unfortunately when I run the test, the scanner from the method being tested Is still called and asks for an number input, negating the values I set in the test data (num1 , num2 etc). Do you have any advice concerning this?
– Emma Cox
Mar 8 at 18:40
int num
parameter confused me. See answer updated.
– devmind
Mar 8 at 19:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068303%2fjunit-test-scanner-input-issue%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your purpose here is to test that method returns correct values with provided input parameters. If you scan incorrect value test will fail. So instead of scanning expected value just put expected value to the assertion. Also you don't need separate tests here, since you test the same functionality.
To simulate input you will need to replace System.in with your own input stream. See JUnit: How to simulate System.in testing? for details.
private final InputStream systemIn = System.in;
private ByteArrayInputStream testIn;
@After
public void resetSystemIn()
System.setIn(systemIn);
private void inputData(String data)
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
@Test
public void testchoosePlayers()
System.out.println("Testing choosingPlayers 2");
inputData("2");
int actual = game.choosePlayers(num2);
assertEquals(num2, actual);
System.out.println("Testing choosingPlayers 3");
inputData("3");
actual = game.choosePlayers(num3);
assertEquals(num3, actual);
System.out.println("Testing choosingPlayers 4");
inputData("4");
actual = game.choosePlayers(num4);
assertEquals(num4, actual);
System.out.println("Test finsihed");
Thank you for your advice, unfortunately when I run the test, the scanner from the method being tested Is still called and asks for an number input, negating the values I set in the test data (num1 , num2 etc). Do you have any advice concerning this?
– Emma Cox
Mar 8 at 18:40
int num
parameter confused me. See answer updated.
– devmind
Mar 8 at 19:21
add a comment |
Your purpose here is to test that method returns correct values with provided input parameters. If you scan incorrect value test will fail. So instead of scanning expected value just put expected value to the assertion. Also you don't need separate tests here, since you test the same functionality.
To simulate input you will need to replace System.in with your own input stream. See JUnit: How to simulate System.in testing? for details.
private final InputStream systemIn = System.in;
private ByteArrayInputStream testIn;
@After
public void resetSystemIn()
System.setIn(systemIn);
private void inputData(String data)
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
@Test
public void testchoosePlayers()
System.out.println("Testing choosingPlayers 2");
inputData("2");
int actual = game.choosePlayers(num2);
assertEquals(num2, actual);
System.out.println("Testing choosingPlayers 3");
inputData("3");
actual = game.choosePlayers(num3);
assertEquals(num3, actual);
System.out.println("Testing choosingPlayers 4");
inputData("4");
actual = game.choosePlayers(num4);
assertEquals(num4, actual);
System.out.println("Test finsihed");
Thank you for your advice, unfortunately when I run the test, the scanner from the method being tested Is still called and asks for an number input, negating the values I set in the test data (num1 , num2 etc). Do you have any advice concerning this?
– Emma Cox
Mar 8 at 18:40
int num
parameter confused me. See answer updated.
– devmind
Mar 8 at 19:21
add a comment |
Your purpose here is to test that method returns correct values with provided input parameters. If you scan incorrect value test will fail. So instead of scanning expected value just put expected value to the assertion. Also you don't need separate tests here, since you test the same functionality.
To simulate input you will need to replace System.in with your own input stream. See JUnit: How to simulate System.in testing? for details.
private final InputStream systemIn = System.in;
private ByteArrayInputStream testIn;
@After
public void resetSystemIn()
System.setIn(systemIn);
private void inputData(String data)
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
@Test
public void testchoosePlayers()
System.out.println("Testing choosingPlayers 2");
inputData("2");
int actual = game.choosePlayers(num2);
assertEquals(num2, actual);
System.out.println("Testing choosingPlayers 3");
inputData("3");
actual = game.choosePlayers(num3);
assertEquals(num3, actual);
System.out.println("Testing choosingPlayers 4");
inputData("4");
actual = game.choosePlayers(num4);
assertEquals(num4, actual);
System.out.println("Test finsihed");
Your purpose here is to test that method returns correct values with provided input parameters. If you scan incorrect value test will fail. So instead of scanning expected value just put expected value to the assertion. Also you don't need separate tests here, since you test the same functionality.
To simulate input you will need to replace System.in with your own input stream. See JUnit: How to simulate System.in testing? for details.
private final InputStream systemIn = System.in;
private ByteArrayInputStream testIn;
@After
public void resetSystemIn()
System.setIn(systemIn);
private void inputData(String data)
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
@Test
public void testchoosePlayers()
System.out.println("Testing choosingPlayers 2");
inputData("2");
int actual = game.choosePlayers(num2);
assertEquals(num2, actual);
System.out.println("Testing choosingPlayers 3");
inputData("3");
actual = game.choosePlayers(num3);
assertEquals(num3, actual);
System.out.println("Testing choosingPlayers 4");
inputData("4");
actual = game.choosePlayers(num4);
assertEquals(num4, actual);
System.out.println("Test finsihed");
edited Mar 8 at 19:19
answered Mar 8 at 18:29
devminddevmind
1449
1449
Thank you for your advice, unfortunately when I run the test, the scanner from the method being tested Is still called and asks for an number input, negating the values I set in the test data (num1 , num2 etc). Do you have any advice concerning this?
– Emma Cox
Mar 8 at 18:40
int num
parameter confused me. See answer updated.
– devmind
Mar 8 at 19:21
add a comment |
Thank you for your advice, unfortunately when I run the test, the scanner from the method being tested Is still called and asks for an number input, negating the values I set in the test data (num1 , num2 etc). Do you have any advice concerning this?
– Emma Cox
Mar 8 at 18:40
int num
parameter confused me. See answer updated.
– devmind
Mar 8 at 19:21
Thank you for your advice, unfortunately when I run the test, the scanner from the method being tested Is still called and asks for an number input, negating the values I set in the test data (num1 , num2 etc). Do you have any advice concerning this?
– Emma Cox
Mar 8 at 18:40
Thank you for your advice, unfortunately when I run the test, the scanner from the method being tested Is still called and asks for an number input, negating the values I set in the test data (num1 , num2 etc). Do you have any advice concerning this?
– Emma Cox
Mar 8 at 18:40
int num
parameter confused me. See answer updated.– devmind
Mar 8 at 19:21
int num
parameter confused me. See answer updated.– devmind
Mar 8 at 19:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068303%2fjunit-test-scanner-input-issue%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown