• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple JUnit test failed

 
Vinod Vijay
Ranch Hand
Posts: 150
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to Junit test. After learning Java, I thought to learn Junit because it is always a good practice to perform junit test to make sure that our logic written is fine. I'm using junit 3.8.1 jar and eclipse as IDE . I have created a blank project with one java class 'Employee.java' which has a small method which returns a String. I created another test class 'TestEmployee.java' to carry the test of that small method.

I'm referring following youtube video link:-
http://www.youtube.com/watch?v=qU9DmFa45rc

Employee.java:-

TestEmployee.java:-

I tried frist with wrong string as argument in asserEquals() and that was fine. But when I tried with correct string "Hello Vinod!" that too didn't worked and failed. Any idea what happened. I uploaded the screen shot as well for your reference.
Thanks a lot in advacne.
JUnit_error.GIF
[Thumbnail for JUnit_error.GIF]
Screenshot
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the test executing to completion? What I mean by that is, are you getting a test failure or a test error?
 
Vinod Vijay
Ranch Hand
Posts: 150
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Is the test executing to completion? What I mean by that is, are you getting a test failure or a test error?


Please refer left hand side of screenshot. I believe it is failing as the color is in red and count 1 for 'Failures'.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod

I'm using the mobile version of this site and attachments don't appear - apologies. So the failure is on your assertEquals statement? The failure message should display expected and actual values, does it not?
 
Vinod Vijay
Ranch Hand
Posts: 150
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Vinod

I'm using the mobile version of this site and attachments don't appear - apologies. So the failure is on your assertEquals statement? The failure message should display expected and actual values, does it not?


James thanks for your suggestion. Well I looked into the warning message and I'm getting the following one:-

junit.framework.AssertionFailedError: No tests found in myPackage.TestEmployee
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.TestSuite$1.runTest(TestSuite.java:263)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Hey, you know what I created a seperate method instead of calling assertEquals() inside main() and it worked, like this:-


Now my question do we have to write always like this only and how come my newly created method was called testSayHello(). I didn't called it neither its a static method?
 
Akhilesh Trivedi
Ranch Hand
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:
junit.framework.AssertionFailedError: No tests found in myPackage.TestEmployee


 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod

It is as I suspected. There is nothing wrong with your test. JUnit cannot find any tests. I'm surprised a failure is reported when an AssertionFailedError is what is thrown.
 
Vinod Vijay
Ranch Hand
Posts: 150
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Vinod

It is as I suspected. There is nothing wrong with your test. JUnit cannot find any tests. I'm surprised a failure is reported when an AssertionFailedError is what is thrown.


Thanks James
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinod,

The method signature: public static void main(String[] args) is used mainly in functional/integration tests,

A default in JUnit is to name the method starting with "test" so the method signature could be: public void testEmployee()

If you are using Java 5.0, you could use JUnit 4+ and then you can annotate your testing method with @Test rather than extending the TestCase class

:)
 
Vinod Vijay
Ranch Hand
Posts: 150
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Niall Loughnane]Hi Vinod,

The method signature: public static void main(String[] args) is used mainly in functional/integration tests,

A default in JUnit is to name the method starting with "test" so the method signature could be: public void testEmployee()

If you are using Java 5.0, you could use JUnit 4+ and then you can annotate your testing method with @Test rather than extending the TestCase class

:)[/quote]

Thanks for your advice Niall :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic