Is there an annotation or some other convenient way to ignore junit tests for specific Android SDK versions? Is there something similar to the Lint annotation TargetApi(x)? Or do I manually have to check whether to run the test using the Build.VERSION?
I don't think there is something ready but it pretty easy to create a custom annotation for this.
Create your custom annotation
#Target( ElementType.METHOD )
#Retention( RetentionPolicy.RUNTIME)
public #interface TargetApi {
int value();
}
Ovverride the test runner (that will check the value and eventually ignore/fire the test)
public class ConditionalTestRunner extends BlockJUnit4ClassRunner {
public ConditionalTestRunner(Class klass) throws InitializationError {
super(klass);
}
#Override
public void runChild(FrameworkMethod method, RunNotifier notifier) {
TargetApi condition = method.getAnnotation(TargetApi.class);
if(condition.value() > 10) {
notifier.fireTestIgnored(describeChild(method));
} else {
super.runChild(method, notifier);
}
}
}
and mark your tests
#RunWith(ConditionalTestRunner.class)
public class TestClass {
#Test
#TargetApi(6)
public void testMethodThatRunsConditionally() {
System.out.print("Test me!");
}
}
Just tested, it works for me. :)
Credits to: Conditionally ignoring JUnit tests
An alternative is to use JUnit's assume functionality:
#Test
fun shouldWorkOnNewerDevices() {
assumeTrue(
"Can only run on API Level 23 or newer because of reasons",
Build.VERSION.SDK_INT >= 23
)
}
If applied, this effectively marks the test method as skipped.
This is not so nice like the annotation solution, but you also don't need a custom JUnit test runner.
I've been searching for an answer to this question, and haven't found a better way than to check the version. I was able to conditionally suppress the execution of test logic by putting a check in the following Android TestCase methods. However, this doesn't actually prevent the individual tests from executing. Overriding the runTest() method like this will cause tests to "pass" on API levels you know will not work. Depending on your test logic, you may want to override tearDown() too. Maybe someone will offer a better solution.
#Override
protected void setUp() throws Exception {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
if (Log.isLoggable(TAG, Log.INFO)) {
Log.i(TAG, "This feature is only supported on Android 2.3 and above");
}
} else {
super.setUp();
}
}
#Override
protected void runTest() throws Throwable {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
assertTrue(true);
} else {
super.runTest();
}
}
I think #mark.w's answer is the path of least resistance:
You might wanna take a look at SdkSuppress annotation. It has two methods -- maxSdkVersion and minSdkVersion which you could use depending on your need.
for example:
#Test
#SdkSuppress(minSdkVersion = Build.VERSION_CODES.KITKAT)
public void testMethodThatRunsConditionally() {
System.out.print("Test me!");
}
I have upgraded answer of #Enrichman for Kotlin modern version. So, this is test runner class:
class ConditionalSDKTestRunner(klass: Class<*>?) : BlockJUnit4ClassRunner(klass) {
override fun runChild(method: FrameworkMethod, notifier: RunNotifier) {
// Annotation class could not have a base class of interface, so we can not group them.
val testOnlyForTargetSDKCode = method.getAnnotation(TestOnlyForTargetSDK::class.java)?.sdkLevel?.code
val testForTargetSDKAndBelowCode = method.getAnnotation(TestForTargetSDKAndBelow::class.java)?.sdkLevel?.code
val testForTargetSDKAndAboveCode = method.getAnnotation(TestForTargetSDKAndAbove::class.java)?.sdkLevel?.code
when {
// If annotation exists, but target SDK is not equal of emulator SDK -> skip this test.
testOnlyForTargetSDKCode != null && testOnlyForTargetSDKCode != Build.VERSION.SDK_INT ->
notifier.fireTestIgnored(describeChild(method))
// If annotation exists, but test SDK is lower than emulator SDK -> skip this test.
testForTargetSDKAndBelowCode != null && testForTargetSDKAndBelowCode < Build.VERSION.SDK_INT ->
notifier.fireTestIgnored(describeChild(method))
// If annotation exists, but test SDK is higher than emulator SDK -> skip this test.
testForTargetSDKAndAboveCode != null && testForTargetSDKAndAboveCode > Build.VERSION.SDK_INT ->
notifier.fireTestIgnored(describeChild(method))
// For other cases test should be started.
else -> super.runChild(method, notifier)
}
}
}
Enum with exist SDKs in your project:
enum class SdkLevel(val code: Int) {
SDK_24(24),
SDK_25(25),
SDK_26(26),
SDK_27(27),
SDK_28(28),
SDK_29(29),
SDK_30(30),
SDK_31(31),
SDK_32(32)
}
and annotations below:
#Target(AnnotationTarget.FUNCTION)
#Retention(AnnotationRetention.RUNTIME)
annotation class TestForTargetSDKAndAbove(val sdkLevel: SdkLevel)
#Target(AnnotationTarget.FUNCTION)
#Retention(AnnotationRetention.RUNTIME)
annotation class TestForTargetSDKAndBelow(val sdkLevel: SdkLevel)
#Target(AnnotationTarget.FUNCTION)
#Retention(AnnotationRetention.RUNTIME)
annotation class TestOnlyForTargetSDK(val sdkLevel: SdkLevel)
To use it just add ConditionalSDKTestRunner to your base android unit test class:
#RunWith(ConditionalSDKTestRunner::class)
abstract class BaseAndroidUnitTest
and necessary annotation for test to make it actual only for special sdk:
#Test
#TestForTargetSDKAndAbove(SdkLevel.SDK_31)
fun getConnectionInfo_positive_SDK31() {
#Test
#TestForTargetSDKAndBelow(SdkLevel.SDK_30)
fun getConnectionInfo_negative1_SDK30() {
#Test
#TestOnlyForTargetSDK(SdkLevel.SDK_29)
fun getConnectionInfo_negative1_SDK29() {
That's all. Thanks!
Related
I'm running UI testing on Android devices using Appium. We recently migrated to JUnit5 and I'm attempting to utilize the #BeforeAll class to make sure the app is in a good state before we continue to the next class.
Currently, the tooltip in Android studio is indicating that the function is never used. In the log I'm seeing a junitException saying that the method must be static. I haven't implemented #TestInstance yet, I'd like to be able to use beforeAll without it for now. I'm just confused why it isn't working since my #beforeEach and #afterEach are both working. The error and code are below.
org.junit.platform.commons.JUnitException: #BeforeAll method 'public final void com.bypass.automation.BaseTest.healthcheck()' must be static unless the test class is annotated with #TestInstance(Lifecycle.PER_CLASS).
open class BaseTest {
lateinit var driver: AndroidDriver<MobileElement>
private val capabilities = DesiredCapabilities().apply {
setCapability(APPIUM_VERSION, "1.19.1")
setCapability(PLATFORM_NAME, "Android")
setCapability(DEVICE_NAME, "Android")
setCapability("appPackage", "com.ourpackage")
setCapability("appActivity", "com.ourpackage.PassthroughHomeActivity")
setCapability("automationName", "uiautomator2")
setCapability("skipDeviceInitialization", true)
setCapability("noReset", true)
setCapability("full-reset", false)
setCapability("enableMultiWindows", false)
setCapability("unlockType", "pin")
setCapability("unlockKey", "0000")
setCapability("newCommandTimeout", "120")
}
#BeforeAll
fun healthcheck() {
val currentActivity = driver.currentActivity()
println("Current activity is $currentActivity")
if (currentActivity.contains("StationSecurePayActivity")) {
println("Exiting Station Pay")
CreditCardEntryView(driver).clickBackButton()
}
when {
currentActivity.contains("kiosk") -> {
Thread.sleep(2000)
println("Exiting Kiosk")
KioskView(driver).exitKiosk()
println("Logging out")
LogInProviderUtil(driver).logOut()
}
currentActivity != ".LoginActivity" -> {
println("Logging out")
LogInProviderUtil(driver).logOut()
}
currentActivity.contains(".LoginActivity") -> {
println("Session was properly logged out. No action taken.")
}
}
}
#BeforeEach
fun setup() {
driver = AndroidDriver(URL("http://127.0.0.1:4750/wd/hub"), capabilities)
driver.manage()?.timeouts()?.implicitlyWait(30, SECONDS)
if (LogInProviderUtil(driver).isLoggedIn()){
LogInProviderUtil(driver).logOut()
}
}
#AfterEach
fun teardown() {
if (LogInProviderUtil(driver).isLoggedIn()){
LogInProviderUtil(driver).logOut()
driver.quit()
}
else {
driver.quit()
}
}
}
It will work. I believe that any method annotated with #BeforeAll must be static (unless the "per-class" test instance lifecycle is used). So it sounds to me like you should switch to that by adding this annotation to your test class: #TestInstance(Lifecycle.PER_CLASS)
Also, it is usual practice to make your setup and teardown methods public. Also, I recommend use of Selenium-Jupiter framework (https://github.com/bonigarcia/selenium-jupiter/blob/master/README.md#appium) . Good luck.
If you want to have an initialization block you may put it simply into
init{} method. And you don't have to annotate it.
I'm stuck trying to mock some stuff with mockk:
I have the following setup on gradle
root:
|-- App (just a sample app for the SDK)
|-- SDK (SDK we develop) << apply plugin: 'com.android.library'
|-- SDKimpl.kt
|-- Foo (wrapper around a .jar library) << apply plugin: 'com.android.library'
|-- Foo.kt
So I'm writing an androidTest for the SDK and trying to mock Foo.kt.
There's nothing unusual about Foo class, just direct class Foo(private val someParams) {
So using androidTestImplementation "io.mockk:mockk-android:1.8.13" the mock goes:
val mock: Foo = mockk()
// val mock: Foo = mockkClass(Foo::class) // also tried this
every { mock.getData() } returns listOf("1", "2", "3")
I'm always getting the following crash:
io.mockk.MockKException: Missing calls inside every { ... } block.
at io.mockk.impl.recording.states.StubbingState.checkMissingCalls(StubbingState.kt:14)
at io.mockk.impl.recording.states.StubbingState.recordingDone(StubbingState.kt:8)
at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:42)
Also tried just to gather information:
running inside JVM test folder. It gets mocked without issues, but I can't run my test as JVM
running androidTest inside Foo module. Got the same crash
using mockkClass(Foo::class). Got some crash
using annotation #MockK and MockKAnnotations.init(this). Got some crash.
added Log.d before every { line and inside getData() method and it seems the actual real method from the class is getting called during the mock setup. That seems super weird to me.
Any idea what's going wrong here?
edit:
as requested, full code. I'm current working on an isolated project to try to isolate the error, so Foo is just:
class Foo {
fun getData(): String {
Log.d(TAG, "invoked foo.getData()")
return "trolololo"
}
}
and then I have FooTest in androidTest:
class FooTest {
#Test
fun mock_foo() {
val foo = mockk<Foo>()
every { foo.getData() } returns "zero"
assertEquals("zero", foo.getData())
}
}
It seems to be a Mockk opened issue: https://github.com/mockk/mockk/issues/182
2 possible quick fixes (pick one):
Run the Instrumented Tests in an emulator >= Android-P
Set Foo class as open (and the method(s) you want to mock too)
Try to check the official guide and see what is missing.
In my case, I tried to mock an extension in Kotlin but missed the mockkStatic
fun Date.asMyTime() : DateTime = DateTime(this, DateTimeZone.getDefault())
mockkStatic("packageName.FileNameKt") // This is what I was missing
every {
DateTime().asMyTime()
} returns mock(DateTime::class.java)
In my case I forgot to spyk the class I was applying every {...} to. 😳
val presenter = spyk(MyPresenter())
every { view.myFun(any()) } returns Unit
In my case, I've missed
#Before
fun setUp() {
MockKAnnotations.init(this)
}
In my case I tried to mock using mock() function instead mockk() (double k)
Make sure the object is really a mock, not the real object.
For instance:
- Sdk sdk = Sdk()
+ Sdk sdk = mockk()
every { sdk.crypto } returns mockk()
My problem was that I used a java class without getters
public class KeyStorePasswordPair {
public KeyStore keyStore;
public String keyPassword;
public KeyStorePasswordPair(KeyStore keyStore, String keyPassword) {
this.keyStore = keyStore;
this.keyPassword = keyPassword;
}
}
I needed to add getters for the variables to make mockking work:
public class KeyStorePasswordPair {
public KeyStore getKeyStore() {
return keyStore;
}
public String getKeyPassword() {
return keyPassword;
}
private KeyStore keyStore;
private String keyPassword;
public KeyStorePasswordPair(KeyStore keyStore, String keyPassword) {
this.keyStore = keyStore;
this.keyPassword = keyPassword;
}
}
Mockk Missing calls inside every { ... } block
may also be thrown when you have an every block defined on an object that is not a mockk, for example:
You define stub behavior like this
every { foo.getData() } returns DATA
and then try to:
every { DATA.getVersion() } returns VERSION
Where MY_THING and VERSION objects are declared (and instantiated) in the test class.
The error message is not very informative and a bit misleading in that case.
try like this
`when`(mock.getData()).thenReturn(listOf("1", "2", "3"))
I came across this limitation when trying to mock a class that refers to classes present in SDK >= 26 and executed the test in a device running SDK 24.
I created a test app to better understand the problem.
open class RandomStuff{
#RequiresApi(Build.VERSION_CODES.O)
fun createDefaultNotificationChannel(): NotificationChannel {
return NotificationChannel("test", "test", NotificationManager.IMPORTANCE_DEFAULT)
}
fun testString(): String = "This is a test string"
}
Note that the RandomStuff class refers to the NotificationChannel class, only found in SDK >=26.
The test object provided via Dagger is the following:
#Module
class AppTestModule : AppModule() {
#Provides
override fun provideRandomStuff(): RandomStuff {
return mock(RandomStuff::class.java)
}
}
mock will fail with this exception:
java.lang.IllegalArgumentException: Could not create type
at provideRandomStuff(AppTestModule.kt) ultimately caused by a
ClassNotFoundException thrown by ByteBuddy (a component used by Mockito).
I was able to work around this issue by adding some checks to my provide method:
#Module
class AppTestModule : AppModule() {
#Provides
override fun provideRandomStuff(): RandomStuff? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return mock(RandomStuff::class.java)
} else {
return null
}
}
}
This temporary solution is very intrusive since now, the production code has to take into consideration that RandomStuff may be null.
I call this a limitation since Mockito has always been promoted as a unit testing framework it does not seem that the Dagger + Espresso + Mockito is a fully supported combination.
Is there a more creative solution for this issue?
I am writing an App for Android, and I am wanting to start writing tests for the classes I am writing. I am fairly new to writing test cases.
Right now to create a test, I use IntelliJ and use it's wizard to make a new JUnit4 test. The wizard allows me to select methods from my class to test.
But for the object I am testing, I do not want a negative number passed to the constructor.
class MinuteTime(private val minutes : Int) {
init {
if (minutes < 0) {
throw IllegalArgumentException("Cannot be less than 0.")
}
}
...
Where in my Test class is the best place to test these constraints? I know that to test the constraints, I just need to make sure the exception is thrown and caught, but I am unsure if I should make a new method for this, or just wedge it into one of the functions IntelliJ pre-made for me. Here is the generated test class:
class MinuteTimeTest {
#Test
fun getTimeInMinutes() {
}
#Test
fun getHours() {
}
#Test
fun getMinutes() {
}
#Test
fun plus() {
}
}
I'd definetly recommend to wrap this into a separate test method. Its concern is simply testing the validation during initialization:
#Test
fun negativeNumberConstructorTest() {
assertFailsWith(IllegalArgumentException::class){
MinuteTime(-1)
}
}
It’s using kotlintest on top of JUnit
I am wondering if there is anyway to stub the value of Build.Version.SDK_INT? Suppose I have the following lines in the ClassUnderTest:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//do work
}else{
//do another work
}
How can I cover all the code ?
I mean I want to run two tests with different SDK_INT to enter both blocks.
Is it possible in android local unit tests using Mockito/PowerMockito?
Thanks
Change the value using reflection.
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
And then
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), 123);
It is tested. Works.
Update:
There is a cleaner way to do it.
Create an interface
interface BuildVersionProvider {
fun currentVersion(): Int
}
Implement the interface
class BuildVersionProviderImpl : BuildVersionProvider {
override fun currentVersion() = Build.VERSION.SDK_INT
}
Inject this class as a constructor argument through the interface whenever you want current build version. Then in the tests when creating a SUT (System Under Test) object. You can implement the interface yourself. This way of doing things may be more code but follows the SOLID principles and gives you testable code without messing with reflection and system variables.
As an alternative to reflection, you can use your own class that checks for API and then use Mockito to test the API-Dependent logic in fast JVM unit tests.
Example class
import android.os.Build
class SdkChecker {
fun deviceIsOreoOrAbove(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
}
Example tested method
fun createNotificationChannel(notificationManager: NotificationManager) {
if (sdkChecker.deviceIsOreoOrAbove()) { // This sdkChecker will be mocked
// If you target Android 8.0 (API level 26) you need a channel
notificationManager.createNotificationChannel()
}
}
Example unit tests
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.verifyZeroInteractions
import com.nhaarman.mockito_kotlin.whenever
#Test
fun createNotificationChannelOnOreoOrAbove() {
whenever(mockSdkChecker.deviceIsOreoOrAbove()).thenReturn(true)
testedClass.createNotificationChannel(mockNotificationManager)
verify(mockNotificationManager).createNotificationChannel()
}
#Test
fun createNotificationChannelBelowOreo() {
whenever(mockSdkChecker.deviceIsOreoOrAbove()).thenReturn(false)
testedClass.createNotificationChannel(mockNotificationManager)
verifyZeroInteractions(mockNotificationManager)
}