Skip to content

mertkalecik/UnitTestFramework

Repository files navigation

UnitTestFramework

Unit tests are a critical part of the software development process. They are automated tests that are designed to check the functionality of small, isolated pieces of code, known as units. The primary goal of unit testing is to ensure that each unit of code performs as expected, independently of other units. This allows developers to catch and fix issues early in the development process, reducing the likelihood of bugs and errors further down the line. Unit tests also make it easier to refactor code by providing a safety net that ensures that changes to one unit do not inadvertently affect other units. Overall, unit testing is an essential practice for any software development team looking to build reliable, maintainable code.

This UnitTestFramework using Kotlin that allows for accessing private fields and methods without using @VisibleForTesting Annotation. One of the primary advantages of this framework is that it enables developers to access private fields and methods without needing to modify the original code under the source code folder. The framework works by using reflection to access the private field/methods and modifying its accessibility, allowing the value to be retrieved or modified. While there are some potential security risks associated with accessing private fields, this framework can be a valuable tool for developers looking to streamline their development process and improve their code's functionality.

Tech Stack

  • Coroutine
  • Kotlin
  • Reflection
  • JUnit

@VisibleForTesting & Drawbacks of the usage

Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable. You can optionally specify what the visibility should have been if not for testing; this allows tools to catch unintended access from within production code.

Screenshot 2023-04-20 at 00 34 21

There are several potential drawbacks to using the @VisibleForTesting annotation in Kotlin. Here are a few:

  • Security: The @VisibleForTesting annotation allows tests to access private class members, which would normally not be used outside the class. This can create potential security vulnerabilities if a private member is accidentally used by code outside of the tests, which could affect the proper functioning of the application or even compromise its security.

  • Code complexity: The @VisibleForTesting annotation grants access to a private member that would not normally be used by application code, which can make the code harder to understand and maintain. Depending on how much the private member is used in tests, this can add an extra layer of complexity to the code.

  • Collaboration: The @VisibleForTesting annotation implies that private members of a class are accessible anywhere in the code, rather than just in test scenarios. This can make communication and collaboration between different teams or even different developers within the same team more difficult.

For these reasons, the @VisibleForTesting annotation should be used carefully, and unnecessary access permissions should be avoided in application code.

UnitTestFramework Usage

Get & Update a private field inside the System Under Test

Screenshot 2023-04-20 at 00 51 15

Screenshot 2023-04-20 at 00 49 22

Usage:

Screenshot 2023-04-20 at 00 52 11

Result:

Screenshot 2023-04-20 at 00 55 39

Invocation of a suspend function from the System Under Test

ViewModel:

Screenshot 2023-04-20 at 03 06 59

Function Invocation with Unit Test Framework:

Screenshot 2023-04-20 at 03 06 03

Usage:

Screenshot 2023-04-20 at 03 07 54

Result:

Screenshot 2023-04-20 at 03 09 14

Version History

  • v1.0.0
    • Initial Release

Add it in your root build.gradle

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Add the dependency for UnitTestFramework

dependencies {
	testImplementation 'com.github.mertkalecik:UnitTestFramework:1.0.0'
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages