【发布时间】:2019-06-11 20:21:06
【问题描述】:
我无法测试这个 getGeneratedBitmap 函数,因为无法创建位图。
import android.graphics.Bitmap
class BitmapGenerator(query: String, private val width: Int, private val height: Int) {
private var sizeExpansion: SizeExpansion = SizeExpansion(query, width, height)
private var bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
private var expandedQuery: String
private var colors: IntArray
private var colorsLength: Int = 0
init {
colorsLength = sizeExpansion.getExpectedLength()
expandedQuery = sizeExpansion.getExpandedString()
colors = IntArray(colorsLength)
generateColorArray()
}
private fun generateColorArray(): IntArray {
for (x in 0 until colorsLength) {
colors[x] = ColorGenerator().generateColorAccToChar(expandedQuery[x])
}
return colors
}
fun getGeneratedBitmap(): Bitmap {
bitmap.setPixels(colors, 0, width, 0, 0, width, height)
return bitmap
}
}
我尝试测试的方式是:
import org.junit.Test
import org.junit.Assert.*
class BitmapGeneratorTest {
@Test
fun getGeneratedBitmap() {
assertNotEquals(BitmapGenerator("salih",25,25).getGeneratedBitmap(),null)
}
}
当我运行这个测试时,它在Bitmap.createBitmap 上抛出异常
java.lang.IllegalStateException: Bitmap.createBitmap(widt… Bitmap.Config.ARGB_8888) must not be null
【问题讨论】:
-
抛出的异常是什么?不要说它没有发布完整的堆栈跟踪就抛出了异常。
-
JVM单元测试还是androidTest?
-
它在 (/src/test/java/)
标签: android unit-testing kotlin