The ‘Java For Testers‘ book is for people who want to learn Java. Author of the books “Selenium Simplified” and “Java For Testers”. Alan also has created online training courses to help people learn Technical Web Testing and Selenium WebDriver with Java. Java For Testers. The book covers 'just enough' to get people writing tests and abstraction layers. For example, the book cover the basics of Inheritance, but. Welcome to this sample of “Java For Testers” by Alan Richardson. This sample Java. If you want to download the full book then you can download the ebook from leanpub.
|Language:||English, Spanish, Dutch|
|Distribution:||Free* [*Registration Required]|
Java For Testers. Learn Java fundamentals fast. This version was published on The right of Alan Richardson to be identified as the author of this. Recent Posts for Java For Testers and Test Automation Blog . Updates to Java For Testers ebook and Notes on File Path construction in Java. Editorial Reviews. About the Author. Alan Richardson is also the author of “ Selenium Kindle Store · Kindle eBooks · Computers & Technology.
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.
Some people may look disparagingly on the text based on the exclusions. So consider this an opinionated introduction to Java because I know that I did not need to use many of those exclusions for the first few years of my automation programming.
I maintain that there is a core set of Java that you need in order to start writing automation code and start adding value to automation projects. I aim to cover that core in this book.
Essentially, I looked at the Java I needed when I started writing automation to support my testing, and used that as scope for this book. While knowledge of Interfaces, Inheritance, and enums, all help make my automation abstractions more readable and maintainable; I did not use those constructs with my early automation. I also want to keep the book small, and approachable, so that people actually read it and work through it, rather than downloading and leaving on their shelf because they were too intimidated to pick it up.
And that means leaving out the parts of Java, which you can pick up yourself, once you have mastered the concepts in this book. This book does not cover any Java 1. The highest version of Java required to work with this book is Java 1. The code in this book will work with Java 1.
After you complete this book, you should be able to pick up the new features in Java 1. The source code was primarily written on Windows 7 and 8, using IntelliJ 13 and But has also been run on Mac using IntelliJ You can download the source code for this book from github.
The source contains the examples and answers to exercises. I suggest you work through the book and give it your best shot before consulting the source code. The source code has been organized into two high level source folders: The full significance of these will be explained in later chapters.
But for now, the test folder contains all the JUnit tests that you see in this book. Each chapter has a package and beneath that an exercises and an examples folder:. This should make it easier for you to navigate the code base. And if you experience difficulties typing in any of the code then you can compare it with the actual code to support the book.
To allow you to read the book without needing to have the source code open, I have added a lot of code in the body of the book and you can find much of the code for the exercises in the appendix. Alan Richardson has worked as a Software professional since although it feels longer. You can find details of his other books, training courses, conference papers and slides, and videos, on his main company web site:.
My thanks go to everyone who bought the book in its early stages, this provided the continued motivation to create something that added value, and then spend the extra time needed to add polish and readability.
Special thanks go to the following people who provided early and helpful feedback during the writing process: I am also grateful to every Java developer that I have worked with who took the time to explain their code.
You helped me observe what a good developer does and how they work. All mistakes in this book are my fault. If you find any, please let me know via compendiumDev. In this first chapter I will show you Java code, and the language I use to describe it, with little explanation. I do this to provide you with some context. I want to wrap you in the language typically used to describe Java code.
And I want to show you small sections of code in context.
Just read the pages which follow, look at the code, soak it in, accept that it works, and is consistent. All the code that we write to do stuff, we write inside a class.
I have named this class AnEmptyClass. Just like your name, class names start with an uppercase letter in Java. The first line is the package that I added the class to. A package is like a directory on the file system, this allows us to find, and use, the class in the rest of our code. We have to add methods to the class before we can do anything.
Methods are the commands we can call, to make something happen. When we start learning Java we will call the methods of our classes from within JUnit tests. For the code in this book we will use JUnit. JUnit is a commonly used library which makes it easy for us to write and run Java code with assertions.
A JUnit test is simply a method in a class which is annotated with Test i. I had to add this class to the package , and I had to import the Test annotation before I could use it, and I did that as the first few lines in the file. I have thrown you into the deep end here; presenting you with a page of possible gobbledygook. And I did that to introduce you to a the Java Programming Language quickly.
I suggest you first, read this whole chapter, and then work through the chapter from the beginning and follow the steps listed. Installing Maven adds an additional degree of complexity to the setup process, but trust me. It will make the whole process of building projects and taking your Java to the next level a lot easier. If you experience any problems that are not covered in this chapter, or on the support pages, then please let me know so I can try to help, or amend this chapter, and possibly add new resources to the support page.
Some of you may already have these tools installed with your machine. The first thing we should do is learn how to check if they are installed or not.
If you type javac -version at your command line and get an error saying that javac can not be found or something similar. Then you need to install and configure a JDK. Then you have a JDK installed. It is worth following the instructions below to check if your installed JDK is up to date, but if you have a 1. If your JDK is version 1.
If you type mvn -version at your command line, and receive an error that mvn can not be found or something similar. Then you need to install and configure Maven before you follow the text in this book. Then you have Maven installed. The Java JDK can be downloaded from oracle. If you mistakenly download from java. This should show you the version number which you downloaded and installed from oracle. Maven is a dependency management and build tool. We will use it to add JUnit to our project and write our code based on Maven folder conventions to make it easier for others to review and work with our code base.
The official Maven web site is maven. You can download Maven and find installation instructions on the official web site. I find that IntelliJ works well for beginners since it tends to pick up paths and default locations better than Eclipse. When you are comfortable with the concepts in this book, you can experiment with other IDEs e.
Eclipse or Netbeans. I suggest you stick with IntelliJ until you are more familiar with Java because then you minimize the risk of issues with the IDE confusing you into believing that you have a problem with your Java. To create your first project, use IntelliJ to do the hard work. The instructions below are for IntelliJ 14, but should be very similar for future versions of IntelliJ. Remember to check JavaForTesters. The New Project wizard should create a new folder with a structure something like the following:.
If the wizard created any. You will be starting this project from scratch. The above directory structure is a standard Maven structure. Maven expects certain files to be in certain directories to use the default Maven configuration.
Since you are just starting you can leave the directory structure as it is. Typically this is application code.
We will use this for our abstraction layer code. The above convention description may not make sense at the moment, but hopefully it will become clear as you work through the book. We basically edit the pom. At the time of writing it was version 4. The pom.
You can see I also added a build section with a maven-compiler-plugin. This was mainly to cut down on warnings in the Maven output. If you really want to make the pom. Amend your pom. I have created a few videos on the JavaForTesters.
In this chapter we will take a slightly different approach. We will advance step-by-step through the chapter and we will write a simple method which we will run as a JUnit test. If you get stuck, you can refer back to this final state and compare it with your current state to help resolve any problems. A class is the basic building block for our Java code. So we want to create a class called MyFirstTest. It starts with an uppercase letter because, by convention, Java classes start with an uppercase letter.
Get in the habit of naming your classes with the first letter in uppercase. It has the word Test at the end. This might not seem important now, but at some point we are going to want to run our code automatically as part of a build process. And we can make that easier if we add Test in the Class name, either as the start of the class name, or at the end.
By naming our classes in this way, Maven will automatically run our JUnit test classes at the appropriate part of the build process. This again is a Java convention, it is not enforced by the compiler.
But people reading your code will expect to see it written like this. The Project hierarchy is shown be default as the tree structure on the left of the screen, and you can make if visible if you close it by selecting the Project button shown vertically on the left of the IntelliJ GUI. Type in the name of the Java class that you want to create i. MyFirstTest and select [OK]. We can easily manually move our code around later.
Or have IntelliJ move it around for us using refactoring. And then name any sub structures as required. So for this class we could create a package called com.
I use this convention to help you find the example and exercise source code in the source download. If we want to, we can add the package statement into our code and have IntelliJ move the class:. The semi-colon at the end of the line is important because Java statements end with a semi-colon. IntelliJ will highlight this line with a red underscore because our class is not in a folder structure that represents that package. The package statement is a line of code which defines the package that this class belongs in.
When we want to use this class in our later code then we would import the class from this package. So if you look in explorer under your project folder you will see that the package is actually a nested set of folders. And underneath the java folder you will have a folder structure that represents the package structure. Java classes only have to be uniquely named within a package.
So I could create another class called MyFirstTest and place it into a different package in my source tree and Java would not complain. I would simply have to import the correct package structure to get the correct version of the class.
We have to declare a class before we use it. And when we do so, we are also defining the rules about how other classes can use it too.
Here the class has public scope. This means that any class, in any package, can use this class if they import it. When we create classes that will be used for JUnit tests , we need to make them public so that JUnit can use them.
All the code that we write for a class has to go between the opening and closing block that represents the class body. This method is declared as public meaning that any class that can use MyFirstTest can call the method. When we use JUnit, any method that we want to use as a JUnit test should be declared as public. The void means that the method does not return a value when it is called.
We will cover this in detail later, but as a general rule, if you are going to make a method a JUnit test , you probably want to declare it as void. Every method declaration has to define what parameters the method can be called with.
If we did have any parameters they would be declared inside these parentheses. In order to write code in a method we add it in the code block of the method body i. In this book we will learn how to use annotations. JUnit implements a few annotations that we will learn.
The first, and most fundamental, is the Test annotation. JUnit only runs the methods which are annotated with Test as JUnit tests. We can have additional methods in our classes without the annotation, and JUnit will not try and run those. When you type Test on the line before the method declaration. The IDE will highlight it as an error. We have to make sure that we look at the list of import options carefully.
Sometimes we will be offered multiple options, because there may be many classes with the same name, where the difference is the package they have been placed into. Variables are a symbol which represent some other value. In programming, we use them to store values: Why 'for testers'?
Java Developers coding production applications in Java need to learn Java differently from other people on the team. Throughout the author's career, he has have written thousands of lines of Java code, but has rarely had to compile the code into an application.
Yet, when we learn Java from most books, one of the first things we learn is 'javac' and the 'main' method and working from the command line. And this is confusing. Most of the code the author writes is wrapped up in a JUnit Test method. The author has trained many people to write automation in Java, and everytime he has taught Java to testers or other people on the team, we start with a JUnit Test method and run tests from the IDE.
Testers, and other people on the team use java differently. This book provides a different order and approach to learning Java. You can find his writing and online training courses at: CompendiumDev.