新品価格 |
Android+Kotlin+PowerMockitoをやってみた
の続編
PowerMockitoを使ってEnvironmentをMockしてみた
・コード
mockStaticを使うだけなので簡単
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import android.os.Environment import junit.framework.Assert import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner import org.mockito.Mockito import java.io.File @RunWith(PowerMockRunner::class) @PrepareForTest(Environment::class) class EnvironmentTest { @Before fun setup() { PowerMockito.mockStatic(Environment::class.java) } @Test fun testAAA() { PowerMockito.`when`(Environment.getExternalStorageState()).thenReturn(Environment.MEDIA_MOUNTED) Assert.assertEquals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED) val file = File("FILE") PowerMockito.`when`<File>(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)).thenReturn(file) Assert.assertEquals(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).absolutePath, file.absolutePath) } } |