SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
モックについて
                2013.01.15




13年1月15日火曜日
テストダブルとは

       テストに使いづらいオブジェクトの代替

          例: スタブ、モック、フェイク、スパイ


       どれもメソッドの差し替えを行う

       目的が違う


13年1月15日火曜日
モックとは


       テスト対象が適切なメソッド呼び出しを
       行っているかを検証

       相互作用の検証 (⇔ 状態の検証)



13年1月15日火曜日
例: メールシステム




13年1月15日火曜日
実装するためには…

  @Test	 public	 void	 メールを送る1()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 //	 assertThat(...)	 example.comへの送信をテストしたいが??
  }




13年1月15日火曜日
実装するためには…

  @Test	 public	 void	 メールを送る1()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 //	 assertThat(...)	 example.comへの送信をテストしたいが??
  }


                          mailServer クラス自身
                         ではテストを書けない




13年1月15日火曜日
相互作用の対象を確認する

  @Test	 public	 void	 メールを送る2()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 assertThat(mailbox.getMail("alice").get(0),	 is("hello"));
  }




13年1月15日火曜日
相互作用の対象を確認する

  @Test	 public	 void	 メールを送る2()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 assertThat(mailbox.getMail("alice").get(0),	 is("hello"));
  }
     mailbox.getMailの実装
              が必要




13年1月15日火曜日
相互作用の対象を確認する

  @Test	 public	 void	 メールを送る2()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 assertThat(mailbox.getMail("alice").get(0),	 is("hello"));
  }
     mailbox.getMailの実装                 mailbox の実装が間違
              が必要                         っていたらエラー




13年1月15日火曜日
相互作用の対象を確認する

  @Test	 public	 void	 メールを送る2()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 assertThat(mailbox.getMail("alice").get(0),	 is("hello"));
  }
     mailbox.getMailの実装                 mailbox の実装が間違
              が必要                         っていたらエラー

                   そもそも mailbox.getMail は
                        プロダクトに必要?

13年1月15日火曜日
例: メールシステム

  @Test	 public	 void	 メールを送る3()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 verify(mailbox).putMail("alice@example.com",	 "hello");
  }




13年1月15日火曜日
例: メールシステム

  @Test	 public	 void	 メールを送る3()	 {	 
  	 	 	 	 mailServer.send("hello",	 "alice@example.com");

  	 	 	 	 verify(mailbox).putMail("alice@example.com",	 "hello");
  }
     mailbox.putMailのイン
     タフェースのみが必要




13年1月15日火曜日
モックのメリット

       簡潔な記述

       テストの他クラスへの依存性の排除

          インタフェースが用意されていれば良い


       テスト用コードの排除


13年1月15日火曜日
注意点




13年1月15日火曜日
自分の物だけをモックする

       モックは外部システムのシミュレートす
       る物ではない

          テストの複雑化

          外部システムのAPIを直接触るのは避ける


       アダプターを用意する

13年1月15日火曜日
TELL, DON'T ASK


       デメテルの法則

       これも依存性の排除のため

          getXXX().getYYY().getZZZ() // ← 隠れた依存性!




13年1月15日火曜日

Más contenido relacionado

Destacado

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destacado (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Mock

  • 1. モックについて 2013.01.15 13年1月15日火曜日
  • 2. テストダブルとは テストに使いづらいオブジェクトの代替 例: スタブ、モック、フェイク、スパイ どれもメソッドの差し替えを行う 目的が違う 13年1月15日火曜日
  • 3. モックとは テスト対象が適切なメソッド呼び出しを 行っているかを検証 相互作用の検証 (⇔ 状態の検証) 13年1月15日火曜日
  • 5. 実装するためには… @Test public void メールを送る1() { mailServer.send("hello", "alice@example.com"); // assertThat(...) example.comへの送信をテストしたいが?? } 13年1月15日火曜日
  • 6. 実装するためには… @Test public void メールを送る1() { mailServer.send("hello", "alice@example.com"); // assertThat(...) example.comへの送信をテストしたいが?? } mailServer クラス自身 ではテストを書けない 13年1月15日火曜日
  • 7. 相互作用の対象を確認する @Test public void メールを送る2() { mailServer.send("hello", "alice@example.com"); assertThat(mailbox.getMail("alice").get(0), is("hello")); } 13年1月15日火曜日
  • 8. 相互作用の対象を確認する @Test public void メールを送る2() { mailServer.send("hello", "alice@example.com"); assertThat(mailbox.getMail("alice").get(0), is("hello")); } mailbox.getMailの実装 が必要 13年1月15日火曜日
  • 9. 相互作用の対象を確認する @Test public void メールを送る2() { mailServer.send("hello", "alice@example.com"); assertThat(mailbox.getMail("alice").get(0), is("hello")); } mailbox.getMailの実装 mailbox の実装が間違 が必要 っていたらエラー 13年1月15日火曜日
  • 10. 相互作用の対象を確認する @Test public void メールを送る2() { mailServer.send("hello", "alice@example.com"); assertThat(mailbox.getMail("alice").get(0), is("hello")); } mailbox.getMailの実装 mailbox の実装が間違 が必要 っていたらエラー そもそも mailbox.getMail は プロダクトに必要? 13年1月15日火曜日
  • 11. 例: メールシステム @Test public void メールを送る3() { mailServer.send("hello", "alice@example.com"); verify(mailbox).putMail("alice@example.com", "hello"); } 13年1月15日火曜日
  • 12. 例: メールシステム @Test public void メールを送る3() { mailServer.send("hello", "alice@example.com"); verify(mailbox).putMail("alice@example.com", "hello"); } mailbox.putMailのイン タフェースのみが必要 13年1月15日火曜日
  • 13. モックのメリット 簡潔な記述 テストの他クラスへの依存性の排除 インタフェースが用意されていれば良い テスト用コードの排除 13年1月15日火曜日
  • 15. 自分の物だけをモックする モックは外部システムのシミュレートす る物ではない テストの複雑化 外部システムのAPIを直接触るのは避ける アダプターを用意する 13年1月15日火曜日
  • 16. TELL, DON'T ASK デメテルの法則 これも依存性の排除のため getXXX().getYYY().getZZZ() // ← 隠れた依存性! 13年1月15日火曜日