• Complain

Viktor Farcic - Test-Driven Java Development - Second Edition

Here you can read online Viktor Farcic - Test-Driven Java Development - Second Edition full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2018, publisher: Packt Publishing, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Viktor Farcic Test-Driven Java Development - Second Edition

Test-Driven Java Development - Second Edition: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Test-Driven Java Development - Second Edition" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

This book will teach the concepts of test driven development in Java so you can build clean, maintainable and robust code

About This Book

  • Explore the most popular TDD tools and frameworks and become more proficient in building applications
  • Create applications with better code design, fewer bugs, and higher test coverage, enabling you to get them to market quickly
  • Implement test-driven programming methods into your development workflows

Who This Book Is For

If youre an experienced Java developer and want to implement more effective methods of programming systems and applications, then this book is for you.

What You Will Learn

  • Explore the tools and frameworks required for effective TDD development
  • Perform the Red-Green-Refactor process efficiently, the pillar around which all other TDD procedures are based
  • Master effective unit testing in isolation from the rest of your code
  • Design simple and easily maintainable code by implementing different techniques
  • Use mocking frameworks and techniques to easily write and quickly execute tests
  • Develop an application to implement behavior-driven development in conjunction with unit testing
  • Enable and disable features using feature toggles

In Detail

Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasizes writing a test before writing the necessary code, and then refactoring the code to optimize it.The value of performing TDD with Java, one of the longest established programming languages, is to improve the productivity of programmers and the maintainability and performance of code, and develop a deeper understanding of the language and how to employ it effectively.

Starting with the basics of TDD and understanding why its adoption is beneficial, this book will take you from the first steps of TDD with Java until you are confident enough to embrace the practice in your day-to-day routine.Youll be guided through setting up tools, frameworks, and the environment you need, and we will dive right into hands-on exercises with the goal of mastering one practice, tool, or framework at a time. Youll learn about the Red-Green-Refactor procedure, how to write unit tests, and how to use them as executable documentation.With this book, youll also discover how to design simple and easily maintainable code, work with mocks, utilize behavior-driven development, refactor old legacy code, and release a half-finished feature to production with feature toggles.You will finish this book with a deep understanding of the test-driven development methodology and the confidence to apply it to application programming with Java.

Style and approach

An easy-to-follow, hands-on guide to building applications through effective coding practices. This book covers practical examples by introducing different problems, each one designed as a learning exercise to help you understand each aspect of TDD.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Viktor Farcic: author's other books


Who wrote Test-Driven Java Development - Second Edition? Find out the surname, the name of the author of the book and a list of all author's works by series.

Test-Driven Java Development - Second Edition — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Test-Driven Java Development - Second Edition" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Injecting the BookRepository dependency

The code for the BookRepository dependency is in BooksEndpoint (fragment):

