Tag: junit

我怎样才能让我的JUnit测试在我的Kotlin + Gradle项目中编译和运行?

根据上一个问题,我创建了一个Kotlin + Gradle项目。 我添加了两个源文件: SRC / HelloWorld.kt package com.example.hello fun main(args : Array<String>) { println("Hello, world!") } 测试/ HelloWorldTest.kt package com.example.hello import org.junit.Assert import org.junit.Test class HelloWorldTest { @Test fun testPasses() { Assert.assertTrue(true) } @Test fun testFails() { Assert.assertTrue(false) } } 我还将src/目录标记为源根目录,并将其作为测试源根目录。 如果我在其他地方创建与这些源文件完全相同的目录结构,并创建一个非Gradle IntelliJ项目,我可以编译并运行此代码,包括测试。 但是,在我的Gradle版本的项目中,我无法从IntelliJ IDEA构建测试。 我得到以下错误: Error:(3, 12) Kotlin: Unresolved reference: junit Error:(4, 12) […]

如何编译使用hamcrest的Kotlin单元测试代码是'

我想为我的Kotlin代码编写单元测试,并使用junit / hamcrest匹配器,我想使用is方法,但是它是Kotlin中的保留字。 我怎样才能得到以下编译? class testExample{ @Test fun example(){ assertThat(1, is(equalTo(1)) } } 目前我的IDE,InteliJ正在强调,作为一个汇编错误,说它期待着a )以后呢?

用KOTLIN android studio进行JUnit测试

我对android开发很陌生,最近做了我的第一个项目。 这只是一个加法,减法,乘法和除法的基本计算器。 现在,我正在考虑使用JUnit和KOTLIN进行单元测试。 我应该怎么做呢? 我一直在四处寻找,不知道。 计算器Java代码: package com.example.zhiwen.calculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity{ Button plus,minus,times,divide; TextView textview3; EditText first, second; double no1 = 0, no2 = 0; double answer = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); plus = […]

如何使用PowerMock模拟Kotlin类(最终)?

我想知道如何用PowerMock模拟kotlin final类,以便测试它。 我遵循指导来测试Java最终班,但我仍然有这个错误 Cannot subclass final class 有没有办法做到这一点?

Java注释处理器 – 注释的Kotlin类单元测试

我有一个注释处理器库 ,我想为Kotlin工作,但是我的单元测试遇到了麻烦。 如果有人能给我一些建议,我将不胜感激。 我目前的单元测试实现使用Google的编译测试库。 我创建输入和输出类并将它们存储在资源目录中。 然后在单元测试期间,编译测试库编译输入java类,执行注释处理器,然后将生成的类与资源目录中的预期输出类进行比较。 这里是我所指的一个例子(从我的项目): 单元测试类 资源(输入和预期的输出类 这对我所有的基于java的单元测试都很有帮助。 但是,当我尝试使用Kotlin类编写一些测试时,我的测试无法加载类。 我相信这是由于编译测试库首先是一个Java特定的库(我没有看到他们的项目中提到Kotlin) 目前我遇到以下问题: java.lang.IllegalArgumentException: Compilation unit is not of SOURCE kind: "/C:/dev/gsonpath/gsonpath-compiler/build/resources/test/adapter/auto/field_types/primitives/valid/TestValidPrimitives.kt" at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:137) at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:107) at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:64) at com.google.testing.compile.Compilation.compile(Compilation.java:69) at com.google.testing.compile.JavaSourcesSubject$CompilationClause.compilesWithoutError(JavaSourcesSubject.java:281) 这个问题相当明显,正在使用不正确的编译器。 当我的文件扩展名不是'.java'时引发异常。 如果我尝试使用'.java'文件扩展名加载Kotlin类,它不起作用,因为它的Java语法不正确。 有没有人遇到过这个问题,并解决了它? 我看了一些其他的注释处理器(比如DBFlow ),而且他们不用这种方式编写单元测试。 由于Kotlin最近才在注释处理中涉足,也许我是第一个有这个问题的?

如何用mockito嘲笑最后一堂课

