1. Trang chủ
  2. » Công Nghệ Thông Tin

Java for testers learn java fundamentals fast alan richardson

291 121 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 291
Dung lượng 1,68 MB

Nội dung

Java For Testers Learn Java fundamentals fast This version was published on 2015-02-27 The right of Alan Richardson to be identified as the author of this work has been asserted by him in accordance with the Copyright, Design and Patents Act 1988 The views expressed in this book are those of the author First published in Great Britain in 2015 by: Compendium Developments http://www.compendiumdev.co.uk contact details: alan@compendiumdev.co.uk Related WebSites: Java For Testers: javaForTesters.com Author’s Software Testing Blog: eviltester.com Compendium Developments: compendiumdev.co.uk Author’s Selenium Blog: seleniumSimplified.com Every effort has been made to ensure that the information contained in this book is accurate at the time of going to press, and the publishers and author cannot accept any responsibility for any errors or omissions, however caused No responsibility for loss or damage occasioned by any person acting, or refraining from action, as a result of the material in this publication can be accepted by the editor, the publisher or the author Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Design and Patents Act 1988; this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission of the publishers, or in the case of reprographic reproduction in accordance with the terms and licenses issued by the Copyright Licensing Agency, 90 Tottenham Court Road, London, W1T 4LP Enquiries concerning reproduction outside these terms should be sent to the publishers e-book ISBN : 978-0-9567332-4-5 © 2013 - 2015 Alan Richardson, Compendium Developments Ltd As ever This book is dedicated to Billie and Keeran Table of Contents Introduction Testers use Java differently Exclusions Supporting Source Code About the Author Acknowledgments Chapter One - Basics of Java Revealed Java Example Code Chapter Two - Install the Necessary Software Introduction Do you already have JDK or Maven installed? Install The Java JDK Install Maven Install The IDE Create a Project using the IDE About your new project Add JUnit to the pom.xml file Summary Chapter Three - Writing Your First Java Code My First JUnit Test Prerequisites Create A JUnit Test Class Create a Method Make the method a JUnit test Calculate the sum Assert the value Run the @Test method Summary References and Recommended Reading Chapter Four - Work with Other Classes Use @Test methods to understand Java Warnings about Integer Summary References and Recommended Reading Chapter Five - Working with Our Own Classes Context First create an @Test method Write code that doesn’t exist New Requirements Now Refactor Summary Chapter Six - Java Classes Revisited: Constructors, Fields, Getter & Setter Methods Context Constructor Getters and Setters Summary References and Recommended Reading Chapter Seven - Basics of Java Revisited Comments Statement Packages Java Classes Importing Classes Static Imports Data Types Operators Strings Summary References and Recommended Reading Chapter Eight - Selections and Decisions Ternary Operators if statement else statement Nested if else switch statement Summary References and Recommended Reading Chapter Nine - Arrays and For Loop Iteration Arrays Exercises Summary References and Recommended Reading Chapter Ten - Introducing Collections A Simple Introduction Iterating with while and do…while Interfaces Summary References and Recommended Reading Chapter Eleven - Introducing Exceptions What is an exception? Catching Exceptions An Exception is an object Catch more than one exception JUnit and Exceptions Throwing an Exception finally Summary References and Recommended Reading Chapter Twelve - Introducing Inheritance Inheritance Inherit from Interfaces and Abstract Classes Summary References and Recommended Reading Chapter Thirteen - More About Exceptions Unchecked and Checked Exceptions Difference between Exception, Error and Throwable Create your own Exception class Summary References and Recommended Reading Chapter Fourteen - JUnit Explored @Test Before & After @Ignore JUnit Assertions Asserting with Hamcrest Matchers and assertThat fail static importing Summary References and Recommended Reading Chapter Fifteen - Strings Revisited String Summary System.out.println Special character encoding String Concatenation Converting to/from a String Constructors Comparing Strings Manipulating Strings Basic String parsing with split Manipulating strings With StringBuilder Concatenation, format, or StringBuilder Summary References and Recommended Reading Chapter Sixteen - Random Data Math.random java.util.random Seeding random numbers Using Random Numbers to generate Random Strings Discussion random data in automation Summary References and Recommended Reading Chapter Seventeen - Dates and Times currentTimeMillis and nanoTime Date SimpleDateFormat Calendar Summary References and Recommended Reading Chapter Eighteen - Properties and Property Files Properties Basics Java’s System Properties Working with Property files Summary References and Recommended Reading Chapter Nineteen - Files Example of reading and writing a file File Writing And Reading Files Additional File Methods Files Summary References and Recommended Reading Chapter Twenty - Math and BigDecimal BigDecimal Math Summary References and Recommended Reading Chapter Twenty One - Collections Revisited Set Map Implementations Summary References and Recommended Reading Chapter Twenty Two - Advancing Concepts Interfaces Abstract Classes Generics Logging Enum Regular Expressions Reflection Annotations Design Patterns Concurrency Additional File considerations Summary Chapter Twenty Three - Next Steps Recommended Reading Recommended Videos Recommended Web Sites Next Steps References Appendix - IntelliJ Hints and Tips Shortcut Keys Code Completion Navigating Source Code Running a JUnit Test Loading Project Source Help Menu Summary Appendix - Exercise Answers Chapter Three - My First JUnit Test Chapter Four - Work With Other Classes Chapter Five - Work With Our Own Classes Chapter Six - Java Classes Revisited: Constructors, Fields, Getter & Setter Methods Chapter Eight - Selections and Decisions Chapter Nine - Arrays and For Loop Iteration Chapter Ten - Introducing Collections Chapter Eleven - Introducing Exceptions Chapter Twelve - Introducing Inheritance Chapter Thirteen - More Exceptions Chapter Fourteen - JUnit Explored Chapter Fifteen - Strings Revisited Chapter Sixteen - Random Data Chapter Seventeen - Dates & Times Chapter Eighteen - Properties and Property Files Chapter Nineteen - Files Chapter Twenty - Math and BigDecimal Chapter Twenty One - Collections Revisited Introduction This is an introductory text At times it takes a tutorial approach and adopts step by step instructions to coding Some people more familiar with programming might find this slow This book is not aimed at those people This book is aimed at people who are approaching Java for the first time, specifically with a view to adding automation to their test approach I do not cover automation tools in this book I do cover the basic Java knowledge needed to write and structure code when automating I primarily wrote this book for software testers, and the approach to learning is oriented around writing automation code to support testing, rather than writing applications As such it might be useful for anyone learning Java, who wants to learn from a “test first” perspective Automation to support testing is not limited to testers anymore, so this book is suitable for anyone wanting to improve their use of Java in automation: managers, business analysts, users, and of course, testers Testers use Java differently I remember when I started learning Java from traditional books, and I remember that I was unnecessarily confused by some of the concepts that I rarely had to use e.g creating manifest files, and compiling from the command line Testers use Java differently Most Java books start with a ‘main’ class and show how to compile code and write simple applications from the command line, then build up into more Java constructs and GUI applications When I write Java, I rarely compile it to a standalone application, I spend a lot of time in the IDE, writing and running small checks and refactoring to abstraction layers By learning the basics of Java presented in this book, you will learn how to read and understand existing code bases, and write simple checks using JUnit quickly You will not learn how to build and structure an application That is useful knowledge, but it can be learned after you know how to contribute to the Java code base with JUnit tests My aim is to help you start writing automation code using Java, and have the basic knowledge you need to do that This book focuses on core Java functionality rather than a lot of additional libraries, since once you have the basics, picking up a library and learning how to use it becomes a matter of reading the documentation and sample code Exclusions I assert on the size of the set, because I know that 6 numbers are supposed to be generated I assert that each of the numbers {15, 16, 17, 18, 19, 20} has been generated Write an @Test method that shows the distributions @Test public void canGenerateRandomGaussianDistributionDouble(){ Random generate = new Random(); int standardDeviationCount1 = 0; int standardDeviationCount2 = 0; int standardDeviationCount3 = 0; int standardDeviationCount4 = 0; for(int x=0; x -1.0d && randomGaussian < 1.0d) standardDeviationCount1++; if(randomGaussian > -2.0d && randomGaussian < 2.0d) standardDeviationCount2++; if(randomGaussian > -3.0d && randomGaussian < 3.0d) standardDeviationCount3++; if(randomGaussian > -4.0d && randomGaussian < 4.0d) standardDeviationCount4++; } float sd1percentage = (standardDeviationCount1/1000f) * 100f; System.out.println("about 70% one standard deviation = " + sd1percentage); float sd2percentage = (standardDeviationCount2/1000f) * 100f; System.out.println("about 95% two standard deviation = " + sd2percentage); float sd3percentage = (standardDeviationCount3/1000f) * 100f; System.out.println("about 99% three standard deviation = " + sd3percentage); float sd4percentage = (standardDeviationCount4/1000f) * 100f; System.out.println("about 99.9% four standard deviation = " + sd4percentage); Assert.assertTrue(sd1percentage < sd2percentage); Assert.assertTrue(sd2percentage < sd3percentage); // I do not assert that sd3 and sd4 are different // because of the small % difference, they do overlap } Write an @Test method which generates 1000 ages using nextGaussian @Test public void canGenerate1000AgesUsingDeviation(){ Random generate = new Random(); Map ages = new HashMap(); for(int x=0; x= 0); assertTrue( BigDecimal.TEN.compareTo(BigDecimal.TEN) >= 0 ); assertTrue( BigDecimal.TEN.compareTo(BigDecimal.TEN)

Ngày đăng: 20/03/2018, 09:13