Testing Output using assertEquals
Scenario1.java

public class Scenario
{	
  public int Square(int no)
  {
    return no*no;
  }
}

SquareTest.java

public class SquareTest 
{
  @Test
  public void test() 
  {
    int SquareNo = new Test1().Square(5);		
    assertEquals(25, SquareNo);
  }
}

Scenario2.java(word Counter)

public class Scenario2 
{	
 public int repeatedWords(String pWord)
 {
   int count = 0;
	
   for (int j = 0; j < pWord.length(); j++) 
   {
     if(pWord.charAt(j) == 'a')
     {
       count++;	
     }
   }	
    return count;
 }
}

RepeatTest.java

public class RepeatTest {
   @Test
   public void test() {
     Scenario2 objScenario2 = new Scenario2();		
     assertEquals(objScenario2.repeatedWords("alphabet"), 2);		
   }
}

Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @SuiteClasses annotations are used to run the suite tests.

AllTests.java (TestSuite)

@RunWith(Suite.class)
@SuiteClasses({ RepeatTest.class, SquareTest.class })
public class AllTests {

}

Now Lets take a Real Life Scenario of Bank Account
Account.java

public class Account 
{
	public int Balance;
	
	public Account()
	{
		Balance = 0;
	}
	
	public int getAccBalance()
	{
		return Balance;
	}
	
	public void getCash(int pCash)
	{
		Balance = Balance - pCash;
	}
	
	public void putCash(int pCash)
	{
		Balance = Balance + pCash;
	}
}

BankTest.java

public class BankTest 
{
	Account objAcc = new Account();
	
	@Test
	public void checkAccBalanceTest()
	{
		assertEquals(objAcc.getAccBalance(), 0);
	}
	
	@Test
	public void checkBalAfterDepositTest()
	{
		objAcc.putCash(50);
		assertEquals(objAcc.getAccBalance(), 50);
	}
	
	@Test
	public void checkBalAfterWithdrawTest()
	{
		objAcc.getCash(30);		
		assertEquals(objAcc.getAccBalance(), 20);
	}
}

Points to Note

  1. The methods in BankTest ends with Test Suffix.This ensures the test cases are executed in order.
  2. The Account.java and BankTest.java are two different projects.In BankTest project the other project is added in Java Build Path

If I write something like this

 System.out.println(19);

Which type has the ’19’? Is it int or byte? Or doesn’t it have a type yet?
This 19 is known as an integer literal. There are all sorts of literals, floating point, String, character, etc.

What is integer literal
Integer data types consist of the following primitive data types: int,long, byte, and short.byte, int, long, and short can be expressed in decimal(base
10), hexadecimal(base 16) or octal(base 8) number systems as well.
Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals.

Examples:

int decimal = 100;
int octal = 0144;
int hexa =  0x64;

Literal means any number,Text or Other information that represents a value.

Different Values that can be assigned to Integer Variable (Integer data type Literal) are

  1. Decimal Literals
  2. Octal Literals
  3. Hexadecimal Literals
  4. Binary Literals
  5. Long Literals
  6. Values with Underscore in Between

Why main() method is Static

  1. the main function is where a program starts execution. It is responsible for the high-level organization of the program’s functionality, and typically has access to the command arguments given to the program when it was executed.
  2. main() is static because at that point in the application’s lifecycle, the application stack is procedural in nature due to there being no objects yet instantiated.It’s a clean slate. Your application is running at this point, even without any objects being declared (remember, there’s procedural AND OO coding patterns). You, as the developer, turn the application into an object-oriented solution by creating instances of your objects and depending upon the code compiled within.
  3. static allows main() to be called before an object of the class has been created. This is neccesary because main() is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class.
  4. The method is static because there would be ambiguity: which constructor should be called? Especially if your class looks like this:
    public class JavaClass
    {
      protected JavaClass(int x){}
    
      public void main(String[] args)
      {
      }
    }
    

    If the JVM call new JavaClass(int)? What should it pass for x?

What would be the Output

public class Test 
{
    static
    {
      System.out.println("Hi static 1");        
    }

    public static void main(String[] args) 
    {
        System.out.println("Hi main");
    }

    static
    {
        System.out.println("Hi static 2");        
    }
}

Output

Hi static 1
Hi static 2
Hi main

What happens when you run a class without main() method
Say, file Demo.java contains source code as

public class Demo{ 

}

when this code is compiled as it’s compile successfully as

$javac Demo.java

but when it’s executed as

$java Demo

then it shows an exceptional error

Main method not found in class Demo, please define the main method as: public static void main(String[] args)

so compiler is not responsible to check whether main() method is present or not. JVM is responsible for it. JVM check for main() method with prototype as

public static void main(Strings[] args)

Why JVM search main() method? Is it possible to change main() method into any other method main_hero()?
JVM is instructed to find main() method from inside JVM. Yes, it’s possible to change main() method into main_hero() method but inside JVM you must have to instruct to search main_hero() method.

Why public?
call from anywhere public is used.

Why void?
JVM is going to call main() method but what can JVM do with return value if main() method return.

Can we declare main() method as private or protected or with no access modifier?
No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.

Can We Declare main() Method As non-static?
No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class. If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.

Most of the Errors incase of change of main() method are runtime errors