J unit introduction

301 views
205 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
301
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

J unit introduction

  1. 1. JUnitTesting code as you write it
  2. 2. Introduction● What is JUnit● Why writing tests● When to write a test● How to write a test
  3. 3. What● open source framework for white box testing● writing test● running test○ supported by modern IDEs
  4. 4. Why● regression-testing framework○ relationship between development and testing○ run suit anytime○ i.e. modifications wont break your system withoutyour knowledge● better code○ structure, separation of concerns● faster to debug
  5. 5. When● while writing code○ TDD● red, green vs. yellow
  6. 6. How● KISS principle● write test when the knowledge is in yourmind○ "Ya, well do it when time remains"
  7. 7. ROI15-35% increase in initialdevelopment time afteradopting TDD, but a 40-90%decrease in pre-releasedefects
  8. 8. Junit● test fixture○ well known and fixed state■ database with specific, known set of data● @BeforeClass, @AfterClass● @Before, @After● @Test
  9. 9. JUnit cont.● descriptive method name● void● without parametersNo return value? How to check values?Assertions.
  10. 10. JUnit assertions● assertEquals(bool expected, bool actual)● Matchers○ assertThat("simone is hot", is("simone is hot"))○ int a = 223;...assertThat(a, is(321))○ assertThat(123, not( is(345) ) );● fail()
  11. 11. Exampleclass CounterTest {Counter counter;@BeforeClasspublic void setup() {counter = new Counter()}...
  12. 12. Example cont.public void testIncrement() {assertTrue(counter.increment() == 1);assertTrue(counter.increment() == 2);}... run code and expect red... now code it
  13. 13. Example cont.public class Counter {int count = 0;public int increment() {count++; return count;}}... run code and expect (hopefully) green
  14. 14. Example cont.class CounterTest {....public void testDecrement() {assertTrue(counter.decrement() == -1);}... run code and expect red
  15. 15. Example cont.class Counter {...public int decrement() {return --counter;}}... run code and expect (hopefully) green... but it is still red
  16. 16. SolutionUse @Before instead of @BeforeClass
  17. 17. Tips● use the same package● forget about System.out.println()● use plugins○ DBUnit○ load testers○ coverage● no javadoc, naming conventions● start small● build process includes compilation of alltests
  18. 18. Further study● UI testing● Scala● Arquillian
  19. 19. "Test user" should "be able to log in" in {go to "http://beta/Afo"title should be ("AFO")val usernameElement = webDriver.findElement(By.name("mainForm:mainNavPanelTabSet:0:loginUserName"))usernameElement.sendKeys("rpavlicek")val passwordElement = webDriver.findElement(By.name("mainForm:mainNavPanelTabSet:0:loginPassword"))// passwordpasswordElement.sendKeys("*****")webDriver.findElement(By.id("mainForm:mainNavPanelTabSet:0:loginLoginButtonText")).click()title should be ("AFO Home")}Scala Selenium smoke test
  20. 20. QuestionsWAKE UP!

×