要将Hamcrest与JUnit 4一起使用,我们使用一个assertThat()函数.但是,JUnit 5不再具有assertThat()功能.如何在没有Hamcrest的情况下使用assertThat()?
maven模块junit-jupiter-api和有什么区别junit-jupiter-engine?是否有必要包含两个依赖项build.gradle?
我是否需要编写两个依赖项
testCompile("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
testCompile("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
Run Code Online (Sandbox Code Playgroud)
要么
testCompile("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
Run Code Online (Sandbox Code Playgroud)
足够?
我需要添加依赖junit-vintage-engine吗?
我有一个gradle FAILURE:
..."Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0."
Run Code Online (Sandbox Code Playgroud)
案例描述:
APP/biuld.gradle
//(Required) Writing and executing Unit Tests on the JUnit Platform
testImplementation "org.junit.jupiter:junit-jupiter-api:5.2.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.2.0"
// (Optional) If you need "Parameterized Tests"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.2.0"
// (Optional) If you also have JUnit 4-based tests
testImplementation "junit:junit:4.12"
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:5.2.0"
testImplementation "io.mockk:mockk:1.8.5"
Run Code Online (Sandbox Code Playgroud)
更新了gradle-wrapper.properties
distributionUrl = https .... gradle- 4.4-all .zip to 4.7-all
毕竟那个gradle成功了
创造了测试calss
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestClass {
@Test
internal fun testName() {
Assert.assertEquals(2, …Run Code Online (Sandbox Code Playgroud)是否有任何理由对多个断言进行分组:
public void shouldTellIfPrime(){
Assertions.assertAll(
() -> assertTrue(isPrime(2)),
() -> assertFalse(isPrime(4))
);
}
Run Code Online (Sandbox Code Playgroud)
而不是这样做:
public void shouldTellIfPrime(){
Assertions.assertTrue(isPrime(2));
Assertions.assertFalse(isPrime(4));
}
Run Code Online (Sandbox Code Playgroud) 在Maven项目中,我有一些现有的测试依赖于JUnit 4.由于多种原因,我无法在JUnit 5中迁移这些测试.
实质上,一些测试依赖于使用JUnit 4 runner的库,代码迁移可能需要一些时间.
我想用JUnit 5创建新的测试类,现在已经发布并提供了新的有趣功能.
怎么做 ?
该@BeforeAll注释标记在所有测试之前运行的方法类.
http://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations
但是在所有类中,有没有办法在所有测试之前运行一些代码?
我想确保测试使用一组特定的数据库连接,并且在运行任何测试之前必须进行这些连接的全局一次性设置.
当我试图在junit5中运行测试用例时,我得到了以下execption:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project CRUD-App: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: There was an error in the forked process
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:161)
at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:52)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:59)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:286)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:240)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
Run Code Online (Sandbox Code Playgroud)
的pom.xml
<dependencies>
...
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>5.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M2</version>
</dependency>
</dependencies>
</plugin>
</plugins> …Run Code Online (Sandbox Code Playgroud) 我有以下抽象单元测试类,我的所有具体单元测试类都扩展了它:
@ExtendWith(SpringExtension.class)
//@ExtendWith(MockitoExtension.class)
@SpringBootTest(
classes = PokerApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
public abstract class AbstractUnitTests {
@MockBean
public RoundService roundService;
@MockBean
public RoundRepository roundRepository;
}
Run Code Online (Sandbox Code Playgroud)
@ExtendWith(SpringExtension.class)使用or 和有什么不一样@ExtendWith(MockitoExtension.class)?
我问,使用任何一个注释似乎都没有什么区别,并且两者在我的代码中分别工作 - 允许我使用 Junit5。那么为什么两者都有效呢?
具体测试类:
@DisplayName("Test RoundService")
public class RoundsServiceTest extends AbstractUnitTests {
private static String STUB_USER_ID = "user3";
// class under test
@InjectMocks
RoundService roundService;
private Round round;
private ObjectId objectId;
@BeforeEach //note this replaces the junit 4 @Before
public void setUp() {
initMocks(this);
round = Mocks.round(); …Run Code Online (Sandbox Code Playgroud) 我有一个基于 gradle 的 java 测试框架。
将 junit 更新到 5.7.2 后,每个测试都开始出现此警告:
Jul 21, 2021 10:23:45 AM org.junit.platform.launcher.core.EngineDiscoveryOrchestrator lambda$logTestDescriptorExclusionReasons$7
INFO: 0 containers and 3 tests were Method or class mismatch
Run Code Online (Sandbox Code Playgroud)
使用的 junit 依赖项是:junit-jupiter-api, junit-jupiter-engine, junit-jupiter-params。
可能是什么原因造成的?潜在的解决方案是什么?
我有测试导致错误.我试图在IntelliJ Idea中执行它2018.3.2.所有jupiter和junit依赖项都有版本RELEASE
错误全文:
Dec 26, 2018 1:17:17 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Run Code Online (Sandbox Code Playgroud)
测试有以下观点
import biz.Services.msg.BookTimeMsgService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(MockitoExtension.class)
public class MsgReceiverTest {
@Mock
BookTimeMsgService bookTimeMsgService; …Run Code Online (Sandbox Code Playgroud) junit5 ×10
java ×8
junit ×4
gradle ×2
maven ×2
unit-testing ×2
android ×1
assertions ×1
assertthat ×1
dependencies ×1
hamcrest ×1
junit4 ×1
kotlin ×1
mockito ×1
spring ×1