简体   繁体   English

how Unit test methods of Kotlin class with private constructor, companion object and extending another class without mocking

[英]how Unit test methods of Kotlin class with private constructor, companion object and extending another class without mocking

My class is extending another class and has companion object and private constructor.我的 class 正在扩展另一个 class 并具有配套的 object 和私有构造函数。 I have to Unit test getLastKnownLocation() method.我必须对 getLastKnownLocation() 方法进行单元测试。 I dont want mock GpsTrackingService class and would like instantiate it in my test class.我不想模拟 GpsTrackingService class 并且想在我的测试 class 中实例化它。 How I can do it?我该怎么做? Thanks for any help, in advance感谢您的帮助,提前

 class GpsTrackingService private constructor(private val context: Context, private val Snavigator: SNavigator?) : LocationListener {
    
        companion object {}
    
    getLastKnowLocation() {}
    }

You can make the constructor internal , mock the dependencies, and use them to instantiate your class.您可以将构造函数设为internal ,模拟依赖项,并使用它们来实例化您的 class。

class GpsTrackingServiceTest {
   
   private lateinit var context: Context
   private lateinit var sNavigator: SNavigator
   private lateinit var trackingService: GpsTrackingService

   @Before
   fun setup() {
      context = Mockito.mock(Context::class.java)
      sNavigator = Mockito.mock(SNavigator::class.java)
      trackingService = GpsTrackingService(context, sNavigator)
   }

   @Test
   fun test_getLastKnowLocation() {
      trackingService.getLastKnowLocation()
      // do your verifications, etc
   }
}

This worked for me这对我有用

class GpsTrackingServiceTest {
   
   private lateinit var mContext: Context
   private lateinit var sNavigator: SNavigator
   private lateinit var trackingService: GpsTrackingService

   @Before
   fun setup() {
      cmContext = InstrumentationRegistry.getInstrumentation().context
      sNavigator = Mockito.mock(SNavigator::class.java)
      trackingService = GpsTrackingService.getInstance(mContext, redNavigator)
   }

   @Test
   fun test_getLastKnowLocation() {
      trackingService.getLastKnowLocation()
      // do your verifications, etc
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin-在另一个对象(不是类)内部可以替代伴侣对象吗? - Kotlin - any substitute for companion object inside of another object (not class)? 如何在伴侣对象Kotlin中保留单例类对象的引用 - How to keep the reference of the singleton class object in the companion object, Kotlin 如何使用kotlin从另一个类调用同伴内部的扩展对象? - How to call extension object inside companion from another class using kotlin? Kotlin 中 object / 私有构造函数 / 伴侣 object 之间的区别? - Difference between object / private constructor / companion object in Kotlin? 在 Kotlin 中访问同伴对象中的父类变量 - Access Parent class variables in companion Object in Kotlin 使用伴侣对象返回Kotlin中的类的实例 - Using companion object to return an instance of the class in Kotlin 用主构造函数和辅助构造函数扩展Kotlin类 - Extending a Kotlin class with a primary and secondary constructor 从其同伴 object 扩展数据 class 时没有可见性 - No visibility when extending function for a data class from its companion object 有没有办法在抽象类和它的实现类之间共享数据而不使用 Kotlin 中的伴随对象 - Is there a way to share data between an abstract class and it's implementation classes without using companion object in Kotlin Kotlin中每个类只允许一个伴随对象 - only one companion object is allowed per class in Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM