Merge pull request #12532 from dkapil/task/JAVA-13625

JAVA-13625 Updated/upgraded Mockito Annotations Code
This commit is contained in:
kwoyke 2022-07-26 10:20:56 +02:00 committed by GitHub
commit c1ffc6c2c5
5 changed files with 26 additions and 74 deletions

View File

@ -29,35 +29,16 @@
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version> <version>${spring-framework.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>${javax.persistence.version}</version>
</dependency>
<!-- utils --> <!-- utils -->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId> <artifactId>mockito-junit-jupiter</artifactId>
@ -77,12 +58,10 @@
</build> </build>
<properties> <properties>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version> <spring-boot.version>2.6.4</spring-boot.version>
<spring-framework.version>5.0.8.RELEASE</spring-framework.version> <spring-framework.version>5.3.20</spring-framework.version>
<spring-data.version>2.0.9.RELEASE</spring-data.version>
<!-- testing --> <!-- testing -->
<powermock.version>2.0.2</powermock.version> <mockito.version>4.6.1</mockito.version>
<javax.persistence.version>2.1.1</javax.persistence.version>
</properties> </properties>
</project> </project>

View File

@ -1,35 +0,0 @@
package com.baeldung.mockito;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockFinals {
@Test
public void whenMockFinalClassMockWorks() {
FinalList finalList = new FinalList();
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
assertNotEquals(mock.size(), finalList.size());
}
@Test
public void whenMockFinalMethodMockWorks() {
MyList myList = new MyList();
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
assertNotEquals(mock.finalMethod(), myList.finalMethod());
}
}

View File

@ -1,16 +1,22 @@
package com.baeldung.mockito; package com.baeldung.mockito;
import org.junit.Before; import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.mockito.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
//@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class MockitoAnnotationUnitTest { public class MockitoAnnotationUnitTest {
@Mock @Mock
@ -19,11 +25,14 @@ public class MockitoAnnotationUnitTest {
@Spy @Spy
private List<String> spiedList = new ArrayList<>(); private List<String> spiedList = new ArrayList<>();
// Use either @RunWith(MockitoJUnitRunner.class) or manually openMocks in the @Before method
/*
@Before @Before
public void init() { public void init() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
*/
// tests // tests
@Test @Test
@ -91,7 +100,7 @@ public class MockitoAnnotationUnitTest {
ArgumentCaptor<String> argCaptor; ArgumentCaptor<String> argCaptor;
@Test @Test
public void whenUseCaptorAnnotation_thenTheSam() { public void whenUseCaptorAnnotation_thenTheSame() {
mockedList.add("one"); mockedList.add("one");
Mockito.verify(mockedList).add(argCaptor.capture()); Mockito.verify(mockedList).add(argCaptor.capture());

View File

@ -11,7 +11,7 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
public class MockitoInitWithMockitoJUnitRuleUnitTest { public class MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest {
@Rule @Rule
public MockitoRule initRule = MockitoJUnit.rule(); public MockitoRule initRule = MockitoJUnit.rule();

View File

@ -5,18 +5,17 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.*; import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class MockitoInjectIntoSpyUnitTest { public class MockitoAnnotationsInjectIntoSpyUnitTest {
@Before @Before
public void init() { public void init() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
spyDic = Mockito.spy(new MyDictionary(wordMap)); spyDic = Mockito.spy(new MyDictionary(wordMap));
} }