我有一个带有数据库和 rabbitmq 用法的小型 Spring Boot 应用程序。所以我想用集成测试(H2 + apache qpid)进行测试。
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestSpringConfig.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Run Code Online (Sandbox Code Playgroud)
正如我的应用程序所期望的那样,数据库和 mq 我使用 @BeforeAll 来启动它:
@BeforeAll
public void before() {
startMessageBroker();
startDatabase();
}
Run Code Online (Sandbox Code Playgroud)
问题是我的 Web 应用程序在 @BeforeAll 中定义的 database/mq 之前启动。
org.springframework.test.context.junit.jupiter.SpringExtension:
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor,
BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback,
ParameterResolver {
// ...
@Override
public void beforeAll(ExtensionContext context) throws Exception {
getTestContextManager(context).beforeTestClass();
}
// ...
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
getTestContextManager(context).prepareTestInstance(testInstance);
}
// ...
Run Code Online (Sandbox Code Playgroud)
Web …
I am transitioning from junit4 to junit5 on a project and trying to figure out how to test logs. Previously, I used
@Rule
OutputCapture outputCapture = new OutputCapture();
Run Code Online (Sandbox Code Playgroud)
and would then write an assertion using outputCapture.toString(), for example
assertThat(outputCapture.toString(),containsString("status=200"));
Since @Rule annotation hasn't been implemented yet in junit5, I can't use outputCapture. Any ideas what to do instead? Thanks!
几乎所有我见过的动态测试的例子都可以使用参数化测试进行重写和编写.因此,这是一个实际场景,其中动态测试是唯一的选择,或者至少比参数化测试更合适.
JUnit 5文档中唯一的"真正"动态测试示例并不实用.
有任何想法吗?
如何为 Android 单元测试配置 JUnit 5?我试过:
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0")
Run Code Online (Sandbox Code Playgroud)
但它不起作用,当我运行以前最简单的单元测试时:
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
Run Code Online (Sandbox Code Playgroud)
我得到错误:
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:31)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:36)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 1
Empty test suite.
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行这样的单元测试:
@org.junit.jupiter.api.Test
void junit5codeCoverage() {
final int result = new Foo().junit5();
Assert.assertEquals(Looper.getMainLooper().getThread(), Thread.currentThread());
assertEquals(-1, result);
}
Run Code Online (Sandbox Code Playgroud)
这是一个带有 Android 依赖项(即)和 Robolectric的Junit5测试Looper.getMainLooper()。
我正在使用mannodermaus 的 junit5 android 插件,它允许在 Android 设置中运行 junit5。但这不是开箱即用的,因为它不会加载 robolectric。所以我尝试了alixwar 的分支,它可以解决 robolectric 和 junit5 测试覆盖率,但仍然不会使用 Android 类。
此外,我还开始研究如何在 junit5 上运行 robolectric 测试,这需要了解其RobolectricTestRunner工作原理以及如何将代码移植到 JUnit5 平台。我对新的 junit5 平台知之甚少,但我发现我可以在运行器之上构建org.junit.platform.runner.JUnitPlatform,以遵循测试运行器概念,这是junit-platform-runner包的一部分。但这与最初的 Junit4 相去甚远,SandBoxTestRunner以至于我无法完成移植。
那么,实现 robolectric junit5 支持的最可行路径是什么,或者我缺少任何(明显的)概念?
在JUnit 4中,可选的断言消息是assertEquals方法中的第一个参数.在JUnit 5中,它是最后一个.
是否有任何技术原因将其移至最后位置?如果是这样,哪个?
我正在尝试编写一个 JUnit5 测试,断言最终在接下来的 15 秒内满足条件。我怎样才能最好地实现这一点?
我在想这个问题:
assertTimeout(ofSeconds(15), () -> {assertThat(condition, is(true));});
Run Code Online (Sandbox Code Playgroud)
但要反复测试条件
根据文档(https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/junit/jupiter/EnabledIf.html#expression--),您可以使用@EnabledIf测试类或测试方法上的注释,如下所示:
@EnabledIf("${smoke.tests.enabled}")
或者
@EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")
其中字符串是 Spring Environment 中可用属性的占位符。
假设我有以下application.yml文件:
smoke:
tests:
enabled: true
spring:
profiles:
active: test
Run Code Online (Sandbox Code Playgroud)
以及以下测试类:
@EnabledIf(value = "#{${spring.profiles.active} == 'test'}", loadContext = true)
@SpringBootTest
public class SomeClassForTests {
@Autowired SomeType autowiredType;
@Test
public void someTest() {
// test logic...
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我收到以下错误:
Test ignored.
org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.springframework.test.context.junit.jupiter.EnabledIfCondition]: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'test' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe …Run Code Online (Sandbox Code Playgroud) tl;dr:如何在所有测试运行之前将自定义数据提供程序实例化为 Spring 组件?
是否有一种聪明的方法可以将 Spring 组件注入到实现 的自定义 JUnit Jupiter 扩展中BeforeAllCallback?该beforeAll方法应该在MyTestClass执行之前触发一个复杂的过程@ExtendWith(OncePerTestRunExtension.class)。
我创建了一个 Spring Boot 应用程序 ( src/main/java),它为我的测试 ( src/test/java) 提供了必要的数据。为测试准备数据可能需要长达几个小时的时间。它还使我可以抽象访问一些休息端点。
在所有测试类的过程之间,数据不会改变。所以我只想提取一次数据。
只在一个类中编写所有测试是可行的,但我认为分成不同的类可以提供更好的概览。
我在使用 Maven 的 SpringBoot 后端应用程序服务器上使用 JUnit5。这是sonar-project.properties位于项目根目录的文件:
sonar.host.url=https://sonarcloud.io
sonar.login=xxx
sonar.organization=xxx
sonar.projectKey=xxx
sonar.sourceEncoding=UTF-8
sonar.language=java
sonar.java.source=12
sonar.sources=src/main/java
sonar.test=src/test
sonar.java.binaries=target/classes
sonar.junit.reportPaths=target/test-results/TEST-**.xml
Run Code Online (Sandbox Code Playgroud)
我使用sonar-scanner命令行在构建/测试后运行更新项目。
Overviewsonar-cloud 上的板子是这样的:
我至少让单元测试得到认可,但不知何故,我在代码覆盖率方面仍然处于 0%。此外,这是Measures董事会:
显然,我的测试不包括任何行。现在,我知道这意味着我很可能没有正确连接测试结果,但我不知道该怎么做。
同样让我感到困惑的是,尽管 SonarQube 识别出我的测试,但它实际上说测试本身的代码行没有经过测试。这是什么意思?
junit5 ×10
java ×9
junit ×6
spring-boot ×3
android ×2
junit4 ×2
spring ×2
spring-test ×2
unit-testing ×2
junit-rule ×1
logging ×1
robolectric ×1
sonarqube ×1
testing ×1