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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Integrate Spring With Open AI
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • 7 Awesome Libraries for Java Unit and Integration Testing

Trending

  • LLM Integration in Enterprise Applications: A Practical Guide
  • Java in a Container: Efficient Development and Deployment With Docker
  • Clean Code: Concurrency Patterns, Context Management, and Goroutine Safety, Part 5
  • [closed] DZone's 2025 Developer Community Survey
  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
109.1K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Integrate Spring With Open AI
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • 7 Awesome Libraries for Java Unit and Integration Testing

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook