Karrasankar158@gmail.
com
Java Tools
▪ CVS
▪ Log4J
▪ JUnit
▪ SVN
▪ MAVEN
▪ Agile
JUNIT
▪ Introduction
▪ Unit Testing
▪ Terminology
▪ Unit Testing with JUnit
▪ Installation of JUnit
▪ JUnit
▪ Introduction to JUnit API
▪ Test
▪ TestCase
▪ Assert
▪ TestRunner
▪ TestSuite
▪ Preparation
▪ Create a Java class
▪ Create a JUnit test
▪ Run your test cases
1
Karrasankar158@[Link]
▪ Run your test via code
▪ JUnit 4.x
▪ Static imports with Eclipse
▪ Annotations
▪ Assert statement
Example 1
[Link]
package com;
import [Link];
public class Person {
int a,b;
Logger log = [Link]();
public int add(int a, int b){
this.a=a;
this.b=b;
[Link]("entered value for a ="+a);
[Link]("entered value for b ="+b);
return a+b;
}
public static void main(String[] args) {
Person p=new Person();
[Link](2,4);
}
[Link]
package com;
2
Karrasankar158@[Link]
import static [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class PersonTest {
Person p=null;
@BeforeClass
public static void setUpBeforeClass() throws
Exception {
[Link]("setUpBeforeClass");
}
@AfterClass
public static void tearDownAfterClass()
throws Exception {
[Link]("tearDownAfterClass");
}
@Before
public void setUp() throws Exception {
[Link]("setUp");
p=new Person();
}
@After
public void tearDown() throws Exception {
[Link]("tearDown");
p=null;
}
@Test
public void testAdd() {
[Link](10,[Link](2,8));
3
Karrasankar158@[Link]
@Test
public void testMain() {
[Link]("main success");
}
output :
setUpBeforeClass
setUp
Dec 13, 2014 4:00:43 PM [Link] add
INFO: entered value for a =2
tearDown
Dec 13, 2014 4:00:43 PM [Link] add
INFO: entered value for b =8
setUp
main success
tearDown
tearDownAfterClass
Example 2
[Link]
package com;
import static [Link];
import [Link];
public class TestJunit {
@Test
public void testAdd(){
String str="Junit is working fine";
assertEquals("Junit is working fine", str);
}
4
Karrasankar158@[Link]
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class TestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link]);
for(Failure failure:[Link]()){
[Link]([Link]());
}
[Link]([Link]());
}//main
output :
true
JUnit write Tests :
[Link]
package com;
public class EmployeeDetails {
private String name;
private int age;
private double monthlySalary;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
5
Karrasankar158@[Link]
}
public int getAge() {
return age;
}
public void setAge(int age) {
[Link] = age;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary)
{
[Link] = monthlySalary;
}
}//class
[Link]
package com;
public class EmpBusinessLogic {
public double
calculateYearlySalary(EmployeeDetails empDetails){
double yearlySalary=0;
yearlySalary=[Link]()*12;
return yearlySalary;
}
public double calculateAppraisal(EmployeeDetails
empDetails){
double appraisal=0;
if([Link]()<10000){
appraisal=500;
}
else{
appraisal=1000;
}
return appraisal;
}
6
Karrasankar158@[Link]
}
[Link]
package com;
import static [Link];
import [Link];
public class EmpTestCase {
EmpBusinessLogic empLogic=new EmpBusinessLogic();
EmployeeDetails empDetails=new EmployeeDetails();
@Test
public void testCalculateAppraisal(){
[Link]("Ashok");
[Link](27);
[Link](8000d);
double
appraisal=[Link](empDetails);
assertEquals(500, appraisal,0.0);
}
@Test
public void testCalculateYearlySalary(){
[Link]("Ashok");
[Link](27);
[Link](8000d);
double
salary=[Link](empDetails);
assertEquals(96000,salary,0.0);
}
}//class
[Link]
package com;
import [Link];
7
Karrasankar158@[Link]
import [Link];
import [Link];
public class EmpTestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link]);
for(Failure failure : [Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
output :
true
JUNIT using Assertion :
methods :
void assertEquals(boolean expected, boolean actual)
void assertTrue(boolean expected, boolean actual)
void assertFalse(boolean condition)
void assertNotNull(Object object)
void assertNull(Object object)
void assertSame(boolean condition)
void assertNotSame(boolean condition)
void assertArrayEquals(expectedArray, resultArray);
8
Karrasankar158@[Link]
Ex :
[Link]
package com;
import static [Link];
import static [Link];
import static [Link];
import static [Link];
import static [Link];
import static [Link];
import static [Link];
import static [Link].*;
import [Link];
public class AssertionTestCase {
@Test
public void testAssertions(){
String s1=new String("abc");
String s2=new String("abc");
String s3=null;
String s4="abc";
String s5="abc";
int i1=234;
int i2=456;
String[] expectedArray={"one","two","three"};
String[] resultArray={"one","two","three"};
assertEquals(s1,s2);
assertTrue(i1<i2);
assertFalse(i1>i2);
assertNotNull(s1);
assertNull(s3);
assertSame(s4,s5);
assertNotSame(s1,s2);
assertArrayEquals(expectedArray,resultArray);
9
Karrasankar158@[Link]
}
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class AssertionTestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link]
s);
for(Failure failure:[Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
}
Annotation :
@Test
@Before
@After
@BeforeClass
@AfterClass
@Ignore
[Link]
10
Karrasankar158@[Link]
package com;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AnnotationTestCase {
@BeforeClass
public static void beforeClass(){
[Link]("in before class");
}
@AfterClass
public static void afterClass(){
[Link]("in after class");
}
@Before
public void before(){
[Link]("in before");
}
@After
public void after(){
[Link]("in after");
}
@Test
public void test(){
[Link]("in test");
}
@Ignore
public void ignoreTest(){
[Link]("in ignore test");
}
11
Karrasankar158@[Link]
}//class
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class AnnotationTestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link]
ss);
for(Failure failure:[Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
output:
in before class
in before
in test
in after
in after class
true
JUNIT Executing Tests
[Link]
package com;
public class MessageApp {
private String message;
public MessageApp(String message) {
12
Karrasankar158@[Link]
[Link]=message;
}
public String printMessage(){
[Link](message);
return message;
}//printMessage
}
[Link]
package com;
import static [Link];
import [Link];
public class MessageTestCase {
String message="Hello";
MessageApp messageApp=new MessageApp(message);
@Test
public void testPrintMessage(){
message="new word"; //--> 1
assertEquals(message,[Link]());
}
}//class
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class MessageTestRunner {
public static void main(String[] args) {
13
Karrasankar158@[Link]
Result
result=[Link]([Link])
;
for(Failure failure:[Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
output :
Hello
testPrintMessage([Link]):
expected:<[new word]> but was:<[Hello]>
false
in the above program, if we comments line (1) the output
is : true
JUNIT Test Suit
[Link]
package com;
public class MessageUtil {
private String message;
public MessageUtil(String message) {
[Link]=message;
}
public String printMessage(){
[Link](message);
return message;
}
public String salutationMessage(){
message="Hi "+message;
14
Karrasankar158@[Link]
[Link](message);
return message;
}
}
[Link]
package com;
import static [Link];
import [Link];
public class MessageTestCaseOne {
String message="Ashok";
MessageUtil messageUtil=new MessageUtil(message);
@Test
public void testPrintMessage(){
[Link]("inside testPrintMessage()");
assertEquals(message,[Link]());
}
}//class
[Link]
package com;
import static [Link];
import [Link];
public class MessageTestCaseTwo {
String message="Ashok";
MessageUtil messageUtil=new MessageUtil(message);
@Test
public void testSalutationMessage(){
[Link]("in testSalutationTest()");
message="Hi "+"Ashok";
15
Karrasankar158@[Link]
assertEquals(message,[Link]
());
}
}//class
[Link]
package com;
import [Link];
import [Link];
@RunWith([Link])
@[Link]({
[Link],
[Link]
})
public class MessageTestSuit {
}
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class MessageTestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link])
;
for(Failure failure:[Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
16
Karrasankar158@[Link]
output :
inside testPrintMessage()
Ashok
in testSalutationTest()
Hi Ashok
true
JUNIT Ignore Test
[Link]
package com;
public class MessageUtil {
private String message;
public MessageUtil(String message) {
[Link]=message;
}
public String printMessage(){
[Link](message);
return message;
}
public String salutationMessage(){
message="Hi!"+message;
[Link](message);
return message;
}
}
[Link]
package com;
17
Karrasankar158@[Link]
import static [Link];
import [Link];
import [Link];
public class MessageTestCase {
String message="Ashok";
MessageUtil messageUtil=new MessageUtil(message);
@Ignore
@Test
public void testPrintMessage(){
[Link]("inside testPrintMessage()");
assertEquals(message,[Link]());
}
@Test
public void testSalutationMessage(){
[Link]("in testSalutationTest()");
message="Hi!"+"Ashok";
assertEquals(message,[Link]
());
}
}//class
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class MessageTestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link])
;
for(Failure failure:[Link]()){
18
Karrasankar158@[Link]
[Link]([Link]());
}//for
[Link]([Link]());
}//main
output:
in testSalutationTest()
Hi!Ashok
true
package com;
import static [Link];
import [Link];
import [Link];
@Ignore
public class MessageTestCase {
String message="Ashok";
MessageUtil messageUtil=new MessageUtil(message);
@Test
public void testPrintMessage(){
[Link]("inside testPrintMessage()");
assertEquals(message,[Link]());
}
@Test
public void testSalutationMessage(){
[Link]("in testSalutationTest()");
message="Hi!"+"Ashok";
assertEquals(message,[Link]
());
}
19
Karrasankar158@[Link]
}//class
output: true
JUNIT Time Test
[Link]
package com;
public class MessageUtil {
private String message;
public MessageUtil(String message) {
[Link]=message;
}
public void printMessage(){
[Link](message);
while(true);
}
public String salutationMessage(){
message="Hi!"+message;
[Link](message);
return message;
}
}
[Link]
package com;
import static [Link];
import [Link];
public class MessageTestCase {
String message="Ashok";
MessageUtil messageUtil=new MessageUtil(message);
20
Karrasankar158@[Link]
@Test(timeout=1000)
public void testPrintMessage(){
[Link]("inside testPrintMessage()");
[Link]();
}
@Test
public void testSalutationMessage(){
[Link]("in testSalutationTest()");
message="Hi!"+"Ashok";
assertEquals(message,[Link]
());
}
}//class
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class MessageTestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link])
;
for(Failure failure:[Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
output :
21
Karrasankar158@[Link]
in testSalutationTest()
Hi!Ashok
inside testPrintMessage()
Ashok
testPrintMessage([Link]):
test timed out after 1000 milliseconds
false
JUNIT Exception Test
[Link]
package com;
public class MessageUtil {
private String message;
public MessageUtil(String message) {
[Link]=message;
}
public void printMessage(){
[Link](message);
int a=0;
int b=1/a;
}
public String salutationMessage(){
message="Hi!"+message;
[Link](message);
return message;
}
}
[Link]
package com;
import static [Link];
import [Link];
public class MessageTestCase {
22
Karrasankar158@[Link]
String message="Ashok";
MessageUtil messageUtil=new MessageUtil(message);
@Test(expected=[Link])
public void testPrintMessage(){
[Link]("inside testPrintMessage()");
[Link]();
}
@Test
public void testSalutationMessage(){
[Link]("in testSalutationTest()");
message="Hi!"+"Ashok";
assertEquals(message,[Link]
());
}
}//class
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class MessageTestRunner {
public static void main(String[] args) {
Result
result=[Link]([Link])
;
for(Failure failure:[Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
23
Karrasankar158@[Link]
output:
in testSalutationTest()
Hi!Ashok
inside testPrintMessage()
Ashok
true
JUNIT Parameterized Test
[Link]
package com;
public class PrimeNumberChecker {
public Boolean validate(final Integer
primeNumber){
for(int i=2;i<(primeNumber/2);i++){
if(primeNumber%i==0)
return false;
}//for
return true;
}//validate
}
[Link]
package com;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import static [Link];
@RunWith([Link])
public class PrimeCheckerTestCase {
24
Karrasankar158@[Link]
private Integer inputNumber;
private boolean expectedResult;
private PrimeNumberChecker primeNumberChecker;
@Before
public void initialize(){
primeNumberChecker=new PrimeNumberChecker();
[Link]("in initialize");
}
public PrimeCheckerTestCase(Integer inputNumber,
boolean
expectedResult){
[Link]=inputNumber;
[Link]=expectedResult;
[Link]("constructor");
}
@[Link]
public static Collection primeNumbers(){
[Link]("In primeNumbers()");
return [Link](new Object[][]{
{2,true},
{6,false},
{19,true},
{22,false},
{23,true}
});
}//primeNumbers
@Test
public void testPrimeNumberChecker(){
[Link]("parameterized No is:
"+inputNumber);
assertEquals(expectedResult,[Link]
idate(inputNumber));
}
}//class
25
Karrasankar158@[Link]
[Link]
package com;
import [Link];
import [Link];
import [Link];
public class PrimeTestRunner {
public static void main(String[] args) {
Result
result=[Link](PrimeCheckerTestCase.c
lass);
for(Failure failure:[Link]()){
[Link]([Link]());
}//for
[Link]([Link]());
}//main
output:
In primeNumbers()
constructor
in initialize
parameterized No is: 2
constructor
in initialize
parameterized No is: 6
constructor
in initialize
parameterized No is: 19
constructor
in initialize
26
Karrasankar158@[Link]
parameterized No is: 22
constructor
in initialize
parameterized No is: 23
true
Agile Methodology
Agility in Software Development :
1. Agile Model
2. Agile Development and Principles
Agile Software Development Methodologies :
1. Extreme Programming (XP) :
1. Documents and Artifacts
2. Roles
3. Process
▪ primary technical practices of XP
▪ corollary technical practices of XP
▪ Stand-Up Meetings
2. Crystal
0. Crystal Clear
1. Crystal Orange
3. Scrum
0. Overview
1. Documents and Artifacts
2. Roles
3. Process
27
Karrasankar158@[Link]
4. Feature-Driven Development (FDD)
0. Documents and Artifacts
1. Roles
2. Process
Introduction :
Agility in Software Development :
1. Agile Model
2. Agile Development and Principles
Agile Software Development
Methodologies
Agile development methodologies are emerging in the
software industry.
we provide an introduction to agile development
methodologies and an overview of 4 specific
methodologies:
1. Extreme Programming
2. Crystal Methods
3. Scrum
4. Feature Driven Development
Extreme Programming (XP)
1. communication
2. simplicity
28
Karrasankar158@[Link]
3. feedback
4. courage
5. respect
Documents and Artifacts
▪ User story cards, paper index cards
▪ Task list
▪ CRC cards (optional)
▪ Customer acceptance tests
▪ Visible Wall Graphs
Roles
▪ Manager
▪ Coach
▪ Tracker
▪ Programmer
▪ Tester
▪ Customer
Process
primary technical practices of XP (13)
1. Sit together
2. Whole team
3. Informative workspace
4. Energized work
5. Pair programming
6. Stories
7. Weekly cycle
29
Karrasankar158@[Link]
8. Quarterly cycle
9. Slack
10. Ten-minute build
11. Test-first programming
12. Continuous integration
13. Incremental design
corollary technical practices of XP (11)
1. Real customer involvement
2. Incremental deployment
3. Team continuity
4. Shrinking team
5. Root cause analysis
6. Shared code
7. Code and tests
8. Daily deployment
9. Negotiated scope contract
10. Pay-per-use
11. Stand-Up Meetings :
1. What he or she accomplished the prior day
2. What he or she plans to do today
3. Any obstacles or difficulties he or she is experiencing
Crystal Methods
Crystal Clear
1. Documents and artifacts
2. Roles
30
Karrasankar158@[Link]
3. Process
Crystal Orange
1. Documents and artifacts
2. Roles
3. Process
Scrum
Overview
Documents and Artifacts
1. Product Backlog
2. Sprint Backlog
3. Sprint Burndown chart
Roles
▪ Product Owner
▪ Scrum Master
▪ Developer
Process
Feature Driven Development (FDD)
Documents and Artifacts
1. Feature lists
2. Design packages
3. Track by Feature
4. "Burn Up" Chart
31
Karrasankar158@[Link]
Roles
▪ Project manager
▪ Chief architect
▪ Development manager
▪ Chief programmer
▪ Class owner
▪ Domain experts
▪ Feature teams
Process
1. Develop an overall model
2. Build a features list
3. Plan by feature
4. Design by feature
5. Build by feature
32