我只想测试是否使用google-truth抛出了给定消息的异常.
使用junit很容易做到这一点@Test(expected=,但是我无法弄清楚如何用真理做到这一点.ThrowableSubject周围没有样本.
我应该坚持使用普通JUnit的这种测试吗?
我有一个Android库(称为api)gradle模块作为一个较大项目的一部分。我只是将整个项目迁移到AndroidX。我现在在apilib 上运行检测测试时遇到此错误:
Task :api:checkDebugAndroidTestDuplicateClasses FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':api:checkDebugAndroidTestDuplicateClasses'.
> 1 exception was raised by workers:
java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules jetified-guava-25.1-android.jar (com.google.guava:guava:25.1-android) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)
Run Code Online (Sandbox Code Playgroud)
如果我检查debugAndroidTest变体的运行时类路径:
./gradlew api:dependencies --configuration debugAndroidTestRuntimeClasspath | grep --color -E "guava|$"
Run Code Online (Sandbox Code Playgroud)
我得到这个输出。我可以看到问题:
------------------------------------------------------------
Project :api
------------------------------------------------------------
debugAndroidTestRuntimeClasspath - Resolved configuration for runtime for variant: debugAndroidTest
+--- project :test_utils
| +--- project :core
...
| +--- …Run Code Online (Sandbox Code Playgroud) 遇到这个要点:https : //gist.github.com/chemouna/00b10369eb1d5b00401b之后,我注意到它正在使用Google Truth库:https : //google.github.io/truth/。因此,我首先按照build.gradle在Android Studio中的文件中添加库的过程进行操作:
buildscript {
repositories.mavenLocal()
}
dependencies {
testImplementation "com.google.truth:truth:0.40"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我想为我的断言java类的Truth入口点添加静态导入时:
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
Run Code Online (Sandbox Code Playgroud)
我收到错误,该符号Truth无法解析。我试图重建我的项目并实现此处所述的解决方案:导入中未识别的AndroidTestCompile 依赖项,主要运行以下gradle任务:
但问题仍然存在。
有什么帮助吗?
我是否应该在build.gradle文件中实际添加这些行?:
buildscript {
repositories.mavenLocal()
}
Run Code Online (Sandbox Code Playgroud)
如果我已经有这些:
repositories {
mavenCentral()
jcenter()
google()
}
Run Code Online (Sandbox Code Playgroud) 我遇到了谷歌真相https://google.github.io/truth/,并考虑尝试一下.我阅读了该网站上的信息,但仍有一个基本问题.
真理是JUnit的替代品吗?我应该如何编写@test方法和测试套件,就像我在JUnit中编写的那样,并通过Jenkins自动执行测试执行?或者说,真理只是让你的断言代码变得美丽而其他一切都保持不变?
Truth还需要JUnit框架(或类似JUnit)的帮助吗?
谢谢!
我想测试一个列表包含一个对象的实例。
例如,对于一个实例:
assertThat(mylist).containsExactly(Matchers.any(ExpectedType.class));
Run Code Online (Sandbox Code Playgroud)
从test返回的数组obj确实包含instance的一个对象ExpectedType。但是我的测试失败了:
java.lang.AssertionError:<[ExpectedType @ 7c781c42]>完全包含<[ExpectedType的实例]>是不正确的。它缺少<[ExpectedType的实例]>,并且具有意外的项目<[ExpectedType @ 7c781c42]>
如何编写此测试?
我正在尝试将Google Truth框架包含在我的项目中进行测试。我遵循了有关如何进行项目设置的文档。
这来自我的应用程序的build.gradle文件:
dependencies {
...
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
androidTestImplementation 'com.google.truth:truth:0.43'
}
Run Code Online (Sandbox Code Playgroud)
同步过程成功完成。
然后,我尝试运行本地单元测试,例如:
import org.junit.Test
import com.google.common.truth.Truth.*
class CustomExampleUnitTest {
@Test
fun testBlank_isCorrect() {
assertThat("".isBlank()).isTrue()
}
}
Run Code Online (Sandbox Code Playgroud)
我收到Kotlin编译器错误:无法解析的参考:真相
有几件事要注意:
因此,在完成上述步骤之后,我尝试运行测试,但仍然遇到未解决的问题。
任何人都可以尝试阐明这一点吗?有没有人遇到这个。我真的很感谢任何帮助!
google-truth ×6
android ×2
java ×2
junit ×2
testing ×2
androidx ×1
gradle ×1
guava ×1
hamcrest ×1
unit-testing ×1