First:
- Naming: you only use the _ char within SOME_CONSTANT; so your method would better be called
testPrintAdequateOption()
- Making the test itself a JUnit @Test; and then; instead of returning a boolean; you make a real assert within the test code; so that the test really fails on unexpected results (see below for how to do that)
- In case you really want to keep it this way, the name is slightly misleading. You would rather call your method
doesPrintAdequateOptionGiveCorrectResultsOn()
or something like that: methods returning a boolean should indicate in their name that they are about, well a boolean choice!
From a pure technical point the above code is OK. Besides the one design problem ... if testing content written to stdout is the "only" way to test your code; then you should look into make real testing possible.
In other words: your test is the best you can do; but the thing that you intend to test; respectively the fact that you have to do it this way ... is the real problem here!
Edit: I somehow overlooked that you were really asking for JUnit unit tests. Thus lets give a better answer and simply show how a real JUnit test should look like here:
@Test
public boolean testPrintAdequateOption() {
// overwrite stdout to allow for capturing print statement
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream sout = System.out;
System.setOut(ps);
// call method with specific input
String text = ...
double[] sol = ...
Formatter formatter = ...
printAdequateOption(text, sol, formatter);
System.out.flush();
System.setOut(sout);
assertThat(baos.toString(), is("expected output));
}
That is how a reasonable test would look like; to be executed using JUnit; for example within your eclipse IDE.
Please note: I am using the assertThat assert, together with the is hamcrest matcher. See here about those two.
And the funny thing is: when you write such a test, it becomes really obvious that you should simply change your logic; very much as tomkab suggests in his answer - you don't want that printAdequateOption()
produces a string and just prints that to System.out. Instead, you want a method that produces that string ... that you can directly. So no such detour as you are doing in your code!