JAVA-13625 Updated/upgraded Mockito Annotations Code

This commit is contained in:
Dhawal Kapil 2022-07-26 12:41:56 +05:30
parent 8999e72cb4
commit 80a4ca1c2b
5 changed files with 26 additions and 74 deletions

View File

@ -29,35 +29,16 @@
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</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 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- 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>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
@ -77,12 +58,10 @@
</build>
<properties>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-framework.version>5.0.8.RELEASE</spring-framework.version>
<spring-data.version>2.0.9.RELEASE</spring-data.version>
<spring-boot.version>2.6.4</spring-boot.version>
<spring-framework.version>5.3.20</spring-framework.version>
<!-- testing -->
<powermock.version>2.0.2</powermock.version>
<javax.persistence.version>2.1.1</javax.persistence.version>
<mockito.version>4.6.1</mockito.version>
</properties>
</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;
import org.junit.Before;
import org.junit.Test;
import org.mockito.*;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
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 {
@Mock
@ -19,10 +25,13 @@ public class MockitoAnnotationUnitTest {
@Spy
private List<String> spiedList = new ArrayList<>();
// Use either @RunWith(MockitoJUnitRunner.class) or manually openMocks in the @Before method
/*
@Before
public void init() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
}
*/
// tests
@ -91,7 +100,7 @@ public class MockitoAnnotationUnitTest {
ArgumentCaptor<String> argCaptor;
@Test
public void whenUseCaptorAnnotation_thenTheSam() {
public void whenUseCaptorAnnotation_thenTheSame() {
mockedList.add("one");
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.mockito.Mockito.when;
public class MockitoInitWithMockitoJUnitRuleUnitTest {
public class MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest {
@Rule
public MockitoRule initRule = MockitoJUnit.rule();

View File

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