Skip to content

Commit

Permalink
Merge pull request #198 from mori-atsushi/v1.3.0-beta01
Browse files Browse the repository at this point in the history
Prepar v1.3.0-beta01
  • Loading branch information
mori-atsushi authored Mar 21, 2023
2 parents 9ab2fcb + 0a34532 commit 80fed9b
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ subprojects {
}

tasks.dokkaHtmlMultiModule {
moduleVersion.set("1.2.0")
moduleVersion.set("1.3.0-beta01")
outputDirectory.set(rootDir.resolve("docs/static/api"))
}
2 changes: 1 addition & 1 deletion docs/docs/android/activity.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The following dependency is required to take advantage of the additional support

```kotlin
dependencies {
implementation("com.moriatsushi.koject:koject-android-activity:1.2.0")
implementation("com.moriatsushi.koject:koject-android-activity:1.3.0-beta01")
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/android/application.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To get started, add the following dependencies for Android:

```kotlin
dependencies {
implementation("com.moriatsushi.koject:koject-android-core:1.2.0")
implementation("com.moriatsushi.koject:koject-android-core:1.3.0-beta01")
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/android/fragment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The following dependency is required to take advantage of the additional support

```kotlin
dependencies {
implementation("com.moriatsushi.koject:koject-android-fragment:1.2.0")
implementation("com.moriatsushi.koject:koject-android-fragment:1.3.0-beta01")
}
```

Expand Down
162 changes: 162 additions & 0 deletions docs/docs/android/tests.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import {
KojectStart,
KojectStartTest,
} from '@site/src/components/CodeLink';


# Android tests

Here we will introduce some tips for testing Android applications.

:::info LINK

To understand the basics of testing, please refer to the [Test documentation](/docs/test) first.

:::

## Unit tests
If the subject to be tested does not require the Android Framework, you can start testing by mocking the application class using mock libraries such as [mockito](https://site.mockito.org/) or [MockK](https://mockk.io/).
When calling <KojectStartTest/>, specify the mocked application class as follows:

```kotlin
class SampleViewModelTest() {
@Before
fun start() {
Koject.startTest {
application(mock(Application::class.java))
}
}

@After
fun stop() {
Koject.stop()
}

@Test
fun test() {
val viewModel = inject<SampleViewModel>()
/* ... */
}
}
```

Create JUnit rules as needed:

```kotlin
class KojectTestRule : TestWatcher() {
override fun starting(description: Description) {
Koject.startTest {
application(mock(Application::class.java))
}
}

override fun finished(description: Description) {
Koject.stop()
}
}
```
```kotlin
class SampleViewModelTest() {
@get:Rule
val kojectTestRule = KojectTestRule()

@Test
fun test() {
val viewModel = inject<SampleViewModel>()
/* ... */
}
}
```

## Instrumented tests / Robolectric tests

If you want to run [instrumented tests](https://developer.android.com/training/testing/instrumented-tests) on a physical device or emulator, or run unit tests using [Robolectric](https://robolectric.org/), you need to replace <KojectStart/> with `Koject.startTest()`.

First, create a test application class using `Koject.startTest()`.

```kotlin
class TestApplication : Application() {
override fun onCreate() {
super.onCreate()

Koject.startTest {
application(this@TestApplication)
}
}
}
```

Next, create a custom runner to use the test application class.

```kotlin
class TestRunner : AndroidJUnitRunner() {
override fun newApplication(
classLoader: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(classLoader, TestApplication::class.java.name, context)
}
}
```

Don't forget to register the runner in the `build.gradle.kts` file.

```diff title="build.gradle.kts"
android {
defaultConfig {
/* ... */

+ testInstrumentationRunner = "com.mypackage.test.TestRunner"
}

/* ... */
}
```

After that, you can write tests as usual.

```kotlin
@RunWith(AndroidJUnit4::class)
class SampleActivityTest() {
@Test
fun test() {
val scenario = launchActivity<SampleActivit>()
}
}
```

### Configuring KojectExtras
If you are using [KojectExtras](/docs/core/extras) and need more detailed settings, consider starting Koject for each test instead of in the `Application`.

```kotlin
class TestApplication : Application() {
override fun onCreate() {
super.onCreate()

// Do not start Koject
}
}
```
```kotlin
@RunWith(AndroidJUnit4::class)
class SampleActivityTest() {
@Before
fun start() {
Koject.startTest {
application(ApplicationProvider.getApplicationContext())
addExtras(/* ... */)
}
}

@After
fun stop() {
Koject.stop()
}

@Test
fun test() {
val scenario = launchActivity<SampleActivit>()
}
}
```
12 changes: 6 additions & 6 deletions docs/docs/android/viewmodel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Add the appropriate dependencies for the injection target:
```kotlin
dependencies {
// Inject ViewModel into Activity
implementation("com.moriatsushi.koject:koject-android-activity:1.2.0")
implementation("com.moriatsushi.koject:koject-android-activity:1.3.0-beta01")
// Inject ViewModel into Fragment
implementation("com.moriatsushi.koject:koject-android-fragment:1.2.0")
implementation("com.moriatsushi.koject:koject-android-fragment:1.3.0-beta01")
// ViewModelFactory only
implementation("com.moriatsushi.koject:koject-android-viewmodel:1.2.0")
implementation("com.moriatsushi.koject:koject-android-viewmodel:1.3.0-beta01")
}
```

Expand Down Expand Up @@ -75,15 +75,15 @@ Refer to the [documentation](/docs/compose/viewmodel) to inject ViewModels in Je

:::

:::note MIGRATION (VERSION 1.2.0)
:::note MIGRATION (VERSION 1.3.0-beta01)

As of version v1.2.0, `injectViewModels()` has been renamed to `lazyViewModels()`:
As of version v1.3.0-beta01, `injectViewModels()` has been renamed to `lazyViewModels()`:

```kotlin
// Until v1.1.0
private val viewModel: TopViewModel by injectViewModels()

// Since v1.2.0
// Since v1.3.0-beta01
private val viewModel: TopViewModel by lazyViewModels()
```
:::
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/compose/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To use Koject for injection into Composable functions, you need to add the follo

```kotlin
dependencies {
implementation("com.moriatsushi.koject:koject-compose-core:1.2.0")
implementation("com.moriatsushi.koject:koject-compose-core:1.3.0-beta01")
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/compose/viewmodel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To inject ViewModels into Composable, add the following dependencies:

```kotlin
dependencies {
implementation("com.moriatsushi.koject:koject-compose-viewmodel:1.2.0")
implementation("com.moriatsushi.koject:koject-compose-viewmodel:1.3.0-beta01")
}
```

Expand Down
14 changes: 0 additions & 14 deletions docs/docs/core/basic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,6 @@ class Repository(

In this case, all three classes (`Api`, `DB`, and `Repository`) are annotated with `@Singleton`, which means that Koject will create only one instance of each and reuse them throughout the application.

It's important to note that you can't inject a normally-scoped type into a singleton-scoped type.

```kotlin
@Provides
class NormalScope

@Singleton
@Provides
class SingletonScope(
// Cannot inject NormalScope into SingletonScope!
private val normal: NormalScope
)
```

## Lazy Injection
By using <LazyInject/> instead of `inject()`, you can get `Lazy` instance and enable lazy injection.

Expand Down
46 changes: 28 additions & 18 deletions docs/docs/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
+ implementation("com.moriatsushi.koject:koject-core:1.2.0")
+ implementation("com.moriatsushi.koject:koject-core:1.3.0-beta01")
}
}
}
}

dependencies {
// Add it according to your targets.
+ val processor = "com.moriatsushi.koject:koject-processor-app:1.2.0"
+ val processor = "com.moriatsushi.koject:koject-processor-app:1.3.0-beta01"
+ add("kspAndroid", processor)
+ add("kspJvm", processor)
+ add("kspJs", processor)
Expand All @@ -55,38 +55,47 @@ plugins {
}

dependencies {
+ implementation("com.moriatsushi.koject:koject-core:1.2.0")
+ ksp("com.moriatsushi.koject:koject-processor-app1.2.0")
+ implementation("com.moriatsushi.koject:koject-core:1.3.0-beta01")
+ ksp("com.moriatsushi.koject:koject-processor-app:1.3.0-beta01")
}
```

## Library module
In the library modules, use `koject-processor-lib` instead of `koject-processor-app`.
This prevents the container from being generated in the library module.
In the library module, use `koject-processor-lib` instead of `koject-processor-app` to prevent container generation in the library module.

To enable the collection of transitive dependencies, set the `moduleName` property.

### Multiplatform

```diff title="build.gradle.kts"
dependencies {
// Add it according to your targets.
- val processor = "com.moriatsushi.koject:koject-processor-app:1.2.0"
+ val processor = "com.moriatsushi.koject:koject-processor-lib:1.2.0"
- val processor = "com.moriatsushi.koject:koject-processor-app:1.3.0-beta01"
+ val processor = "com.moriatsushi.koject:koject-processor-lib:1.3.0-beta01"
add("kspAndroid", processor)
add("kspJvm", processor)
add("kspJs", processor)
add("kspIosX64", processor)
add("kspIosArm64", processor)
}

+ ksp {
+ arg("moduleName", project.name)
+ }
```

### Single platform

```diff title="build.gradle.kts"
dependencies {
implementation("com.moriatsushi.koject:koject-core:1.2.0")
- ksp("com.moriatsushi.koject:koject-processor-app:1.2.0")
+ ksp("com.moriatsushi.koject:koject-processor-lib:1.2.0")
implementation("com.moriatsushi.koject:koject-core:1.3.0-beta01")
- ksp("com.moriatsushi.koject:koject-processor-app:1.3.0-beta01")
+ ksp("com.moriatsushi.koject:koject-processor-lib:1.3.0-beta01")
}

+ ksp {
+ arg("moduleName", project.name)
+ }
```

## Android
Expand All @@ -95,13 +104,13 @@ For Android applications, additional functionality is available by adding the fo
```kotlin
dependencies {
// Inject Application / Context (Recommended)
implementation("com.moriatsushi.koject:koject-android-core:1.2.0")
implementation("com.moriatsushi.koject:koject-android-core:1.3.0-beta01")
// Activity support
implementation("com.moriatsushi.koject:koject-android-activity:1.2.0")
implementation("com.moriatsushi.koject:koject-android-activity:1.3.0-beta01")
// Fragment support
implementation("com.moriatsushi.koject:koject-android-fragment:1.2.0")
implementation("com.moriatsushi.koject:koject-android-fragment:1.3.0-beta01")
// ViewModelFactory only
implementation("com.moriatsushi.koject:koject-android-viewmodel:1.2.0")
implementation("com.moriatsushi.koject:koject-android-viewmodel:1.3.0-beta01")
}
```

Expand All @@ -110,7 +119,7 @@ If you are using [Jetpack Compose](https://developer.android.com/jetpack/compose

```kotlin
dependencies {
implementation("com.moriatsushi.koject:koject-compose-core:1.2.0")
implementation("com.moriatsushi.koject:koject-compose-core:1.3.0-beta01")
}
```

Expand All @@ -119,7 +128,7 @@ To inject Android ViewModel into Composable, use the following package.
```kotlin
dependencies {
// Inject ViewModel into Composable (Android only)
implementation("com.moriatsushi.koject:koject-compose-viewmodel:1.2.0")
implementation("com.moriatsushi.koject:koject-compose-viewmodel:1.3.0-beta01")
}
```

Expand All @@ -128,10 +137,11 @@ Copy the following snippets if you are using [gradle verion catalog](https://doc

```xml title="libs.versions.toml"
[versions]
koject = "1.2.0"
koject = "1.3.0-beta01"

[libraries]
koject-core = { group = "com.moriatsushi.koject", name = "koject-core", version.ref = "koject" }
koject-test = { group = "com.moriatsushi.koject", name = "koject-test", version.ref = "koject" }
koject-processor-app = { group = "com.moriatsushi.koject", name = "koject-processor-app", version.ref = "koject" }
koject-processor-lib = { group = "com.moriatsushi.koject", name = "koject-processor-lib", version.ref = "koject" }
koject-android-core = { group = "com.moriatsushi.koject", name = "koject-android-core", version.ref = "koject" }
Expand Down
Loading

0 comments on commit 80fed9b

Please sign in to comment.