Reputation: 91
Can somebody kindly answer the below question I have on Junit test execution.
1) I have a Java class SampleMessage.java that just prints a simple message
2) I wrote a Junit test class with method testSampleMessage to test the above Java class. Annoatated the method (@Test)
3) I created a runner Java class file and used the JUnitCore runner to run the test case I created in step 2.
Now, I don't see JUnit UI in Eclipse showing the green bar indicating test passed. I assumed that the JUnitCore runner would use JUnit framework to run the above test and produce that green/red bar in the JUnit tab. Does this not work like that? It just displayed the message in the Eclipse Console. What will Java do when I right click on the runner class and select run as/Java application. Does it not use JUnit?
Upvotes: 0
Views: 230
Reputation: 31648
I assumed that the JUnitCore runner would use JUnit framework to run the above test and produce that green/red bar in the JUnit tab.
Your assumption is wrong. JUnitCore will run your test if you told it to but will return a Result object with the test results. If you are using JUnitCore directly you will have to call the getters on that object to see if everything passed.
It is also possible to add a testListener to the JUnitCore object to get notification events of which tests are running at what their results are as they happen. This is actually how Eclipse's JUnit tab gets the information to display.
As @Rudi_Bravo mentioned in his answer, you don't need to use JUnitCore at all if you are just running your tests in eclipse. Right-clicking on the class (or method name) and selecting "Run as" > "JUnit test" will start Eclipse's JUnit plug in runner for you with the green/red bar.
Upvotes: 1
Reputation: 74
In Eclipse specifically you don't need the JUnitCore to run the tests. You can simply right click the Test class or the whole project and select 'Run as' -> 'JUnit test'
Upvotes: 2