Skip to content

Commit 688b17f

Browse files
Update functions/spanner to use plain Mockito rather than PowerMockito. (GoogleCloudPlatform#2850)
* Update functions/spanner to use plain Mockito rather than PowerMockito. * Static-import Mockito.mock too. * Update CODEOWNERS (GoogleCloudPlatform#2849) Adding datastore (sorry) * Update functions/spanner to use plain Mockito rather than PowerMockito. * Static-import Mockito.mock too. Co-authored-by: Cameron Zahedi <czahedi@google.com>
1 parent 88605d2 commit 688b17f

File tree

2 files changed

+20
-39
lines changed

2 files changed

+20
-39
lines changed

functions/spanner/pom.xml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</parent>
3232

3333
<properties>
34-
<powermock.version>2.0.7</powermock.version>
34+
<mockito.version>3.3.3</mockito.version>
3535
<maven.compiler.target>11</maven.compiler.target>
3636
<maven.compiler.source>11</maven.compiler.source>
3737
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -65,21 +65,9 @@
6565

6666
<!-- The following dependencies are only required for testing -->
6767
<dependency>
68-
<groupId>org.powermock</groupId>
69-
<artifactId>powermock-core</artifactId>
70-
<version>${powermock.version}</version>
71-
<scope>test</scope>
72-
</dependency>
73-
<dependency>
74-
<groupId>org.powermock</groupId>
75-
<artifactId>powermock-module-junit4</artifactId>
76-
<version>${powermock.version}</version>
77-
<scope>test</scope>
78-
</dependency>
79-
<dependency>
80-
<groupId>org.powermock</groupId>
81-
<artifactId>powermock-api-mockito2</artifactId>
82-
<version>${powermock.version}</version>
68+
<groupId>org.mockito</groupId>
69+
<artifactId>mockito-core</artifactId>
70+
<version>3.3.3</version>
8371
<scope>test</scope>
8472
</dependency>
8573
<dependency>

functions/spanner/src/test/java/functions/HelloSpannerTest.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package functions;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.when;
2022

2123
import com.google.cloud.functions.HttpRequest;
2224
import com.google.cloud.functions.HttpResponse;
@@ -38,8 +40,7 @@
3840
import org.junit.runner.RunWith;
3941
import org.junit.runners.JUnit4;
4042
import org.mockito.Mock;
41-
import org.mockito.Mockito;
42-
import org.powermock.api.mockito.PowerMockito;
43+
import org.mockito.MockitoAnnotations;
4344

4445
@RunWith(JUnit4.class)
4546
public class HelloSpannerTest {
@@ -69,31 +70,25 @@ public static void restoreLogging() {
6970

7071
@Before
7172
public void beforeTest() throws IOException {
72-
Mockito.mockitoSession().initMocks(this);
73-
74-
request = PowerMockito.mock(HttpRequest.class);
75-
response = PowerMockito.mock(HttpResponse.class);
76-
client = PowerMockito.mock(DatabaseClient.class);
73+
MockitoAnnotations.initMocks(this);
7774

7875
responseOut = new StringWriter();
7976
writerOut = new BufferedWriter(responseOut);
80-
PowerMockito.when(response.getWriter()).thenReturn(writerOut);
77+
when(response.getWriter()).thenReturn(writerOut);
8178

8279
logHandler.clear();
8380
}
8481

8582
private void setupSuccessfulMockQuery() {
86-
ReadContext readContext = PowerMockito.mock(ReadContext.class);
87-
ResultSet resultSet = PowerMockito.mock(ResultSet.class);
88-
PowerMockito.when(resultSet.next()).thenReturn(true, true, false);
89-
PowerMockito.when(resultSet.getLong("SingerId")).thenReturn(1L, 2L, 0L);
90-
PowerMockito.when(resultSet.getLong("AlbumId")).thenReturn(1L, 1L, 0L);
91-
PowerMockito.when(resultSet.getString("AlbumTitle")).thenReturn("Album 1", "Album 2", null);
92-
PowerMockito.when(
93-
readContext.executeQuery(
94-
Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums")))
83+
ReadContext readContext = mock(ReadContext.class);
84+
ResultSet resultSet = mock(ResultSet.class);
85+
when(resultSet.next()).thenReturn(true, true, false);
86+
when(resultSet.getLong("SingerId")).thenReturn(1L, 2L, 0L);
87+
when(resultSet.getLong("AlbumId")).thenReturn(1L, 1L, 0L);
88+
when(resultSet.getString("AlbumTitle")).thenReturn("Album 1", "Album 2", null);
89+
when(readContext.executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums")))
9590
.thenReturn(resultSet);
96-
PowerMockito.when(client.singleUse()).thenReturn(readContext);
91+
when(client.singleUse()).thenReturn(readContext);
9792
}
9893

9994
@Test
@@ -110,14 +105,12 @@ DatabaseClient getClient() {
110105
}
111106

112107
private void setupFailedMockQuery() {
113-
ReadContext readContext = PowerMockito.mock(ReadContext.class);
114-
PowerMockito.when(
115-
readContext.executeQuery(
116-
Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums")))
108+
ReadContext readContext = mock(ReadContext.class);
109+
when(readContext.executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums")))
117110
.thenThrow(
118111
SpannerExceptionFactory.newSpannerException(
119112
ErrorCode.NOT_FOUND, "Table `Albums` not found"));
120-
PowerMockito.when(client.singleUse()).thenReturn(readContext);
113+
when(client.singleUse()).thenReturn(readContext);
121114
}
122115

123116
@Test

0 commit comments

Comments
 (0)