SlideShare una empresa de Scribd logo
1 de 34



•
•
•
•
•
•
•
•
•
•
•
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
@RunWith(JUnitPlatform.class)
public class TodoAppTest {
@Test
public void myTest() {
}
}
• org.junit.jupiter:junit-jupiter-api
• org.junit.jupiter:junit-jupiter-engine
• org.junit.platform:junit-platform-runner
org.junit.jupiter.api.Assertions
assertEquals(9, 4 + 5);
assertTrue(myBoolean);
assertNull(result);
assertNull(result, () -> "result should be null");
assertAll("dim",
() -> { assertEquals(200, dim.getHeight()); },
() -> { assertEquals(300, dim.getWidth()); }
);
MyException e = assertThrows(MyException.class,
() -> { doSomething(); }
);
assertEquals("oh oh!", e.getMessage());
public MyTest {
@BeforeAll
static void setupClass() { … }
@BeforeEach
void setup() { … }
@Test
void someTest() { … }
@AfterEach
void tearDown() { … }
@AfterAll
static void tearDownClass() { … }
}
org.junit.jupiter.api.Assumptions
@Test
public void windowsOnly() {
assumeTrue(System.getenv("OS").startsWith("Windows"));
// test code here
}
@Test
public void buildServerOnly() {
assumingThat("CI".equals(System.getenv("ENV")),
() -> {
// test code here
}
);
}
•
@Disabled
public class DisabledTests() {
void disabledTest() {
// test code here
}
}
@Test
@Disabled("needs a review")
void disabledTest() {
// test code here
}
• @Ignore
• @Category
@Tag("performance")
public void performanceTest() {
// test code here
}
// Gradle plugin configuration
junitPlatform {
platformVersion 1.0
engines {
include 'junit-jupiter' }
tags {
include 'performance'
// exclude 'fast', 'smoke'
}
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("performance")
public @interface PerformanceTest {
}
@DisplayName("TodoApp")
public class TodoAppTest {
@Test
@DisplayName("Should be empty when initialized")
void init() { … }
@Nested
@DisplayName("Removing todos")
class RemoveTodo {
@Test
@DisplayName("Todo list should be empty after deleting last todo")
void removeLastTodo() { … }
@Test
@DisplayName("Should throw exception when deleting unknown todo")
void removeUnknownTodo() { … }
}
}
public interface EqualsContract<T> {
T createValue();
T createEqualValue();
@Test
default void reflexive() {
T value = createValue();
assertEquals(value, value);
}
@Test
default void symmetric() {
T value = createValue();
T equalValue = createEqualValue();
assertAll(() -> { assertEquals(value, equalValue); },
() -> { assertEquals(equalValue, value); }
);
}
}
public class TodoTest implements EqualsContract<Todo> {
public Todo createValue() { … }
public Todo createEqualValue() { … }
}
@ParameterizedTest
@ValueSource(strings={"alice", "bob"})
void test(String username) {
// test code here
}
@ParameterizedTest
@CsvFileSource(resources="testdata.csv"})
void csvTest(String name, int age) {
// test code here
}






assertAll("Stefan Bley",
() -> {
assertEquals("https://www.twitter.com/sbley", onTwitter());
},
() -> {
assertEquals("https://www.github.com/sbley", onGithub());
}
);

Más contenido relacionado

