android: Replace PowerMock with mechanism provided by newer Mockito versions

PowerMock isn't maintained anymore and causes issues with newer Java
versions.  We only used it to mock static methods, which Mockito now
supports as well.  Instead of using the try-with-resources construct,
this uses a @Before and @After method so we don't have to change all the
test methods.
This commit is contained in:
Tobias Brunner
2024-01-16 11:00:29 +01:00
parent 1cab544c75
commit 980491ebcd
3 changed files with 25 additions and 20 deletions
+1 -4
View File
@@ -49,8 +49,5 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.10.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.11.2'
testImplementation 'org.powermock:powermock-core:2.0.9'
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.9'
testImplementation 'org.mockito:mockito-core:5.8.0'
}
@@ -16,13 +16,11 @@
package org.strongswan.android.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.strongswan.android.utils.IPRange;
import org.strongswan.android.utils.IPRangeSet;
import org.strongswan.android.utils.Utils;
@@ -36,15 +34,21 @@ import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Utils.class, IPRangeSet.class })
public class IPRangeSetTest
{
MockedStatic<Utils> mockedUtils;
@Before
public void initUtils() throws UnknownHostException
{
PowerMockito.mockStatic(Utils.class);
Mockito.when(Utils.parseInetAddress(anyString())).thenAnswer(invocation -> InetAddress.getByName(invocation.getArgument(0)));
mockedUtils = Mockito.mockStatic(Utils.class);
mockedUtils.when(() -> Utils.parseInetAddress(anyString())).thenAnswer(invocation -> InetAddress.getByName(invocation.getArgument(0)));
}
@After
public void deinitUtils()
{
mockedUtils.close();
}
private void assertSubnets(IPRangeSet set, IPRange...exp)
@@ -16,13 +16,11 @@
package org.strongswan.android.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.strongswan.android.utils.IPRange;
import org.strongswan.android.utils.Utils;
@@ -33,15 +31,21 @@ import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Utils.class, IPRange.class })
public class IPRangeTest
{
MockedStatic<Utils> mockedUtils;
@Before
public void initUtils() throws UnknownHostException
{
PowerMockito.mockStatic(Utils.class);
Mockito.when(Utils.parseInetAddress(anyString())).thenAnswer(invocation -> InetAddress.getByName(invocation.getArgument(0)));
mockedUtils = Mockito.mockStatic(Utils.class);
mockedUtils.when(() -> Utils.parseInetAddress(anyString())).thenAnswer(invocation -> InetAddress.getByName(invocation.getArgument(0)));
}
@After
public void deinitUtils()
{
mockedUtils.close();
}
@Test