我有最后一堂课,像这样: public final class RainOnTrees{ public void startRain(){ // some code here } } 我正在使用这个类,像这样的其他类: public class Seasons{ RainOnTrees rain = new RainOnTrees(); public void findSeasonAndRain(){ rain.startRain(); } } 在我的JUnit测试类Seasons.java我想模拟RainOnTrees类。 我怎样才能用Mockito做到这一点?

Kotlin:单元测试在gson之后声明对象

我有这样的JUnit测试: Test fun testCategoriesLoading() { val subscriber = TestSubscriber<List<ACategory>>() service.categories().subscribe(subscriber) subscriber.awaitTerminalEvent() subscriber.assertNoErrors() } 服务是Retrofit ,它使用GsonConverter将json反序列化成 data class ACategory(val id: String, val title: String, val parentId: String?, val hasChildren: Boolean) 实例。 测试即将通过,即使ACategory填充id = null,title = null等 所以,就我所知,使用反射的gson和kotlin懒散地解决了第一次访问时的这种可空性约束。 有没有办法强制这个决心? 一些好看的解决方案,无需手动直接访问字段? 我真的不想手写每一个断言。

为什么Mockito不能在Kotlin中使用数字类型来模拟一个通用的参数类型?

我们正在将我们的项目转移到Kotlin语言。 我们决定从测试开始,但面临一些奇怪的行为。 这是我们的测试案例: Service.java public final class Service { private final JdbcTemplate jdbcTemplate; public Service(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public long check() { return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM table", Long.class); } } JavaTest.java (工作正常) @RunWith(MockitoJUnitRunner.class) public final class JavaTest { @Mock private JdbcTemplate jdbcTemplate; @InjectMocks private Service testSubject; @Test public void test() { //given […]

纯粹的kotlin模块中的“空测试套件”。 (斯波克/ Android装置)

我的Android应用程序是多模块项目: include (android-app/kotlin-android)':application', (pure kotlin)':presentation', (pure kotlin)':domain', (android-library/kotin-android)':dataproviders' 模块:应用程序和:数据提供者与Spock正常工作,测试运行和完成没有问题。 但是:纯粹kotlin模块的表现和领域与spock框架有问题。 有我简单的例子: MostPopularPresenterTest.groovy class MostPopularPresenterTest extends Specification { def "exampleTest"(){ when: int i = 1 then: i == 1 } } 此测试以错误结束: 未找到类:“pl.hypeapp.presentation.mostpopular.MostPopularPresenterTest”空的测试套件。 但是,用Java / Junit4写的测试通过并没有发生任何错误: MostPopularPresenterTest2.java public class MostPopularPresenterTest2 { @Test public void test(){ assertEquals(1, 1); } } 我正在使用Android Studio 3.0 Canary 5你可以看看我在github上的build.gradle文件: 的build.gradle dependencies.gradle […]

Mockito嘲笑像间谍:Kotlin

我正在研究一个kotlin jar,我试图模拟一个函数的输入 class MyService fun serviceFunction(input: ClassFromAnotherLibrary): Output { val foo = input.memberFunction() 只是碰巧, memberFunction已经通过包级扩展函数被添加到我的包中的那个类 fun ClassFromAnotherLibrary.memberFunction() : Foo { val mapper = jacksonObjectMapper() return mapper.readValue(this.serializedFoo, Foo::class.java) } 现在我想写一个serviceFunction的测试,但我想嘲笑memberFunction调用(我有单独的测试)。 所以在我的Mockito JUnit测试中,我做了以下 val service = Service() val mockClassFromAnotherLibrary = mock<ClassFromAnotherLibrary>() val mockFoo = mock<Foo>() whenever(mockClassFromAnotherLibrary.memberFunction()) .thenReturn(mockFoo) service.serviceFunction(mockClassFromAnotherLibrary) 我期望memberFunction的实际实现永远不会被调用,而且我的模拟会拦截任何尝试调用它,而是返回我的mockFoo 。 实际上 , whenever设置模拟出调用底层函数的方法时,当mapper尝试读取serializedFoo (当然是null)时,会导致NullPointerException 。 我的问题是: 为什么地球上真正的memberFunction被执行? […]