@Path("books") @Component public class BooksEndpoint { private BooksRepository books = new BooksRepository(); [...]
Final validation

Let us imagine that a different person worked on the code that should fulfill the requirements set by our scenarios. This person picked one scenario at the time, developed the code, ran that scenario, and confirmed that his implementation was correct. Once the implementation of all scenarios has been done, it is time to run the whole story and do the final validation.

For that matter, the application has been packed as a Docker file and we have prepared a virtual machine with Vagrant for executing the application.

Check out the branch at https://bitbucket.org/vfarcic/tdd-java-ch08-books-store/branch/03-validation and run Vagrant:

$ vagrant up

The output should be similar to the following:

==> default: Importing base box 'ubuntu/trusty64'...==> default: Matching MAC address for NAT networking...==> default: Checking if box 'ubuntu/trusty64' is up to date......==> default: Running provisioner: docker...default: Installing Docker (latest) onto machine...default: Configuring Docker to autostart containers...==> default: Starting Docker containers...==> default: -- Container: books-fe

Once Vagrant is finished, we can see the application by opening http://localhost:9001 in our browser of choice:

Now let us run our scenarios again gradle clean test This time there were - photo 1

Now, let us run our scenarios again:

$ gradle clean test

This time there were no failures and all scenarios ran successfully:

Once all scenarios are passing we meet the acceptance criteria and the - photo 2

Once all scenarios are passing, we meet the acceptance criteria and the application can be delivered to production.

Requirement 7 win condition (III)

This is the second win condition, which is pretty similar to the previous one.

If a player inserts a disc and connects more than three discs of his color in a straight horizontal line, then that player wins.

This time, we are trying to win the game by inserting discs into adjacent columns:

@Testpublic void when4HorizontalDiscsAreConnectedThenPlayerWins() { int column; for (column = 0; column < 3; column++) { tested.putDiscInColumn(column); // R tested.putDiscInColumn(column); // G } assertThat(tested.getWinner(), isEmptyString()); tested.putDiscInColumn(column); // R assertThat(tested.getWinner(), is("R"));}

The code to pass this test is put into the checkWinners method:

if (winner.isEmpty()) { String horizontal = Stream
.of(board[row])
.reduce(String::concat).get();
if (winPattern.matcher(horizontal).matches()) winner = colour; }
Web-testing frameworks

The application classes and data sources have been tested throughout this chapter, but there is still something missing; the most common user entry pointthe web. Most enterprise applications such as intranets or corporate sites are accessed using a browser. For this reason, testing the web provides significant value, helping us to make sure that it is doing what it is expected to do.

Furthermore, companies are investing a lot of time performing long and heavy manual tests every time the application changes. This is a big waste of time since a lot of those tests can be automatized and executed without supervision, using tools such as Selenium or Selenide.

Implementation

In order to track who should play next, we need to store who played last:

private char lastPlayer = '\0';public void play(int x, int y) { checkAxis(x); checkAxis(y); setBox(x, y); lastPlayer = nextPlayer();}public char nextPlayer() { if (lastPlayer == 'X') { return 'O'; } return 'X';}

You are probably starting to get the hang of it. Tests are small and easy to write. With enough experience, it should take a minute, if not seconds, to write a test and as much time or less to write the implementation.

Legacy kata

You have been given a task to adopt a system that is already in production, a working piece of software for a book library: the Alexandria project.

The project currently lacks documentation, and the old maintainer is no longer available for discussion. So, should you accept this mission, it is going to be entirely your responsibility, as there is no one else to rely on.

Implementation

With such a small and clearly defined specification, it should be fairly easy to write the code that implements it:

public boolean moveForward() { return location.forward(); }
AssertJ

AssertJ works in a similar way to Hamcrest. A major difference is that AssertJ assertions can be concatenated.

To work with AssertJ, the dependency must be added to Gradle's dependencies:

testCompile 'org.assertj:assertj-core:2.0.0'

Let's compare JUnit asserts with AssertJ:

Assert.assertEquals(5, friendships.getFriendsList("Joe").size()); List friendsOfJoe =
Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul");
Assert.assertTrue( friendships.getFriendsList("Joe")
.containsAll (friendsOfJoe) );

The same two asserts can be concatenated to a single one in AssertJ:

assertThat(friendships.getFriendsList("Joe")) .hasSize(5) .containsOnly("Audrey", "Peter", "Michael", "Britney", "Paul");

This was a nice improvement. There was no need to have two separate asserts, nor was there a need to create a new list with expected values. Moreover, AssertJ is more readable and easier to understand.

The complete source code can be found in the FriendshipsAssertJTest class at https://bitbucket.org/vfarcic/tdd-java-ch02-example-junit.

Now that we have the tests up and running, we might want to see what the code coverage is that is generated by our tests.

Development practices

Practices listed in this section are focused on the best way to write tests. Write the simplest code to pass the test as it ensures cleaner and clearer design and avoids unnecessary features.

The idea is that the simpler the implementation, the better and easier it is to maintain the product. The idea adheres to the keep it simple, stupid (KISS) principle. This states that most systems work best if they are kept simple rather than made complex; therefore, simplicity should be a key goal in design, and unnecessary complexity should be avoided. Write assertions first, act later as it clarifies the purpose of the requirements and tests early.

Once the assertion is written, the purpose of the test is clear and the developer can concentrate on the code that will accomplish that assertion and, later on, on the actual implementation. Minimize assertions in each test as it avoids assertion roulette; it allows the execution of more asserts.

If multiple assertions are used within one test method, it might be hard to tell which of them caused a test failure. This is especially common when tests are executed as part of the CI process. If the problem cannot be reproduced on a developer's machine (as may be the case if the problem is caused by environmental issues), fixing the problem may be difficult and time consuming.

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Test-Driven Java Development - Second Edition»

Look at similar books to Test-Driven Java Development - Second Edition. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Test-Driven Java Development - Second Edition»

Discussion, reviews of the book Test-Driven Java Development - Second Edition and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.