Monday, December 5, 2011

JUnit or TestNG annotations gives syntax error

Sometimes JUnit or TestNG annotations gives syntax error eventhough we have imported all the necessary packages. It gives the error "Syntax error, annotations are only available if source level is 5.0". When you hover at the annotations, you will be able to see this error.

Actually nothing wrong with your JUnit or TestNG packages/jars. This is something to do with your Eclipse setup. This will happen if you have JDK 1.3 or 1.4 installed or configured with your Eclipse. If you have JDK 1.5 or 1.6 installed/configured you can fix this issue. Make sure which version of JDK has been installed/configured.

Follow these steps to get this issue fixed.

1) Eclipse > Window Menu > Preferences - Expand Java - Select Compiler - Check the version of JDK Compilance level.
2) If you see JDK 1.3 or 1.4, select JDK 1.5. or 1.6 for all the options and click on Apply.