我想就我们在Kotlin项目中尝试单元测试平台特定的Date类时所面临的一个问题寻求一些帮助/建议。
我们的Kotlin多平台库目标是:JVM、Native & JavaScript,并且正在使用特定于平台的日期类:
java.util.Date (JVM)
platform.Foundation.NSDate (原住民)
kotlin.js.Date (JS)
它们通过预期的/实际的Date类公开,并在公共包中的公共域类中使用。例如:
public data class Event {
val id: String,
val start: Date
我正在用mokito-kotlin测试我的代码,我遇到了一个被执行的lambda有错误数据的情况。我发现我从JavaScript那里了解到的“作用域”与预期的不同。当我使用spy()时,lambda的this是不同的。 以下是我的简化测试: @Test
fun strangeLambda() {
open class Foo {
var bar: String? = null
val lambda = { bar }
fun magic() = lambda()
}
Foo().apply {
bar
我创建了一个多平台Kotlin项目(JVM & JS),声明了一个预期的类并实现了它:
// Common module:
expect class Request(/* ... */) {
suspend fun loadText(): String
}
// JS implementation:
actual class Request actual constructor(/* ... */) {
actual suspend fun loadText(): String = suspendCoroutine { continuation ->
Kotlin版本1.0.x-1.1.0支持使用进行开箱即用的单元测试,但是当我进行负载测试时,它抛出了这个异常:
ReferenceError: ok is not defined
at QUnitAsserter.assertTrue_4mavae$ (file:///D:/neoranga/Mis%20documentos/GitHub/FibonacciCounter/app/build/classes/test/lib/kotlin-test-js.js:50:5)
at assertTrue (file:///D:/neoranga/Mis%20documentos/
我有一个多平台库,可以用Ktor执行一些API调用(2.0.0-beta-1)
class DiscoveryServicesImpl(private val client: HttpClient) : DiscoveryServices {
override suspend fun getServers(domain: String): DAAServers {
return client.get(Servers.Domain(domain = domain)).body()
}
}
// Where client is created with speci
您知道不能同步Kotlin中的内联函数吗?我找不到这方面的任何文件。
假设您有一个具有同步方法的类;
/**
* Allows modifying the value in a synchronized way so that the get() and set() are atomic.
*
* Note: anything synchronized cannot be `inline`.
*/
@Synchronized fun safeSet(calculateNewValue: (T) -> T) {
set(calculateNewValue(get()))
}
在处理一些遗留测试时,我最近发现了Mockito及其间谍的一些意外行为。考虑以下类(特别注意SomeInterface的匿名嵌套实现)
public class ClassUnderTest {
private String name = "initial value";
private final SomeInterface impl = new SomeInterface() {
@Override
public void foo(String name) {
// the following call "unwraps
考虑一下我用Kotlin编写的一个简单测试:
class LocaleTest {
@Test fun english() {
val locale = Locale("en")
assertEquals("English", locale.displayLanguage)
assertEquals("en", locale.language)
}
@Test fun indonesia() {
val locale = Locale("id
我是Robolectric的新手,我正在尝试对一个简单的getter/setter代码进行单元测试,但是在使用Robolectric进行测试时,SparseIntArray似乎不能像预期的那样工作,而且根本不会改变。(使用get方法我总是得到'0‘)。
我还尝试为SparseIntArray本身添加一个静态getter,并发现在调用setter之后,数组大小为0。
这里我漏掉了什么?
这是我要测试的代码:
public class MyClass {
private static SparseIntArray sRequestStatus;
public static int
我想从此站点运行示例测试
class HelloAppTest : AutoCloseKoinTest() {
val model by inject<HelloMessageData>()
val service by inject<HelloService>()
@Before
fun before() {
startKoin {
modules(helloModule)
}
}
@Test
fun tesKoinComponents() {
val helloApp = HelloApplication()
h
如何将Long和Int与assertEquals和Kotlin和Junit进行比较
例如
var size: Int = 20
var totalElements: Long = 20
assertEquals(size, totalElements)
java.lang.AssertionError: expected: java.lang.Integer<20> but was: java.lang.Long<20>
Expected :java.lang.Integer<20>
Actual :java.lang.Long<20>
我已经在Kotlin中定义了一个异常,但是当我执行e.message时,它会打印完全限定的异常名,后面跟着消息,但是我只想打印异常消息。
public final class AbcException extends Exception {
private static final long serialVersionUID = 1L;
public AbcException() {
}
public AbcException(String message) {
super(message);
}
public AbcException(String me
我想使用kotlin本机步骤定义。
package cucumber.runtime.kotlin.test;
...
class LambdaStepdefs : En {
init {
Before { scenario: Scenario ->
assertNotSame(this, lastInstance)
lastInstance = this
}
Given("^this data table:$") { peopleTable: DataTable ->
val people