Interesting behavior when simulating user console input when testing multithreaded Java application with JunitJUnit: How to simulate System.in testing?JUnit testing with simulated user inputHow can I unit test classes that read input in Java?Testing asynchronous code with JUnitTest java programs that read from stdin and write to stdoutSystem.in, System.out from stringsDetect when System.in is being queried in javaWhy is it not possible to change System.in more than once in Java?How to test a simple command line application in Java using JUnitWhen I run The Junit Test Case in Eclispe I am getting The errrJava JUnit testing program output with given test cases
What is the source of "You can achieve a lot with hate, but even more with love" (Shakespeare?)
All numbers in a 5x5 Minesweeper grid
What the did the controller say during my approach to land (audio clip)?
Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?
Is there a theorem in Real analysis similar to Cauchy's theorem in Complex analysis?
Who are the people reviewing far more papers than they're submitting for review?
All numbers twice in a 7x7 Minesweeper grid
Why do IXPs need ASN?
Talk about Grandpa's weird talk: Who are these folks?
Can I separate garlic into cloves for storage?
Unpredictability of Stock Market
How to make classical firearms effective on space habitats despite the coriolis effect?
Python web-scraper to download table of transistor counts from Wikipedia
How to give my students a straightedge instead of a ruler
Bash attempts to write two shell prompts?
Is a global DNS record a security risk for phpMyAdmin?
Plausibility and performance of a composite longbow
Is it safe to unplug a blinking USB drive after 'safely' ejecting it?
Problem of Induction: Dissolved
Why is belonging not transitive?
Is the name of an interval between two notes unique and absolute?
Can we have a C++ function with multiple return types? ( C++11 and above)
Delete empty subfolders, keep parent folder
Persuading players to be less attached to a pre-session 0 character concept
Interesting behavior when simulating user console input when testing multithreaded Java application with Junit
JUnit: How to simulate System.in testing?JUnit testing with simulated user inputHow can I unit test classes that read input in Java?Testing asynchronous code with JUnitTest java programs that read from stdin and write to stdoutSystem.in, System.out from stringsDetect when System.in is being queried in javaWhy is it not possible to change System.in more than once in Java?How to test a simple command line application in Java using JUnitWhen I run The Junit Test Case in Eclispe I am getting The errrJava JUnit testing program output with given test cases
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to develop an automatic assignment grading script using JUnit in Java. I am simulating user input by passing in test case inputs (read from a file in the classpath) by changing System.in to a ByteArrayInputStream containing the string data in bytes. I want to be able to run multiple test cases to check all cases and then use the Gradle-built report to mark the assignment submission.
Currently, I have been able to simulate user input to reflect the input provided by the test cases using this link: JUnit: How to simulate System.in testing?. I am also changing System.out to a PrintStream to capture the printed statements from the assignment. My main method spawns a new thread, inside which is the assignment's codes done in, as shown below.
The unit tests perform successfully when I am testing each @Test - annotated case individually. However, when I run all the tests contained in a single Test class, JUnit works successfully in the first @Test -annotated case but fails on the eventual ones, which is somewhat unexpected here. Hopefully the codes below will help to clarify the situation.
My Main method:
public static void main(String ... args)
Assignment assignment = new Assignment();
new Thread(assignment).start();
My test class has some @before and @after annotated methods:
@Before
public void setup()
systemIn = System.in; // global variable
systemOut = System.out; // global variable
output = new ByteArrayOutputStream(); // global variable
System.setOut(new PrintStream(output));
assignment = new Assignment();
@After
public void conclude()
System.setIn(systemIn);
System.setOut(systemOut);
I am reading the corresponding file for each test case input and output, and in the function below, I am setting the test case input.
private void setInput(String data)
ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes());
System.setIn(input);
I am running each test case as follows:
@org.junit.Test // 2,3,4 ....for other cases
public void runTestCase1()
testCases(1);
The IO class the assignment is using is as follows:
public static String readLine() // w/o try/catch blocks
String read = reader.readLine(); // reader -> bufferedReader(new InputStream(System.in))
return read.trim();
Also note that I am using Thread.sleep(50) before invoking the main method so that the input test case is successfully read and loaded in System.in in time for Assignment to execute.
When running all tests together, the first test gives the correct output, i.e. the expected and the actual outputs match. However, for the second case and onward, IO.readline() generates a null pointer exception and hence no output is produced by the assignment. Upon investigation, I discovered that during the second case, System.in is no longer pointing to a ByteArrayInputStream, but rather (the old?) BufferedInputStream. I am confused why this might be happening (probably due to a new thread spawning maybe?) and how do I overcome this problem?
java multithreading junit jvm system.in
add a comment
|
I am trying to develop an automatic assignment grading script using JUnit in Java. I am simulating user input by passing in test case inputs (read from a file in the classpath) by changing System.in to a ByteArrayInputStream containing the string data in bytes. I want to be able to run multiple test cases to check all cases and then use the Gradle-built report to mark the assignment submission.
Currently, I have been able to simulate user input to reflect the input provided by the test cases using this link: JUnit: How to simulate System.in testing?. I am also changing System.out to a PrintStream to capture the printed statements from the assignment. My main method spawns a new thread, inside which is the assignment's codes done in, as shown below.
The unit tests perform successfully when I am testing each @Test - annotated case individually. However, when I run all the tests contained in a single Test class, JUnit works successfully in the first @Test -annotated case but fails on the eventual ones, which is somewhat unexpected here. Hopefully the codes below will help to clarify the situation.
My Main method:
public static void main(String ... args)
Assignment assignment = new Assignment();
new Thread(assignment).start();
My test class has some @before and @after annotated methods:
@Before
public void setup()
systemIn = System.in; // global variable
systemOut = System.out; // global variable
output = new ByteArrayOutputStream(); // global variable
System.setOut(new PrintStream(output));
assignment = new Assignment();
@After
public void conclude()
System.setIn(systemIn);
System.setOut(systemOut);
I am reading the corresponding file for each test case input and output, and in the function below, I am setting the test case input.
private void setInput(String data)
ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes());
System.setIn(input);
I am running each test case as follows:
@org.junit.Test // 2,3,4 ....for other cases
public void runTestCase1()
testCases(1);
The IO class the assignment is using is as follows:
public static String readLine() // w/o try/catch blocks
String read = reader.readLine(); // reader -> bufferedReader(new InputStream(System.in))
return read.trim();
Also note that I am using Thread.sleep(50) before invoking the main method so that the input test case is successfully read and loaded in System.in in time for Assignment to execute.
When running all tests together, the first test gives the correct output, i.e. the expected and the actual outputs match. However, for the second case and onward, IO.readline() generates a null pointer exception and hence no output is produced by the assignment. Upon investigation, I discovered that during the second case, System.in is no longer pointing to a ByteArrayInputStream, but rather (the old?) BufferedInputStream. I am confused why this might be happening (probably due to a new thread spawning maybe?) and how do I overcome this problem?
java multithreading junit jvm system.in
add a comment
|
I am trying to develop an automatic assignment grading script using JUnit in Java. I am simulating user input by passing in test case inputs (read from a file in the classpath) by changing System.in to a ByteArrayInputStream containing the string data in bytes. I want to be able to run multiple test cases to check all cases and then use the Gradle-built report to mark the assignment submission.
Currently, I have been able to simulate user input to reflect the input provided by the test cases using this link: JUnit: How to simulate System.in testing?. I am also changing System.out to a PrintStream to capture the printed statements from the assignment. My main method spawns a new thread, inside which is the assignment's codes done in, as shown below.
The unit tests perform successfully when I am testing each @Test - annotated case individually. However, when I run all the tests contained in a single Test class, JUnit works successfully in the first @Test -annotated case but fails on the eventual ones, which is somewhat unexpected here. Hopefully the codes below will help to clarify the situation.
My Main method:
public static void main(String ... args)
Assignment assignment = new Assignment();
new Thread(assignment).start();
My test class has some @before and @after annotated methods:
@Before
public void setup()
systemIn = System.in; // global variable
systemOut = System.out; // global variable
output = new ByteArrayOutputStream(); // global variable
System.setOut(new PrintStream(output));
assignment = new Assignment();
@After
public void conclude()
System.setIn(systemIn);
System.setOut(systemOut);
I am reading the corresponding file for each test case input and output, and in the function below, I am setting the test case input.
private void setInput(String data)
ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes());
System.setIn(input);
I am running each test case as follows:
@org.junit.Test // 2,3,4 ....for other cases
public void runTestCase1()
testCases(1);
The IO class the assignment is using is as follows:
public static String readLine() // w/o try/catch blocks
String read = reader.readLine(); // reader -> bufferedReader(new InputStream(System.in))
return read.trim();
Also note that I am using Thread.sleep(50) before invoking the main method so that the input test case is successfully read and loaded in System.in in time for Assignment to execute.
When running all tests together, the first test gives the correct output, i.e. the expected and the actual outputs match. However, for the second case and onward, IO.readline() generates a null pointer exception and hence no output is produced by the assignment. Upon investigation, I discovered that during the second case, System.in is no longer pointing to a ByteArrayInputStream, but rather (the old?) BufferedInputStream. I am confused why this might be happening (probably due to a new thread spawning maybe?) and how do I overcome this problem?
java multithreading junit jvm system.in
I am trying to develop an automatic assignment grading script using JUnit in Java. I am simulating user input by passing in test case inputs (read from a file in the classpath) by changing System.in to a ByteArrayInputStream containing the string data in bytes. I want to be able to run multiple test cases to check all cases and then use the Gradle-built report to mark the assignment submission.
Currently, I have been able to simulate user input to reflect the input provided by the test cases using this link: JUnit: How to simulate System.in testing?. I am also changing System.out to a PrintStream to capture the printed statements from the assignment. My main method spawns a new thread, inside which is the assignment's codes done in, as shown below.
The unit tests perform successfully when I am testing each @Test - annotated case individually. However, when I run all the tests contained in a single Test class, JUnit works successfully in the first @Test -annotated case but fails on the eventual ones, which is somewhat unexpected here. Hopefully the codes below will help to clarify the situation.
My Main method:
public static void main(String ... args)
Assignment assignment = new Assignment();
new Thread(assignment).start();
My test class has some @before and @after annotated methods:
@Before
public void setup()
systemIn = System.in; // global variable
systemOut = System.out; // global variable
output = new ByteArrayOutputStream(); // global variable
System.setOut(new PrintStream(output));
assignment = new Assignment();
@After
public void conclude()
System.setIn(systemIn);
System.setOut(systemOut);
I am reading the corresponding file for each test case input and output, and in the function below, I am setting the test case input.
private void setInput(String data)
ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes());
System.setIn(input);
I am running each test case as follows:
@org.junit.Test // 2,3,4 ....for other cases
public void runTestCase1()
testCases(1);
The IO class the assignment is using is as follows:
public static String readLine() // w/o try/catch blocks
String read = reader.readLine(); // reader -> bufferedReader(new InputStream(System.in))
return read.trim();
Also note that I am using Thread.sleep(50) before invoking the main method so that the input test case is successfully read and loaded in System.in in time for Assignment to execute.
When running all tests together, the first test gives the correct output, i.e. the expected and the actual outputs match. However, for the second case and onward, IO.readline() generates a null pointer exception and hence no output is produced by the assignment. Upon investigation, I discovered that during the second case, System.in is no longer pointing to a ByteArrayInputStream, but rather (the old?) BufferedInputStream. I am confused why this might be happening (probably due to a new thread spawning maybe?) and how do I overcome this problem?
java multithreading junit jvm system.in
java multithreading junit jvm system.in
edited Apr 3 at 3:54
sbsatter
asked Mar 28 at 13:23
sbsattersbsatter
11412 bronze badges
11412 bronze badges
add a comment
|
add a comment
|
0
active
oldest
votes
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/4.0/"u003ecc by-sa 4.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%2f55398750%2finteresting-behavior-when-simulating-user-console-input-when-testing-multithread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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%2f55398750%2finteresting-behavior-when-simulating-user-console-input-when-testing-multithread%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