concept testing in category java

appears as: testing, testing
JUnit in Action

This is an excerpt from Manning's book JUnit in Action.

  • Parameterized tests, a very flexible tool for testing that consumes different data sets and dynamic tests created at runtime (annotations: @ParameterizedTest, @TestFactory)
  • Listing 11.2 A simple Gradle build.gradle file
    plugins {
        // Apply the java plugin to add support for Java
        id 'java'
     
        // Apply the application plugin to support building a CLI application
        id 'application'
    }
     
    repositories {
        // Use jcenter for resolving dependencies.
        // You can declare any Maven/Ivy/file repository here.
        jcenter()
    }
     
    dependencies {
        // Use JUnit Jupiter API for testing.
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
     
        // Use JUnit Jupiter Engine for testing.
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
    }
     
    application {
        // Define the main class for the application
        mainClassName = 'com.manning.junitbook.ch11.App'
    }
     
    test {
        // Use junit platform for unit tests
        useJUnitPlatform()
    }

    As we have discovered in the previous chapters, software testing has several purposes. Tests make us interact with the application and understand how it works. Testing helps us deliver software that meets expectations. It is a metric of the quality of the code and protects us against the possibility of introducing regressions. Consequently, effectively and systematically organizing the process of software testing is very important.

    Java Testing with Spock

    This is an excerpt from Manning's book Java Testing with Spock.

    In the first case (which is possible with only small- to middle-sized software projects), developers aren’t entirely sure what’s broken after a code change. Therefore, they manually test all parts of the application after they implement a new feature or fix an issue. This approach wastes a lot of time/money because developers suffer from the repetitive nature of testing (which is a natural candidate for automation).

    In addition, as the project grows, testing everything by hand becomes much more difficult. Either the development progress comes to a crawl, as most developers deal with testing instead of adding new features, or (the most common case) developers add features and test only parts of the application that they think might be affected. The result is that bugs enter production code and developers become firefighters; each passing day is a big crisis as the customer discovers missing functionality.

    A better approach is needed, preferably one that decouples the test code (which should be written once) from the sets of test data and expected output (which should be written for all scenarios). This kind of testing needs a framework with explicit support for parameterized tests.

    Enterprise Java Microservices

    This is an excerpt from Manning's book Enterprise Java Microservices.

    But what if, in spite of the testing performed by the Spring Boot team, there’s a problem with a starter dependency’s choice of libraries? How can you override the starter?

    Where to start! So many types and levels of testing can be implemented for anything. Complicating things further is that different people will likely have different points of view, specifically in regard to what the various types of testing should accomplish.

    Let’s get on the same page with respect to the types of testing and create a common understanding of their meaning for us all! In this chapter, you’ll focus only on the types of testing that are relevant for our purposes. There are too many types of testing to cover them all; it’d become overwhelming.

    Then you’ll use the admin service you created in chapter 2 to show the types of testing that can be performed with a microservice.

    4.1. What type of testing do you need?

    Three types of testing are covered in this chapter:

    Why did I choose these three types of testing to focus on, given that dozens of types are available? I’m not saying that these three are the only types you need to worry about, but these are certainly crucial to your goal of ensuring that a microservice is as robust as possible. Unit and integration testing are focused on ensuring that what you, as a developer of a microservice, have written meets the requirements that have been outlined for a microservice. Consumer-driven contract testing changes perspectives to look from outside a microservice, to ensure that a microservice can correctly process whatever clients are passing to you. Though it may not be part of the requirements of a microservice, it’s possible that a client expects slightly different behavior than has been developed. Figure 4.1 shows how the three types of testing fit in terms of your code.

    Figure 4.1. Types of testing

    The key point with respect to testing of any type is that you’re not writing tests for fun or for one-off execution. The purpose, and benefit, of writing any test is the ability to continually execute it against code as it changes and is modified, typically as part of a continuous integration process that regularly builds your code. And why do you want these tests running all the time on old and updated code? For the simple reason that it reduces the number of errors, or bugs, that make it into production code. As I mentioned in chapter 1, anything you can do to reduce the number of times you’re called about production bugs, the better you are for it.

    Testing Java Microservices: Using Arquillian, Hoverfly, AssertJ, JUnit, Selenium, and Mockito

    This is an excerpt from Manning's book Testing Java Microservices: Using Arquillian, Hoverfly, AssertJ, JUnit, Selenium, and Mockito.

    This book isn’t intended to be a tutorial on the varied microservice architectures that are available today; we’ll assume you have some understanding of the subject. Rather, we’re going to help you overcome the challenges involved in testing the common features that all microservice applications share. In order to do that, in this chapter we’ll establish some common ground about what a microservice is, so that you can relate to where we’re coming from when we discuss these topics in later chapters.

    Shifting toward the ever-more-popular microservice architecture means you need to adopt new strategies in development, testing, and restructuring/refactoring and move away from some of the purely monolithic-application practices.

    Microservices offer you the advantage of being able to scale individual services, and the ability to develop and maintain multiple services in parallel using several teams, but they still require a robust approach when it comes to testing.

    Figure 7.3. The UI automation during testing
  • Every environment that runs tests contains exactly the same versions of the required libraries, dependencies, and/or servers. It doesn’t matter which physical machines are run: it could be the development, testing, or preproduction machine. All of them contain the same running bits and bobs.
  • Play for Java

    This is an excerpt from Manning's book Play for Java.

    Be sure to replace the path of the publishing repo with your own. Now, if we issue the publish command in the Play console, commit, and push the changes in the Pages repo, we’ll have published our module. Note that, since we never updated the version number, we’ve published a snapshot version. This has a specific meaning in the world of Maven artifacts and no sane project will rely on snapshot versions other than for development and testing. If you’re happy with the state of your module, update the version to 1.0 or any version number you like (without the -SNAPSHOT part) and publish that. Don’t forget to increment the version number and add -SNAPSHOT back afterward, lest you release a development version with an already existing production version number.

    As we’ve seen in the previous chapters, a Play application consists of several core components—controllers, models, view templates, routes, forms, and so on. One of the nice things about Play is that they all result in runnable code. This means that for all these concepts, there’s a class or method that you can access from your Java code. It also means that you can take any separate component and test it by executing its code and checking the result. This is the core principle of automated testing—run some code, compare the result against an expected value, and return the findings. This kind of testing is called unit testing. It’s called unit testing because you take an individual unit of code and... well... test it.

    Spring Integration in Action

    This is an excerpt from Manning's book Spring Integration in Action.

    It gets even more interesting and more relevant for our lead-up to Spring Integration when we look at Spring’s support for invoking methods on any Spring-managed object. Sure, the MessageListener interface seems simple enough, but it has a few limitations. First, it requires a dependency on the JMS API. This inhibits testing and also pollutes otherwise pure business logic achieved by relying on the IoC principle. Second, and more severe, it has a void return type. That means you can’t easily send a reply message from the listener method’s implementation. Both of these limitations are eliminated if you instead reference a POJO instance that doesn’t implement MessageListener and add the method attribute to the configuration. For example, let’s assume you want to invoke the following service method:

    As you can see, the header matching methods are very convenient and drastically reduce the amount of noise in test code. The example above demonstrates several of the matching options: checking for the presence of a header key, verifying that a header value is not null, validating a header value’s type, and asserting that a header contains an expected instance. Moreover, all of the predefined header keys can be matched via explicitly named methods as shown above with the hasCorrelationId method. On the last line, you see that there’s even a method for testing that all key-value pairs in a given map are present as headers on the message against which you match. That’s far more convenient than iterating through the map directly and testing each key and value against the message headers. Not only is the test code more readable, but the error message produced by a failed assertion will provide much more detail than if you were testing individual values directly. If we change the correlation ID in the test message, for example, the resulting test failure message would contain the following:

    Griffon in Action

    This is an excerpt from Manning's book Griffon in Action.

    But there’s an advantage to using closure properties over method definitions. When it comes to testing, it’s easier to overwrite an action implemented as a closure than it is to mock out a method.

    Testing is one of those tasks that often get relegated to the end of the development cycle. Reasons vary, but typically it’s because writing testing code isn’t as fun as writing production code. The Griffon framework recognizes this and tries its best to make writing and executing test code as fun as doing the same with production code.

    Spring Roo in Action

    This is an excerpt from Manning's book Spring Roo in Action.

    Most of these platforms use runtime type checking and are based on interpreted languages. The languages lack a compilation step, and problems usually occur at runtime, putting more of a burden on testing.

    Testing is a key part of the development lifecycle, and you’re encouraged to write tests against any of your coded logic. This chapter has only scratched the surface. Let’s look at some other topics you may want to consider.

    9.5. Web testing with Selenium

    Testing shouldn’t stop at the unit test or integration test level. These tests all exercise a particular component or set of components in compositions that you define yourself. But the best way to verify that the entire stack functions properly is to use some sort of external, black box test—meaning a test external to the application itself.

    Flex on Java

    This is an excerpt from Manning's book Flex on Java.

    As you can see from figure 2.5 Maven generated the project structure and added a couple of files for testing.

    One of the main purposes of unit testing is to try to narrow your scope of testing as much as possible, which is why it’s called unit testing. To do that you can use a mock testing framework to remove any external dependencies in the class you’re testing. By injecting these mocks into your class during unit testing, you’re able to test how your class interacts with its external dependencies without relying on the existence of those external dependencies. The mock testing framework we’ll be utilizing in this chapter is called mock-as3. The mock-as3 project is hosted via Google Code at http://code.google.com/p/mock-as3/.

    JUnit in Action, Second Edition

    This is an excerpt from Manning's book JUnit in Action, Second Edition.

    When you’re first writing tests, you want them to run as quickly and easily as possible. You should be able to make testing part of the development cycle: code-run-test-code (or test-code run-test if you’re test-first inclined). There are IDEs and compilers for quickly building and running applications; JUnit lets you build and run tests.

    Listing 6.6. Providing custom stream handler classes for testing
  • Comparing stubs, mock objects, and in-container testing
  • AspectJ in Action, Second Edition

    This is an excerpt from Manning's book AspectJ in Action, Second Edition.

    You’re now forced to address the problem (which may involve a simple fix or may require design modifications) before the system can be compiled without errors again. This is a huge improvement in productivity, because fixing errors found at compile-time takes much less effort than if the same bugs are found during code review, testing, or production.

    Listing 15.6. Support class simplifying implementation and testing

    Each phase of a software project—design, implementation, testing, and maintenance—emphasizes certain activities. AOP plays a different role in each of the phases. Let’s look at some typical ways AOP helps in each situation.

    Struts 2 in Action

    This is an excerpt from Manning's book Struts 2 in Action.

    The ValidationAware interface also specifies methods for testing whether any errors exist. The workflow interceptor will use these to determine whether it should redirect the workflow back to the input page. If it finds that errors exist, it’ll look for a result with the name input. The following snippet from chapterThree.xml shows that the Register action has declared such a result:

    Test-driven development is sweeping the nation. Testing code is no longer considered something we might do at the end of the project. Your work is expected to perform correctly as it interacts with all the other objects that make up an object-oriented application. As we pointed out in chapter 1, a great thing about Struts 2 is that action classes are Plain Old Java Objects (POJOs) and can be tested independently of all the complicated plumbing that comes with web and/or application servers. Actions in Struts 1 were tangled up with the Servlet API and were difficult to test outside of the server environment. This often led to either no testing at all or complicated tests involving funky mock objects configured to “trick” your actions into thinking they were interacting with the Servlet API code they so desperately relied upon. Struts 2 avoids tangling its actions up with the Servlet API so you can easily test them. Another tenet of modern software design is to minimize dependency. Rather than have your code dependent on other code, it’s based loosely on interfaces or roles. This allows different concrete implementations to be injected into your code according to the context or environment in which it’s running. The next section reveals how this can be useful in the context of testing.