Destacado

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
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Destacado (20)

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
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Modern Unit Testing With JUnit 5 - JJUG Tokyo 2017

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 8.
  • 11.
  • 12.
  • 13. import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; @RunWith(JUnitPlatform.class) public class TodoAppTest { @Test public void myTest() { } } • org.junit.jupiter:junit-jupiter-api • org.junit.jupiter:junit-jupiter-engine • org.junit.platform:junit-platform-runner
  • 14.
  • 15. org.junit.jupiter.api.Assertions assertEquals(9, 4 + 5); assertTrue(myBoolean); assertNull(result); assertNull(result, () -> "result should be null"); assertAll("dim", () -> { assertEquals(200, dim.getHeight()); }, () -> { assertEquals(300, dim.getWidth()); } ); MyException e = assertThrows(MyException.class, () -> { doSomething(); } ); assertEquals("oh oh!", e.getMessage());
  • 16.
  • 17. public MyTest { @BeforeAll static void setupClass() { … } @BeforeEach void setup() { … } @Test void someTest() { … } @AfterEach void tearDown() { … } @AfterAll static void tearDownClass() { … } }
  • 18.
  • 19. org.junit.jupiter.api.Assumptions @Test public void windowsOnly() { assumeTrue(System.getenv("OS").startsWith("Windows")); // test code here } @Test public void buildServerOnly() { assumingThat("CI".equals(System.getenv("ENV")), () -> { // test code here } ); } •
  • 20. @Disabled public class DisabledTests() { void disabledTest() { // test code here } } @Test @Disabled("needs a review") void disabledTest() { // test code here } • @Ignore
  • 21.
  • 22. • @Category @Tag("performance") public void performanceTest() { // test code here } // Gradle plugin configuration junitPlatform { platformVersion 1.0 engines { include 'junit-jupiter' } tags { include 'performance' // exclude 'fast', 'smoke' } } @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Tag("performance") public @interface PerformanceTest { }
  • 23.
  • 24. @DisplayName("TodoApp") public class TodoAppTest { @Test @DisplayName("Should be empty when initialized") void init() { … } @Nested @DisplayName("Removing todos") class RemoveTodo { @Test @DisplayName("Todo list should be empty after deleting last todo") void removeLastTodo() { … } @Test @DisplayName("Should throw exception when deleting unknown todo") void removeUnknownTodo() { … } } }
  • 25.
  • 26. public interface EqualsContract<T> { T createValue(); T createEqualValue(); @Test default void reflexive() { T value = createValue(); assertEquals(value, value); } @Test default void symmetric() { T value = createValue(); T equalValue = createEqualValue(); assertAll(() -> { assertEquals(value, equalValue); }, () -> { assertEquals(equalValue, value); } ); } } public class TodoTest implements EqualsContract<Todo> { public Todo createValue() { … } public Todo createEqualValue() { … } }
  • 27.
  • 28. @ParameterizedTest @ValueSource(strings={"alice", "bob"}) void test(String username) { // test code here } @ParameterizedTest @CsvFileSource(resources="testdata.csv"}) void csvTest(String name, int age) { // test code here }
  • 29.
  • 31.
  • 32.
  • 33.
  • 34. assertAll("Stefan Bley", () -> { assertEquals("https://www.twitter.com/sbley", onTwitter()); }, () -> { assertEquals("https://www.github.com/sbley", onGithub()); } );

Notas del editor

  1. Geringe Modularität  alles in einem JAR Begrenzte Erweiterbarkeit (Runner, Rules)
  2. Erklärung in den folgenden Folien
  3. JUnit Vintage JUnit 3 und 4 Tests mit JUnit 5 ausführen JUnit 4 Runner  @RunWith(JUnitPlatform.class)  solange IDEs noch kein Jupiter können
  4. Branch: 01-initial 01 JUnit 4 Test schreiben, ausführen 02 JUnit 5 Artefakte einbinden 03 JUnit 5 API -> ausführen -> fail 04 Mit mvn ausführen -> success 05 @RunWith(JUnitPlatform.class) -> success
  5. - In Eclipse zeigen  01-initial - Surefire-Plugin einbinden für Maven
  6. Branch 02-junit5 1. TodoApp.addTodo und .getTodoList assertEquals 1b. assertEquals mit description 2. assertEquals get(0).getTitle 3. -> assertAll getTitle und getDueDate 3b. assertAll mit description Branch 03-todoapp-add 4. removeTodo 5. removeTodo -> assertThrows 6. exception message mit expectThrows 7. assertion description als lambda Branch 04-assertions
  7. Branch 05-test-annotations beispielhaft zeigen: System.getenv(„OS“).startsWith(„Windows“)
  8. @Nested und @DisplayName Branch 08-nested
  9. Branch 09-equals-before Branch 10-equals-after
  10. Branch 05-test-annotations beispielhaft zeigen: System.getenv(„OS“).startsWith(„Windows“)
  11. Das ist die letzte Folie!