Tag: 测试

Junit,如何禁用每个测试方法创建Test的新实例

有没有一些方法来禁用Test per @Test的创建新实例?

在Mockito类的模拟过程中投射参数

当我用下面的方法(在Kotlin中) val card: Card = mock<Card>()模拟类时val card: Card = mock<Card>()它会自动将所有参数(cardNumber,expMonth,expYear,CVC)初始化为String,因为它们保存为String。 然而,在我要测试的代码(Java)中,我通过以下方式初始化类卡 Card card = new Card(mView.getCreditCardNumber(), Integer.valueOf(mView.getMonth()), Integer.valueOf(mView.getYear()), mView.getCVV()); 我已经用以下方式嘲笑了这些价值 whenever(mView.creditCardNumber).then { "1234567890123456" } whenever(mView.month).then { "12" } whenever(mView.year).then { "2022" } whenever(mView.cvv).then { "123" } 所以区别在于中间的2个参数应该是整数,而我不能这样做。 我尝试了以下解决方案 whenever(mView.month).then { 12 } whenever(mView.year).then { 2022 } 但是我从Int到String得到CastException。 由于mView.month /年应该是字符串。 那么,有没有办法使这个强制转换,否则我不能够正确地测试代码的其余部分。 请帮忙,否则我疯了。 谢谢 :) !

我应该如何测试Kotlin扩展功能?

有人能告诉我如何在Kotlin中单元测试扩展函数吗? 由于它们是静态解析应该作为静态方法调用还是非静态? 另外,由于语言与Java是完全可互操作的,因此Java单元如何测试Kotlin扩展函数应该如何执行?

在Before方法中,ActivityTestRule.getActivity返回null

我需要在每次测试之前清空我的用户数据 // Kotlin code fun getActivity() = activityRule.getActivity() Before fun setUp() { cleanUp(getActivity()) } 我需要获取一个上下文才能这样做,但在setUp中,activityRule.getActivity()返回null。 我也试过: Before fun setUp() { val i = Intent() i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) activityRule.lauchActivity(i) cleanUp(getActivity()) } 我有一个活动,但清理工作只有一半的时间(我相信一些竞争条件适用于此) 我想避免清理后,以便手动查看我的应用程序状态,如果需要。 有没有在Before函数中获取上下文? 谢谢

运行Spek测试显示错误“空测试套件”

与Kotlin熟悉了一下,我想介绍一下另一个Android-Java项目,作为测试的第一步。 我决定直接与Spek开始。 我添加了以下依赖关系来构建要测试的模块的渐变: testCompile 'junit:junit:4.12' testCompile "org.jetbrains.kotlin:kotlin-stdlib:1.0.2" testCompile "org.jetbrains.kotlin:kotlin-test-junit:1.0.2" testCompile "org.jetbrains.spek:spek:1.0.25" 其中我使用了SimpleTest类的git仓库的spek-samples: import org.jetbrains.spek.api.Spek import kotlin.test.assertEquals class SampleCalculator { fun sum(x: Int, y: Int) = x + y fun subtract(x: Int, y: Int) = x – y } class SimpleTest : Spek({ describe("a calculator") { val calculator = SampleCalculator() it("should return the result of adding […]

用MVP + Dagger进行Android测试2

我使用依赖注入框架Dagger 2使用模式MVP创建Android应用程序。 应用程序具有用于将请求发送到REST的自定义IntentService。 每个活动都有自己的Dagger组件,用于提供包括Presenter(表示层)和Interactor(模型层)类的所有必需对象。 Presenter包含没有Android SDK的代码 – 只有逻辑。 Interactor可以启动IntentService来接收数据,包含ApplicationContext等 为了测试我的Java / Kotlin类,我使用了JUnit和我自己的模拟类。 对于使用Android SDK的Instrumented测试,我正在使用Espresso 。 这里是简单的测试我如何做(Kotlin): @RunWith(AndroidJUnit4::class) @MediumTest class SampleBehaviorTest : UiBehaviorTestBase() { @get:Rule val rule = IntentsTestRule<SampleActivity>(SampleActivity::class.java, true, false) @Inject lateinit var someService: SomeService @Before fun setUp() { (getApp().buildSampleComponent() as MockSimpleComponent).inject(this) val intent = Intent() rule.launchActivity(intent) } @Test fun should_display_trade_on_receive() { someService.doSomeJob() // Assert […]

Robolectric测试和LiveData

为了得到Kotlin,LiveData和Robolectric的一切,我得到了一个简单的启动画面的活动。 它在运行应用程序时正常工作,但在测试中无法正常工作。 这就像现场数据的回调从未触发或如果没有注册它的话。 这是测试: @Test fun should_redirect_to_login_when_time_is_up_after_onStart() { val timeUp = MutableLiveData<Boolean>() kodein.addConfig { bind<SplashViewModel>(overrides = true) with factory { _: BaseActivity -> mock<SplashViewModel> { on { isTimeUp() } doReturn timeUp } } } val activity = Robolectric.buildActivity(SplashActivity::class.java).create().start().resume().get() timeUp.value = true val startedIntent = shadowOf(activity).nextStartedActivity assertThat(startedIntent).isNotNull assertThat(startedIntent).hasComponent(application.packageName, LoginActivity::class.java) } 活动: class SplashActivity : JapetActivity() { […]

Mockito通缉,但没有调用

我是新手写测试和使用Mockito。 我已经阅读了Stackoverflow中的类似主题,并进行了建议的更改,确保所考虑的类/接口/方法处于打开状态。 我试图遵循这一点 嘲笑构造函数注入依赖 这是我到目前为止的测试 class RegistrationPresenterTest { @Test fun testRegisterSuccess() { val mockService = mock<IHerokuInteractor>() val mockLocal = mock<ILocalStorageInteractor>() val mockView = mock<RegisterView>() val mockRegistrationResponse = HerokuRegisterResponse("hash") val mockPair = ImeiPair("imei","hash") val presenter = RegisterPresenterImpl(mockLocal,mockService) whenever(mockService.register(any())).thenReturn(Observable.just(mockRegistrationResponse)) whenever(mockLocal.clearPreferences()).thenReturn(Observable.just(true)) whenever(mockLocal.putImeiPair(any())).thenReturn(Observable.just(true)) //whenever(presenter.writeImeiPairLocally(any())) How do I specify parameters since it uses a parameter from the register method? presenter.bindView(mockView) […]

如何使用Spek

我试过这个示例 ,但是会引发一个语法错误。 有没有其他的方式来测试Kotlin? 例如,用JUnit或Spek? import kotlin.test.assertEquals import org.jetbrains.spek.api.Spek class BlaherSpecs: Spek() {{ given("Let's test Blaher") { var blaher = Blaher() on("Blaher blah") { val blah = blaher.blah() it("should be Blah!") { assertEquals("Blah1!", blah) } } } }}

Kotlin Spek中的组执行顺序是错误的

这是我的测试: object MySpek : Spek({ val myMock1: MyMock1 = mock() val myMock2: MyMock2 = mock() val handler = StartModeHandler(myMock1, myMock2) val session = mock<Session> { on { user }.doReturn(User.builder().withUserId("userId").build()) } describe("item exists for user") { beforeGroup { reset(digitalPointDao, trackDao) } whenever(myMock1.loadItem(session.user.userId)).thenReturn(Optional.of(MyItem())) whenever(myMock2.loadSomething()).thenReturn(ArrayList()) context("method onLaunch was called") { val response = handler.onLaunch(session) it("should return the response […]