GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Testing Spring based code

Testing Spring based code

Jump to bottom
Olga Naumenko edited this page Oct 17, 2023 · 14 revisions

Testing Spring-based code

UnitTestBot Java can now generate tests for Spring-based code.

General notes

UnitTestBot Java detects if your code is based on the Spring framework and provides you with necessary options right in the dialog window of the standard UnitTestBot Java plugin.

You may choose from the three approaches to Spring test generation:

  • standard unit tests that mock the Spring-related code,
  • Spring-specific unit tests that use information about the Spring application context,
  • and integration tests that validate interactions between Spring components.

Hereinafter, by components we mean Spring components.

For classes under test, select an appropriate type of test generation. In the sections below, you can find recommendations on how to choose the generation type.

NOTE

UnitTestBot Java with Spring support uses symbolic execution to generate unit tests (both standard and Spring-specific ones), so it may be not so efficient for:

  • multithreaded programs,
  • functions with calls to external libraries,
  • processing large collections.

Testing frameworks supported:

Testing framework Generating standard and Spring-specific unit tests Generating integration tests
JUnit 4 ✔️ ✔️
JUnit 5 ✔️ ✔️
TestNG ✔️ ❌

Standard unit tests

In UnitTestBot Java, standard unit tests mock all the Spring environmental dependencies using Mockito. This test generation type does not process the Spring context of the original application.

Use cases

This approach is most useful in these cases:

  • if the unit under test performs operations with data rather than calls the external Spring components,
  • if the unit under test has no Spring specifics while being a part of a Spring project,
  • for testing the Service or DAO layer of a Spring application,
  • if you need the fastest and the easiest (“one-click”) way of generating tests.

How to generate

  1. Open your Spring project in IntelliJ IDEA.
  2. Choose the required package, class or set of classes in the Project tool window.
    Right-click and choose Generate Tests with UnitTestBot or press Alt+Shift+U.
  3. Alternatively, in the Editor, place the caret at the required class or method.
    Press Alt+Shift+U or right-click and choose Generate > Tests with UnitTestBot.
  4. In the Generate Tests with UnitTestBot window, for Spring configuration, choose No configuration.

For setting the rest of the options, refer to Fine-tune test generation.

  1. Press Generate Tests or Generate and Run.

Spring-specific unit tests

To let unit tests get use of injected dependencies, UnitTestBot Java

  • starts executing a Spring application based on a user configuration,
  • gets information about the bean implementations from bean definitions (already constructed but not initialized yet),
  • destroys these definitions and shuts down the application context gracefully.

Dependencies in Spring applications are often injected via interfaces, each having one implementation per context. UnitTestBot Java uses information about the particular implementation to generate the context-relevant test. If an interface has more than one implementation, UnitTestBot Java mocks this dependency.

To guide this type of test generation, select a configuration:

Configuration approaches Support
.xml file ✔️
Java annotation ✔️
annotation-based configuration for Spring Boot application ✔️

Use cases

This approach is most useful in these cases:

  • when you need to reduce the number of mocks in generated tests,
  • when you want to test a particular component implementation—not an interface.

NOTES

  • It may provide not the maximum line coverage as it is aimed at covering the lines relevant to the chosen configuration.
  • The generation process may exceed the requested timeout due to symbolic execution limitations.

How to generate

  1. Open your Spring project in IntelliJ IDEA.
  2. Choose the required package, class or set of classes in the Project tool window.
    Right-click and choose Generate Tests with UnitTestBot or press Alt+Shift+U.
  3. Alternatively, in the Editor, place the caret at the required class or method.
    Press Alt+Shift+U or right-click and choose Generate > Tests with UnitTestBot.
  4. In the Generate Tests with UnitTestBot window, for Spring configuration, choose the required one.

  1. For Test type, choose Unit tests.

  1. For setting the rest of the options, refer to Fine-tune test generation.

  2. Press Generate Tests or Generate and Run.

Integration tests

For integration testing, UnitTestBot Java does not approximate application's behavior—it executes real application code. It uses a fuzzing technique (not symbolic execution) to mutate the initial autowired state of the component under test.

⚠️ WARNING

We strictly recommend creating a separate testing configuration or profile. If you choose to generate integration tests, you will run your application with real Spring component interactions. Method calls will be executed with random inputs. By doing this, you may publish or corrupt user data, or inflict other harm.

NOTES

  • Integration test generation may take longer than generating unit tests.
  • If the method under test contains calls to microservices, you need to start that microservice manually.

Use cases

  • Integration tests are the most appropriate way to test controllers.
  • Integration tests are relevant for testing services and microservices.

How to generate

  1. Open your Spring project in IntelliJ IDEA.
  2. Choose the required package, class or set of classes in the Project tool window.
    Right-click and choose Generate Tests with UnitTestBot or press Alt+Shift+U.
  3. Alternatively, in the Editor, place the caret at the required class or method.
    Press Alt+Shift+U or right-click and choose Generate > Tests with UnitTestBot.
  4. In the Generate Tests with UnitTestBot window, for Spring configuration, choose the required one.
  5. For Test type, choose Integration tests.
  6. For Active profile(s), enter the profile name:

  1. For setting the rest of the options, refer to Fine-tune test generation.

  2. Press Generate Tests or Generate and Run.

What is a contextLoads test?

For the Microservice layer, UnitTestBot Java creates a contextLoads test: it checks if the Spring application context has started normally.

Failing test means that the application is not properly configured, so the other test failures are not related to regression in the tested code. If loading context fails, the test contains a commented exception type, a message, and a stack trace to help a developer investigate the problem.

User guides

IntelliJ IDEA plugin

Testing pure Java code

  • Check system requirements
  • Install or update plugin
  • Generate tests with default configuration
  • Fine-tune test generation
  • Get use of test results

Testing Spring-based code

  • General notes
  • Standard unit tests
  • Spring-specific unit tests
  • Integration tests

Troubleshooting

Command-line interface

GitHub Action

Contributor guides

(redirect to /docs in main repo)

General info

  • Contributing guide
  • Developer guide
  • Naming and labeling conventions
  • Interprocess debugging
  • Interprocess logging

Continuous integration

  • CI in UnitTestBot Java
  • Docker for UnitTestBot Java
  • SSH session with GitHub agent

Design docs

  • UnitTestBot overall architecture
  • Android Studio support
  • Assumption mechanism
  • Choosing language-specific IDE
  • Code generation and rendering
  • Fuzzing Platform (FP) Design
  • Instrumented process API: handling errors and results
  • UnitTestBot JavaScript plugin setup
  • Night statistics monitoring
  • RD for UnitTestBot
  • Sandboxing
  • UnitTestBot settings
  • Speculative field non-nullability assumptions
  • Symbolic analysis of static initializers
  • Summarization module
  • Taint analysis
  • UnitTestBot decomposition
  • UtUtils class
  • UnitTestBot Family changes

Clone this wiki locally

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.