1

In the book "Next Generation Java Testing: TestNG and Advanced Concepts", it is said that "Annotations are strongly typed, so the compiler will flag any mistakes right away.". I know that strongly typed language checks the type of a variable before performing an operation on it, whereas a weakly typed language does not. In addition, strongly typed languages require explicit casts, while weakly typed languages perform implicit casts. So, what does the phrase "strongly typed" mean here?

learningQA
  • 641
  • 1
  • 5
  • 20

1 Answers1

1

What the book is most likely trying to do, is distinguish between the following two approaches:

  1. Use method name conventions to identify test methods
    This was the case in (for example) JUnit 3, where the name of the test method would be prefixed with test (e.g. testSomeLogic()). This allowed the test library to distinguish between those methods that were actually considered tests, and other setup or utility methods.

  2. Use annotations to identify test methods
    In TestNG and later versions of JUnit, the approach has been to prefer annotating the methods with specific test-related annotations (e.g. @Test). The goal here is the same, it allows the test library to correctly identify test and other test-related methods.

The benefit of using annotations is that they are actual Java types and will be checked by the compiler. Mistakenly using the annotation @Tset will result in a compilation error, alerting the user to the problem.

Spelling mistakes in method names can not be detected by the compiler. A method tsetSomeLogic() will just not be executed, and the user will be left unaware.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Thanks for the answer. By the way, you said that annotations are Java types. What do you mean by Java types? – learningQA Jun 22 '18 at 04:14
  • 1
    See [here](https://stackoverflow.com/questions/16600750/difference-between-class-and-type). – Robby Cornelissen Jun 22 '18 at 04:18
  • Okay. Data type. I get it. String, Int,... are the built-in data types of the Java language. Is @Test a built-in type of Java language or is it linked to Java language through TestNG framework? Can you please clarify? – learningQA Jun 22 '18 at 04:25
  • 1
    It's an annotation type defined by the TestNG framework. The concept of annotation types itself is inherent to the Java language (since Java 5). – Robby Cornelissen Jun 22 '18 at 04:30
  • Ok. So Cedric Beust used the built-in annotation types of Java language and wrote the TestNG framework. Am I correct? – learningQA Jun 22 '18 at 04:32
  • 1
    Cedric Beust created a series of new annotation types to use in his framework. The fact that he was able to create annotation types is because the Java language defined the concept and behavior of annotation types in Java 5. – Robby Cornelissen Jun 22 '18 at 04:34
  • Ok. Let me understand myself with an analogy. We create new data types in the form of classes using the built-in data types of Java languages. Then we use those data types in other programs by importing those classes. Likewise, Cedric Beust created classes(data types) like Test, BeforeClass,... and we use them by importing the respective classes. Am I correct? – learningQA Jun 22 '18 at 04:39
  • Close enough :) – Robby Cornelissen Jun 22 '18 at 04:41
  • Ok, let me put it in another way for the sake of clarification. Classes are user-defined data types based on the built-in data types like String, int,.. Is @Test an user-defined data type based on the built-in annotation type and the user is Cedric Beust? – learningQA Jun 22 '18 at 04:44
  • It's not a data type but an annotation type, created by Cedric Beust and used by Cedric Beust, me, and probably a gazillion others. – Robby Cornelissen Jun 22 '18 at 04:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173591/discussion-between-learningqa-and-robby-cornelissen). – learningQA Jun 22 '18 at 04:49