2. Outline
• Problem ?
• What's IoC ?
• Method to make your software components
testable
3. Problem ? (1/3)
/// pseudo code for CheckAuthentication
/// input: string id
/// input: string password
/// output: true --> if pass the checking
/// false --> else fail the checking
Validation depends on Class AccountDAO
/// get the password's hash result from DAO
AccountDAO dao = new AccountDao();
String passwordHashFromDAO =
dao.getPasswordHash(id);
Validation depends on Class Hash
/// get the id's hash result from Hash
Hash hash = new Hash();
String hashResult = hash.getHashResult(password);
/// check the result
return passwordHashFromDAO.equals(hashResult);
4. Problem ? (2/3)
It violates the OCP (Open Closed Principle)
If we want to mock AccountDAO & Hash,
we must modify the code in class of Validation.
/// pseudo code for CheckAuthentication
/// input: string id
/// input: string password
/// output: true --> if pass the checking
/// false --> else fail the checking
/// get the password's hash result from DAO
AccountDAO dao = new AccountDao();
String passwordHashFromDAO =
dao.getPasswordHash(id);
/// get the id's hash result from Hash
Hash hash = new Hash();
String hashResult = hash.getHashResult(password);
/// check the result
return passwordHashFromDAO.equals(hashResult);
5. Problem ? (3/3)
Now, you can upgrade your AccountDAO &
Hash without modifying the class of Validation.
(If you just want to test, mock it.)
這個把初始化動作,由原本目標物件內,轉移到目標物件之外, /// pseudo code for CheckAuthentication
稱作「控制反轉
控制反轉」,也就是IoC。
控制反轉 /// input: string id
/// input: string password
/// output: true --> if pass the checking
/// false --> else fail the checking
/// get the password's hash result from DAO
這個把依賴的物件,透過目標物件公開建構式,交給外 AccountDAO dao = new AccountDao();
部來決定,稱作「依賴注入
依賴注入」,也就是DI。
依賴注入 String passwordHashFromDAO =
mDao.getPasswordHash(id);
/// get the id's hash result from Hash
Hash hash = new Hash();
String hashResult = mHash.getHashResult(password);
/// check the result
return passwordHashFromDAO.equals(hashResult);
6. Definition for IoC & DI
• Inversion of Control [IoC] (from wiki)
o An object-oriented programming practice where the
object coupling is bound at run time by an assembler
object and it typically not known at compile time
using static analysis.
o Usually used in Unit Test to do the isolation test.
• Dependency Injection [DI] (from wiki)
o A software design pattern that allows a choice of
component to be made at run-time rather than
compile time.
7. Method to make your SW testable (1)
• Constructor
• Public Setter
• Pass Arguments
• Inheritance
8. Method to make your SW testable (2)
• Constructor [used in the previous slides]
• Public Setter
• Pass Arguments
• Inheritance
9. Method to make your SW testable (3)
• Constructor
• Public Setter
• Pass Arguments
• Inheritance
10. Method to make your SW testable (4)
Set the dependency object via public setter
/// pseudo code for CheckAuthentication
/// input: string id
/// input: string password
/// output: true --> if pass the checking
/// false --> else fail the checking
/// get the password's hash result from DAO
AccountDAO dao = new AccountDao();
String passwordHashFromDAO =
mDao.getPasswordHash(id);
/// get the id's hash result from Hash
Hash hash = new Hash();
String hashResult = mHash.getHashResult(password);
/// check the result
return passwordHashFromDAO.equals(hashResult);
11. Method to make your SW testable (5)
• Constructor
• Public Setter
• Pass Arguments
• Inheritance
12. Method to make your SW testable (6)
/// pseudo code for CheckAuthentication
/// input: string id
/// input: string password
/// input: Hash hash
/// input: AccountDAO dao
/// output: true --> if pass the checking
/// false --> else fail the checking
/// get the password's hash result from DAO
AccountDAO dao = new AccountDao();
String passwordHashFromDAO =
dao.getPasswordHash(id);
/// get the id's hash result from Hash
Hash hash = new Hash();
String hashResult = hash.getHashResult(password);
/// check the result
return passwordHashFromDAO.equals(hashResult);
13. Method to make your SW testable (7)
• Constructor
• Public Setter
• Pass Arguments
• Inheritance
14. Method to make your SW testable (8)
Use the subclass to do the UT.
Override the getter to mock objects.
/// pseudo code for CheckAuthentication
/// input: string id
/// input: string password
/// output: true --> if pass the checking
/// false --> else fail the checking
/// get the password's hash result from DAO
AccountDAO dao = getAccountDAO();
String passwordHashFromDAO =
dao.getPasswordHash(id);
/// get the id's hash result from Hash
Hash hash = getHash();
String hashResult = hash.getHashResult(password);
/// check the result
return passwordHashFromDAO.equals(hashResult);