DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Migrate From JUnit 4 to JUnit 5 Step by Step
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Spring Boot - Microservice- JaxRS Jersey - HATEOAS - JerseyTest - Integration
  • Integrate Spring With Open AI

Trending

  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Docker Model Runner: Streamlining AI Deployment for Developers
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Spring Boot 2 With JUnit 5 and Mockito 2 for Unit, Integration Testing

Spring Boot 2 With JUnit 5 and Mockito 2 for Unit, Integration Testing

Want to learn more about using Spring Boot 2 for integration and unit testing? Check out this post to learn more about testing with JUnit 5 and Mockito 2.

By 
Mahmoud Romeh user avatar
Mahmoud Romeh
·
Updated Sep. 06, 18 · Tutorial
Likes (20)
Comment
Save
Tweet
Share
108.6K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we will explain how to use JUnit 5 and Mockito 2 with Spring Boot 2 for unit and integration testing. Let's get started!

Image title

First, if you are interested in reading more about Junit 5 and Mockito 2, please check out the following links: JUnit 5 and Mockito 2.

Here, we will demonstrate the following :

  1. Gradle configuration for JUnit 5 with Spring Boot 2
  2. How to define and execute the unit test with JUnit 5 and Mockito 2
  3. How to define and execute Spring Boot integration testing with Junit 5

Gradle Configuration for JUnit 5 With Spring Boot 2

To add the needed JUnit 5 dependencies, here is what you will need to implement, with the entire code sample available on GitHub:

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter'
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.2.0')
    testCompile('org.junit.jupiter:junit-jupiter-params:5.2.0')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0')
    testCompile "org.mockito:mockito-core:2.+"
    testCompile('org.mockito:mockito-junit-jupiter:2.18.3')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}


How to Define and Execute the Unit Test With JUnit 5 and Mockito 2

To run the JUnit 5 test case with Mockito2, we use Jupiter extensions support, and here, we will use the Mockito extension. The purpose of the JUnit 5 extensions is to extend the behavior of test classes or methods, and these can be reused for multiple tests.

@ExtendWith(MockitoExtension.class)
@DisplayName("Spring boot 2 mockito2 Junit5 example")
public class ShowServiceTests {
    private static final String MOCK_OUTPUT = "Mocked show label";

    @Mock
    private TextService textService;

    @InjectMocks
    private ShowService showService;

    @BeforeEach
    void setMockOutput() {
         when(textService.getText()).thenReturn(MOCK_OUTPUT);
    }

    @Test
    @DisplayName("Mock the output of the text service using mockito")
    public void contextLoads() {
        assertEquals(showService.getShowLable(), MOCK_OUTPUT);
    }
}


How to Define and Execute Spring Boot Integration Test With JUnit 5

Now, if we want to call the real service and not the mocked one for the integration test with JUnit 5, we will use the Spring extension to support that. Now, your integration test is running with JUnit 5 support as well.

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class SpringBootJunit5IntegrationTest {

    @Autowired
    private ShowService showService;

    @Test
    @DisplayName("Integration test which will get the actual output of text service")
    public void contextLoads() {
        assertEquals(showService.getShowLable(), ORIGINAL_OUTPUT);
    }
}


Well, that's it! I hope this helped you get started using JUnit 5 in your Spring Boot application.

You can find the entire sample code in GitHub

Spring Framework Spring Boot JUnit Mockito Integration testing Integration unit test

Published at DZone with permission of Mahmoud Romeh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Migrate From JUnit 4 to JUnit 5 Step by Step
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Spring Boot - Microservice- JaxRS Jersey - HATEOAS - JerseyTest - Integration
  • Integrate Spring With Open AI

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!