Find file History
Ranga Rao Karanam Ranga Rao Karanam
Ranga Rao Karanam and Ranga Rao Karanam I'm too lazy to put in a comment
Latest commit e0ee2f5 Mar 19, 2018
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src
test
.DS_Store
.gitignore
code-21July2017.zip
readme.md
step22.md
step22.zip
step25.md
step25.zip

readme.md

First 5 Steps in JUnit

Step 1 : What is JUnit and Unit Testing?

  • What is JUnit?
  • What is Unit Testing?
  • Advantages of Unit Testing

Step 2 : First JUnit Project and Green Bar

  • What is JUnit?
  • First Project with JUnit
  • First JUnit Class
  • No Failure is Success
  • MyMath class with sum method

Step 3 : First Code and First Unit Test

  • Unit test for the sum method

Step 4 : Other assert methods

  • assertTrue and assertFalse methods

Step 5 : Important annotations

  • @Before @After annotations
  • @BeforeClass @AfterClass annotations

Complete Code Example

/src/com/in28minutes/junit/MyMath.java

package com.in28minutes.junit;

public class MyMath {
	int sum(int[] numbers) {
		int sum = 0;
		for (int i : numbers) {
			sum += i;
		}
		return sum;
	}
}

/test/com/in28minutes/junit/AssertTest.java

package com.in28minutes.junit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class AssertTest {

	@Test
	public void test() {
		boolean condn = true;
		assertEquals(true, condn);
		assertTrue(condn);
		// assertFalse(condn);
	}

}

/test/com/in28minutes/junit/MyMathTest.java

package com.in28minutes.junit;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyMathTest {
	MyMath myMath = new MyMath();

	@Before
	public void before() {
		System.out.println("Before");
	}

	@After
	public void after() {
		System.out.println("After");
	}

	@BeforeClass
	public static void beforeClass() {
		System.out.println("Before Class");
	}

	@AfterClass
	public static void afterClass() {
		System.out.println("After Class");
	}

	// MyMath.sum
	// 1,2,3 => 6
	@Test
	public void sum_with3numbers() {
		System.out.println("Test1");
		assertEquals(6, myMath.sum(new int[] { 1, 2, 3 }));
	}

	@Test
	public void sum_with1number() {
		System.out.println("Test2");
		assertEquals(3, myMath.sum(new int[] { 3 }));
	}
}