my dog learned polymorphism*
The moose likes Testing and the fly likes Simple JUnit test failed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Simple JUnit test failed" Watch "Simple JUnit test failed" New topic
Author

Simple JUnit test failed

Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

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.



[Thumbnail for JUnit_error.GIF]



Vinod Vijay Nair
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: Sep 13, 2011
Posts: 137

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

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: Sep 13, 2011
Posts: 137

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

Joined: Jun 22, 2005
Posts: 1527
Vinod Vijay wrote:
junit.framework.AssertionFailedError: No tests found in myPackage.TestEmployee



Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: Sep 13, 2011
Posts: 137

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

Joined: Dec 07, 2006
Posts: 208
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

Joined: Sep 13, 2011
Posts: 137

[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 :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple JUnit test failed