From 4c7bc8d19aef1976d84e2449ef460a7bbc9d90f5 Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Tue, 5 Sep 2023 19:18:56 +0100 Subject: [PATCH] BAEL-6729 Add example code for mocking one method with multiple parameters --- .../ExampleService.java | 7 +++ .../OneMethodMultipleParamsUnitTest.java | 63 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/onemethodmultipleparams/ExampleService.java create mode 100644 testing-modules/mockito-2/src/test/java/com/baeldung/onemethodmultipleparams/OneMethodMultipleParamsUnitTest.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/onemethodmultipleparams/ExampleService.java b/testing-modules/mockito-2/src/main/java/com/baeldung/onemethodmultipleparams/ExampleService.java new file mode 100644 index 0000000000..7177013721 --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/onemethodmultipleparams/ExampleService.java @@ -0,0 +1,7 @@ +package com.baeldung.onemethodmultipleparams; + +public class ExampleService { + public int getValue(int arg) { + return 1; + } +} diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/onemethodmultipleparams/OneMethodMultipleParamsUnitTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/onemethodmultipleparams/OneMethodMultipleParamsUnitTest.java new file mode 100644 index 0000000000..0633a5a2f0 --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/onemethodmultipleparams/OneMethodMultipleParamsUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.onemethodmultipleparams; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class OneMethodMultipleParamsUnitTest { + + @Mock + ExampleService exampleService; + + @Test + public void givenAMethod_whenStubbingForMultipleArguments_thenExpectDifferentResults() { + when(exampleService.getValue(10)).thenReturn(100); + when(exampleService.getValue(20)).thenReturn(200); + when(exampleService.getValue(30)).thenReturn(300); + + assertEquals(100, exampleService.getValue(10)); + assertEquals(200, exampleService.getValue(20)); + assertEquals(300, exampleService.getValue(30)); + } + + @Test + public void givenAMethod_whenUsingThenAnswer_thenExpectDifferentReults() { + when(exampleService.getValue(anyInt())).thenAnswer(invocation -> { + int argument = (int) invocation.getArguments()[0]; + int result; + switch (argument) { + case 25: + result = 125; + break; + case 50: + result = 150; + break; + case 75: + result = 175; + break; + default: + result = 0; + } + return result; + }); + assertEquals(125, exampleService.getValue(25)); + assertEquals(150, exampleService.getValue(50)); + assertEquals(175, exampleService.getValue(75)); + } + + @Test + public void givenAMethod_whenUsingConsecutiveStubbing_thenExpectResultsInOrder() { + when(exampleService.getValue(anyInt())).thenReturn(9, 18, 27); + assertEquals(9, exampleService.getValue(1)); + assertEquals(18, exampleService.getValue(1)); + assertEquals(27, exampleService.getValue(1)); + assertEquals(27, exampleService.getValue(1)); + } + +}