Skip to content

Commit

Permalink
feat: android mocker
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Mar 18, 2021
1 parent 726ea32 commit 1ba0921
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.williamfzc.randunit.env

import com.williamfzc.randunit.models.AndroidMockModel

class AndroidNormalTestEnv @JvmOverloads constructor(envConfig: EnvConfig = EnvConfig()) : NormalTestEnv(envConfig) {
init {
mockModel = AndroidMockModel(envConfig.mockConfig)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.williamfzc.randunit.mock

import android.content.Context
import com.williamfzc.randunit.exceptions.RUMockException
import org.robolectric.RuntimeEnvironment

class AndroidMocker(cfg: MockConfig) : AbstractMocker(cfg) {
override fun mock(t: Class<*>): Any {
return when {
Context::class.java.isAssignableFrom(t) -> RuntimeEnvironment.systemContext
else -> throw RUMockException("failed to mock with robolectric: $t")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.williamfzc.randunit.models

import com.williamfzc.randunit.mock.*

class AndroidMockModel @JvmOverloads constructor(mockConfig: MockConfig = MockConfig()) : MockModel(mockConfig) {
init {
mockerList.add(AndroidMocker(mockConfig))
mockerList.add(MockitoMocker(mockConfig))
mockerList.add(EasyRandomMocker(mockConfig))
if (mockConfig.ktFirst)
mockerList.add(0, MockkMocker(mockConfig))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.williamfzc.randunit

import com.williamfzc.randunit.env.AndroidNormalTestEnv
import com.williamfzc.randunit.env.EnvConfig
import com.williamfzc.randunit.env.NormalTestEnv
import com.williamfzc.randunit.env.Statement
import com.williamfzc.randunit.env.rules.AbstractRule
import com.williamfzc.randunit.mock.MockConfig
Expand Down Expand Up @@ -61,7 +61,7 @@ class SelfSmokeWithJUnit4Test(private val statementModel: StatementModel) {
mockConfig = MockConfig(ktFirst = true),
ignoreExceptions = mutableSetOf(IllegalStateException::class.java)
)
private val testEnv = NormalTestEnv(envConfig)
private val testEnv = AndroidNormalTestEnv(envConfig)

@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.williamfzc.randunit;

import com.williamfzc.randunit.env.AbstractTestEnv;
import com.williamfzc.randunit.env.NormalTestEnv;
import com.williamfzc.randunit.env.AndroidNormalTestEnv;
import com.williamfzc.randunit.env.Statement;
import com.williamfzc.randunit.env.rules.AbstractRule;
import com.williamfzc.randunit.models.StatementModel;
Expand All @@ -21,7 +21,7 @@
@RunWith(ParameterizedRobolectricTestRunner.class)
public class SelfSmokeWithJUnit4TestInJava {
private final StatementModel sm;
private static final AbstractTestEnv testEnv = new NormalTestEnv();
private static final AbstractTestEnv testEnv = new AndroidNormalTestEnv();

static class CustomRule extends AbstractRule {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.williamfzc.randunit.models.StatementModel
// - statement model (s)
// - about how to gen runnable statement from statement model
abstract class AbstractTestEnv @JvmOverloads constructor(var envConfig: EnvConfig = EnvConfig()) {
private val mockModel = MockModel(envConfig.mockConfig)
var mockModel = MockModel(envConfig.mockConfig)
private val strategy: AbstractStrategy by lazy {
envConfig.strategy.kotlin.objectInstance ?: envConfig.strategy.newInstance()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.williamfzc.randunit.exceptions.RUTypeException
import java.lang.reflect.Method
import java.util.logging.Logger

class NormalTestEnv @JvmOverloads constructor(envConfig: EnvConfig = EnvConfig()) :
open class NormalTestEnv @JvmOverloads constructor(envConfig: EnvConfig = EnvConfig()) :
AbstractTestEnv(envConfig) {
companion object {
private val logger = Logger.getGlobal()
Expand Down
15 changes: 15 additions & 0 deletions randunit/src/main/kotlin/com/williamfzc/randunit/env/Statement.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.williamfzc.randunit.env

import com.williamfzc.randunit.models.StatementModel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.williamfzc.randunit.env.strategy

import com.williamfzc.randunit.env.Statement
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.williamfzc.randunit.env.strategy

import com.williamfzc.randunit.env.Statement
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2021 williamfzc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.williamfzc.randunit.exceptions

class RUMockException(reason: String) : RUException(reason)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class MockModel @JvmOverloads constructor(mockConfig: MockConfig = MockConf
private val logger = Logger.getGlobal()
}

private val mockerList = mutableListOf<AbstractMocker>()
val mockerList = mutableListOf<AbstractMocker>()

init {
mockerList.add(MockitoMocker(mockConfig))
Expand Down

0 comments on commit 1ba0921

Please sign in to comment.