Tag: junit

DataJPATest Junit方法命名和NullPointerExceptions

我正在使用SpringBoot / Kotlin / JPA / Hibernate / Junit,并且拥有JpaServiceTest类,可以执行与单个实体有关的存储库方法。 JpaService类的方法名遵循约定findByXXXXId , findAll , addXXXX , addXXXX和deleteXXXX 。 为了保持一致性,我使用相同的约定为JpaTest类中的方法命名。 我的JpaTest类有两个findById场景,其中’Null’预期是另一个返回映射实体的地方。 我的应用程序按预期方式工作,但是我的测试类在findById方案上失败,这个方案有望返回一个有效的实体。 The service class @Service(“MyService”) @Transactional internal class JpaMyService(val MyRepo: MyRepository) : MyService { val log = LoggerFactory.getLogger(“MyService”) override fun findByMyId(MyId: Long): MyDto? { log.debug(“Retrieving My: {}”, MyId) return MyRepo.findOne(MyId)?.toDto() } override fun findAllMys(): List { […]

测试两个目录树的相等性

我正在集成测试我的代码的一部分,在SVN下创建目录树。 这要求我测试一下目录结构和文件是否是我所期望的。 一方面,我有期望的目录树与我想要的文件,另一方面,从SVN文件svn export (喜欢svn export svn co避免.svn噪音)。 但是,有没有可以断言两个目录树的库? 我想到的最后一招是自己做一个迭代比较。 基本上我正在寻找一个API可以接受两个目录,并告诉我,如果他们是平等的或不。 东西在线上 boolean areDirectoriesEqual(File dir1, File dir2)

如何在Android项目中的Java模块上模拟Mockito 2的最终课程?

我有一个Android Clean Architecture项目,在Kotlin中有三个模块: 数据(Android库) domaine(Java库) 演示(Android应用程序) 这3个模块都有用junit编写的unit testing 。 但是对于Kotlin,每个课程都是默认的最终课程。 我很快就遇到了问题: 如何用mockito来嘲笑最后一堂课 现在可以用Mockito 2 可以通过mockito扩展机制来完成,方法是创建包含一行的文件/mockito-extensions/org.mockito.plugins.MockMaker : mock-maker-inline 此解决方案在数据模块(Android库)和表示模块(Android应用程序)上运行良好, 但在我的domaine模块(Java库)上不起作用 。 我知道这个问题已经被问到( 如何用mockito来模拟最后一个类 , Mock对象用Mockito来调用最终类的静态方法 ),但是我没有find我正在寻找的答案。

未解决的参考:使用@ExtendWith注释的SpringExtension

我正在使用SpringBoot中的Junit 5来利用它提供的一些新function。 基本的设置工作,我可以使用新的注释,如@BeforeAll和@AfterAll但是,我相信,因为我不能解决@ExtendWith(SpringExtension::class)我不能初始化我的测试类,以使用注入资源。 我已经尝试将所需的依赖项添加到我的build.gradle文件,但我假设我缺少一个键依赖项: buildscript { ext.dokka_version = ‘0.9.15’ ext.dockerVersion = ‘1.2’ ext.junit4Version = ‘4.12’ ext.junitVintageVersion = ‘4.12.2’ ext.junitPlatformVersion = ‘1.0.2’ ext.junitJupiterVersion = ‘5.0.2’ ext.kotlin_version = ‘1.1.60’ ext.log4jVersion = ‘2.9.0’ ext.springVersion = ‘1.5.8.RELEASE’ repositories { mavenCentral() jcenter() } dependencies { // Needed for the ‘kotlin’ plugin classpath(“org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}”) // Needed for the ‘org.springframework.boot’ plugin classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springVersion}”) // […]

如何更改TestEngine ID以运行JUnit 5?

我有我的Kotlin Android项目的以下依赖项{ dependencies { … testImplementation “org.junit.platform:junit-platform-engine:1.0.2” testImplementation “org.junit.platform:junit-platform-gradle-plugin:1.0.2” testImplementation “org.junit.platform:junit-platform-launcher:1.0.2” testImplementation “org.junit.platform:junit-platform-suite-api:1.0.2” testImplementation “org.junit.jupiter:junit-jupiter-api:5.0.2” testImplementation “org.junit.jupiter:junit-jupiter-engine:5.0.2” testImplementation “org.junit.jupiter:junit-jupiter-params:5.0.2” } 运行我的测试时得到的错误是: org.junit.platform.launcher.core.DefaultLauncher handleThrowable WARNING: TestEngine with ID ‘junit-vintage’ failed to discover tests java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter … Caused by: java.lang.ClassNotFoundException: org.junit.runner.manipulation.Filter Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest; 如果我的测试引擎id是junit-vintage ,那么是否可以改变它? 它似乎仍然使用junit 4引擎。 我怎样才能改变这个,所以它使用junit-jupiter? 我从我的依赖中删除了junit-4.12,所以我想知道为什么它使用junit-vintage引擎而不是junit-jupiter。 目前使用: Android Studio 3.0 […]

可以在Kotlin中编写函数(即在顶层)。 我可以unit testing那些顶级?

如果我在顶层编写代码(在课堂之外)。 我可以编写此代码的JUnit测试,而无需为此测试目的创建类?

Androidunit testing没有被嘲笑

我遵循本指南https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support,但我坚持这个错误: junit.framework.AssertionFailedError: Exception in constructor: testSaveJson (java.lang.RuntimeException: Method put in org.json.JSONObject not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details. 我修改了Gradle构建像指南说,​​但它没有区别 testOptions { unitTests.returnDefaultValues = true }

测试环境配置:Android + JUnit 5 + Mockito + Spek + Kotlin

我很难配置基于JUnit Jupiter(5)的测试环境。 我有两个不同的错误: WARNING: TestEngine with ID ‘spek’ failed to discover tests org.junit.platform.commons.util.PreconditionViolationException: Could not load class with name… Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61)… 配置如下。 主build.gradle : apply plugin: ‘org.junit.platform.gradle.plugin’ buildscript { ext.kotlin_version = ‘1.1.4-3’ repositories { google() jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:3.0.0-beta5’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” classpath “org.junit.platform:junit-platform-gradle-plugin:1.0.0” classpath “de.mannodermaus.gradle.plugins:android-junit5:1.0.0” } } […]

Android Studio 3 / Kotlin代码覆盖

我的Android应用程序是多模块项目: include (android-app/kotlin-android)’:application’, (pure kotlin)’:presentation’, (pure kotlin)’:domain’, (android-library/kotin-android)’:dataproviders’ 我正在使用Junit / Mockito进行测试,并且只有为kotlin android模块生成代码覆盖的问题。 经过测试的行对于android studio是可见的。 在ui.viewmodel包中测试类: 但是,对于纯粹的kotlin (例如,域,演示)测试覆盖工作正常: 我正在使用Android Studio 3.0 Canary 8您可以在github上查看我的build.gradle文件: 的build.gradle dependencies.gradle application.build.gradle presentation.build.gradle Android应用程序模块中的示例测试: MostPopularViewModelTest 纯kotlin模块中的示例测试: MostPopularPresenterTest 有人可以帮我解决我的问题吗? 我尝试通过Jacoco生成代码覆盖,但它也没有显示代码覆盖率。

SpringBoot ArrayIndexOutOfBoundsException MethodParameter.getGenericParameterType

我正在使用spring引导,我试图建立一个测试场景,我调用一个bean,调用一个模拟JPARepository使用mockito。 这是相关的代码: 存储库 @Repository interface FooRepository : JpaRepository 服务 @Service class FooService( private val FooRepository: FooRepository ) { @Transactional fun call(input: String) = FooRepository.saveAndFlush( Foo(input) ) } 和测试 @SpringBootTest @RunWith(SpringRunner::class) @Import(FooServiceTest.MockFooRepositoryConfiguration::class) class FooServiceTest { @Autowired private lateinit var fooService: FooService @Autowired private lateinit var fooRepository: FooRepository @Test fun test1() { FooService.call(“xxx”) Mockito.verify(FooRepository, Mockito.times(1)).saveAndFlush(“xxx”) } @Configuration […]