Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Testing Spring based code

Olga Naumenko edited this pageOct 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 rightin the dialog window of the standard UnitTestBot Java plugin.

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

Hereinafter, bycomponents 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 andSpring-specific ones), so it may benot so efficient for:

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

Testing frameworks supported:

Testing frameworkGenerating standard and Spring-specific unit testsGenerating 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 theService orDAO 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 chooseGenerate Tests with UnitTestBot or pressAlt+Shift+U.
  3. Alternatively, in the Editor, place the caret at the required class or method.
    PressAlt+Shift+U or right-click and chooseGenerate >Tests with UnitTestBot.
  4. In theGenerate Tests with UnitTestBot window, forSpring configuration, chooseNo configuration.

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

  1. PressGenerate Tests orGenerate and Run.

Spring-specific unit tests

To let unit testsget 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 aninterface has more than one implementation, UnitTestBot Java mocks this dependency.

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

Configuration approachesSupport
.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 chosenconfiguration.
  • 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 chooseGenerate Tests with UnitTestBot or pressAlt+Shift+U.
  3. Alternatively, in the Editor, place the caret at the required class or method.
    PressAlt+Shift+U or right-click and chooseGenerate >Tests with UnitTestBot.
  4. In theGenerate Tests with UnitTestBot window, forSpring configuration, choose the required one.

  1. ForTest type, chooseUnit tests.

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

  2. PressGenerate Tests orGenerate and Run.

Integration tests

For integration testing, UnitTestBot Java does not approximate application's behavior—it executes real applicationcode. It uses a fuzzing technique (not symbolic execution) to mutate the initial autowired state of the componentunder 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 chooseGenerate Tests with UnitTestBot or pressAlt+Shift+U.
  3. Alternatively, in the Editor, place the caret at the required class or method.
    PressAlt+Shift+U or right-click and chooseGenerate >Tests with UnitTestBot.
  4. In theGenerate Tests with UnitTestBot window, forSpring configuration, choose the required one.
  5. ForTest type, chooseIntegration tests.
  6. ForActive profile(s), enter the profile name:

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

  2. PressGenerate Tests orGenerate and Run.

What is acontextLoads test?

For theMicroservice layer, UnitTestBot Java creates acontextLoads test: it checks if the Spring applicationcontext has started normally.

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

User guides

IntelliJ IDEA plugin

Contributor guides

(redirect to/docs inmain repo)

General info

Continuous integration

Design docs

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp