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
Newsletter
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

  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS
  • Building a Real Pipeline With senro: Monorepos, Caching, and Triggers
  • Delta Lake UniForm to Snowflake: Cross-Platform Table Access Without a Second Copy
  • Time to First Token: How Long Does It Take for a Large Model to Start Responding?

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Sep. 06, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
109.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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