├── .gitignore ├── junit-4.12.jar ├── hamcrest-core-1.3.jar ├── Engineer.java ├── Manager.java ├── README.md ├── Payroll.java ├── Employee.java └── EmployeeTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | \#*\# 3 | *.class 4 | -------------------------------------------------------------------------------- /junit-4.12.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/junit-demo/master/junit-4.12.jar -------------------------------------------------------------------------------- /hamcrest-core-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/junit-demo/master/hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /Engineer.java: -------------------------------------------------------------------------------- 1 | public class Engineer extends Employee { 2 | 3 | // Engineers make lots of money 4 | public double getYearlySalary() { 5 | if (this.yearlySalary < 150000) 6 | return 225000; 7 | return this.yearlySalary; 8 | } 9 | 10 | // Engineers can hack the payroll system 11 | public void setYearlySalary(double yearlySalary) { 12 | this.yearlySalary = yearlySalary*1.20; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Manager.java: -------------------------------------------------------------------------------- 1 | public class Manager extends Employee { 2 | 3 | // managers should get the most money 4 | public double getYearlySalary() { 5 | if (this.yearlySalary < 300000) 6 | return 500000; 7 | return this.yearlySalary; 8 | } 9 | 10 | public void setYearlySalary(double yearlySalary) { 11 | this.yearlySalary = yearlySalary; 12 | } 13 | 14 | // manager should always look older 15 | public int getAge() { 16 | if (super.getAge() < 50) 17 | return 50; 18 | return super.getAge(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # junit-demo 2 | Easy self-contained junit demo on a payroll application with basic object oriented programming 3 | 4 | 5 | ## To execute locally: 6 | 7 | * clone the repository 8 | 9 | git clone git@github.com:mimoralea/junit-demo.git 10 | 11 | 12 | * compile the application 13 | 14 | javac Payroll.java 15 | 16 | 17 | * test it out 18 | 19 | java Payroll 20 | 21 | 22 | * compile the tests 23 | 24 | javac -cp .:junit-4.12.jar EmployeeTest.java 25 | 26 | 27 | * run the tests 28 | 29 | java -cp .:junit-4.12.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore EmployeeTest 30 | -------------------------------------------------------------------------------- /Payroll.java: -------------------------------------------------------------------------------- 1 | public class Payroll { 2 | 3 | public static void main(String args[]) { 4 | 5 | // register an engineer 6 | Engineer eng = new Engineer(); 7 | eng.setFirstName("John"); 8 | eng.setLastName("Johnson"); 9 | eng.setAge(20); // this should stay the same 10 | eng.setEmployeeId("12345678"); 11 | eng.setYearlySalary(225000); // we should get more money 12 | 13 | System.out.println("Engineer registered successfully:\n" + 14 | "\tName:\t" + eng.getFullName() + "\n" + 15 | "\tAge:\t" + eng.getAge() + "\n" + 16 | "\tSalary\t$" + eng.getYearlySalary()); 17 | System.out.println(); 18 | 19 | Manager man = new Manager(); 20 | man.setFirstName("Peter"); 21 | man.setLastName("Peterson"); 22 | man.setAge(30); // don't they all look older? 23 | man.setEmployeeId("87654321"); 24 | man.setYearlySalary(500000); // no more than this please! 25 | 26 | System.out.println("Manager registered successfully:\n" + 27 | "\tName:\t" + man.getFullName() + "\n" + 28 | "\tAge:\t" + man.getAge() + "\n" + 29 | "\tSalary\t$" + man.getYearlySalary()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Employee.java: -------------------------------------------------------------------------------- 1 | /* abstract class cannot be instantiated 2 | * this class must be inherited in order to 3 | * be used */ 4 | public abstract class Employee { 5 | // protected properties are only visible to the 6 | // current class and its descendants 7 | protected double yearlySalary; 8 | 9 | // private properties are only accessible within the 10 | // current class 11 | private String firstName; 12 | private String lastName; 13 | private String employeeId; 14 | private int age; 15 | 16 | // Employee constructor 17 | // sets the default values for 18 | // all kinds of employees 19 | public Employee() { 20 | this.employeeId = "000000"; 21 | this.yearlySalary = 200000; 22 | } 23 | 24 | /* Getters and setters BEGIN */ 25 | public String getFirstName() { 26 | return this.firstName; 27 | } 28 | 29 | public String getLastName() { 30 | return this.lastName; 31 | } 32 | 33 | public String getFullName() { 34 | return this.getFirstName() + " " + this.getLastName(); 35 | } 36 | 37 | public String getEmployeeId() { 38 | return this.employeeId; 39 | } 40 | 41 | public int getAge() { 42 | return this.age; 43 | } 44 | 45 | public void setFirstName(String firstName) { 46 | this.firstName = firstName; 47 | } 48 | 49 | public void setLastName(String lastName) { 50 | this.lastName = lastName; 51 | } 52 | 53 | public void setEmployeeId(String employeeId) { 54 | this.employeeId = employeeId; 55 | } 56 | 57 | public void setAge(int age) { 58 | this.age = age; 59 | } 60 | /* Getters and setters END */ 61 | 62 | 63 | // abstract methods that will have to be overridden 64 | // by classes inheriting the current 65 | abstract double getYearlySalary(); 66 | abstract void setYearlySalary(double yearlySalary); 67 | } 68 | -------------------------------------------------------------------------------- /EmployeeTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | import static org.junit.Assert.*; 3 | 4 | public class EmployeeTest { 5 | 6 | @Test 7 | public void engineerMinimumSalary() { 8 | Engineer en1 = new Engineer(); 9 | en1.setYearlySalary(50000); 10 | 11 | Engineer en2 = new Engineer(); 12 | en2.setYearlySalary(100000); 13 | 14 | // Engineers make at least 225000 guaranteed 15 | assertTrue(en1.getYearlySalary() == 225000); 16 | assertTrue(en2.getYearlySalary() == 225000); 17 | } 18 | 19 | @Test 20 | public void managerMinimumSalary() { 21 | Manager ma1 = new Manager(); 22 | ma1.setYearlySalary(150000); 23 | 24 | Manager ma2 = new Manager(); 25 | ma2.setYearlySalary(250000); 26 | 27 | // Managers make at leas 500000 guaranteed 28 | assertTrue(ma1.getYearlySalary() == 500000); 29 | assertTrue(ma2.getYearlySalary() == 500000); 30 | } 31 | 32 | @Test 33 | public void engineerHackedSetSalary() { 34 | Engineer en1 = new Engineer(); 35 | en1.setYearlySalary(250000); 36 | 37 | Engineer en2 = new Engineer(); 38 | en2.setYearlySalary(400000); 39 | 40 | // Engineers get 20% more than the valid salary 41 | // they agreed upon 42 | assertTrue(en1.getYearlySalary() > 250000); 43 | assertTrue(en2.getYearlySalary() > 400000); 44 | } 45 | 46 | @Test 47 | public void managerHackedSetAge() { 48 | Manager ma1 = new Manager(); 49 | ma1.setAge(30); 50 | 51 | Manager ma2 = new Manager(); 52 | ma2.setAge(58); 53 | 54 | // No matter how old they are 55 | // Manager look old and should be at least 50 56 | assertTrue(ma1.getAge() == 50); 57 | assertTrue(ma2.getAge() == 58); 58 | } 59 | 60 | @Test 61 | public void engineerAreSameAge() { 62 | Engineer en1 = new Engineer(); 63 | en1.setAge(20); 64 | 65 | Engineer en2 = new Engineer(); 66 | en2.setAge(30); 67 | 68 | // Engineers stay forever young 69 | assertTrue(en1.getAge() == 20); 70 | assertTrue(en2.getAge() == 30); 71 | } 72 | } 73 | --------------------------------------------------------------------------------