Static method and static variable

In Java, static denotes class methods and class variables (as opposed to instance methods and instance variables). These methods and variables can be accessed without an instance present.

Contrast this to instance methods and instance variables: they must be accessed through an object. For example, length() operates on an object:

String a = "hello";
int len = a.length();

In contrast, valueOf cannot operate on an object; moreover, it creates a new object when called:

String x = String.valueOf(123.45);

Note how instance methods are called using <objectName> followed by a dot ., while static methods are accessed using <className> followed by a dot ..

Advertisements

JUnit: Unit Testing

JUnit is used for unit testing. The unit testing is one of the testing methodologies that tests module or method that was written.

Writing the test is done by creating normal java class, except we use special annotations for it.

Note: best practice in unit testing is to have multiple tests for single method.

Let’s say you have method calculateMinAge(), it’s good idea to create multiple test methods

Unlike other java classes, JUnit test class has different structure:

@Suit or @TestRetention <– this annotation helps to include the current class file into larger test group, containing other test files

@Before <Рthis annotation can be used to initialize the objects that are used in different methods. Also the method name should be called setUp(). Ex:

@Before

public void setUp(){}