Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit method not found

Tags:

I'm trying to build a sample test class using junit framework.
I've downloaded junit4.9b3.
When I try to complie my test class I get the following error:-

javac -cp ".;C:\Documents and Settings\user\Desktop\junit\junit4.9b3\junit-4.9b3.jar"      TestSubscription.java     TestSubscription.java:10: cannot find symbol     symbol  : method assertTrue(boolean)     location: class TestSubscription                 assertTrue(s.pricePerMonth()==100.0);                     ^ TestSubscription.java:17: cannot find symbol     symbol  : method assertTrue(boolean)     location: class TestSubscription                 assertTrue(s.pricePerMonth()==66.67);             ^ 2 errors 

Looks like assertTrue is not available but the junit javadoc mentions this method.
I'm using the import as follows

import org.junit.*;      import org.junit.Assert.*; 

Any ideas?

like image 285
TL36 Avatar asked Jul 21 '11 07:07

TL36


People also ask

What is assertThat in JUnit?

The assertThat is one of the JUnit methods from the Assert object that can be used to check if a specific value match to an expected one. It primarily accepts 2 parameters. First one if the actual value and the second is a matcher object.

How do I resolve NoClassDefFoundError in JUnit?

To resolve module dependency, we use the module path. However, adding external jars in the module path does not make them available for the class loader. Hence the class loader considers them as missing dependencies and throws the NoClassDefFoundError.


2 Answers

You've imported the types, but not used a static import to make the members available without qualification. If you use:

import static org.junit.Assert.*; 

then that should statically import all the static methods in the Assert class, so you can write assertTrue instead of Assert.assertTrue.

Note that presumably Assert itself has nested types, otherwise I'd have expected your "normal" import to fail.

like image 72
Jon Skeet Avatar answered Sep 21 '22 06:09

Jon Skeet


You have to do a static import.

import static org.junit.Assert.*; 
like image 41
Thomas Jung Avatar answered Sep 22 '22 06:09

Thomas Jung