来自jetbrains博客:
IntelliJ IDEA支持实际运行为JUnit 5编写的测试的能力 - 不需要使用其他库(例如Gradle或Maven插件),您只需要包含JUnit 5依赖项.
我是Java和IntelliJ IDEA的新手,我不清楚使用Junit 5进行测试应该采取哪些步骤.
为什么JUnit 5 package-private中的默认访问修饰符是?
JUnit 4中的测试必须是公开的。
将其更改为程序包专用有什么好处?
我看到一些人遇到了这个问题,并且已经挣扎了几个星期,但无法在同一个项目上运行 JUnit4 和 JUnit5(我需要它来维护一些旧的测试)。我注意到,如果删除 Maven Surefire 插件,我可以运行 JUnit4 测试,而当它添加到 POM 时,只能运行 JUnit5 测试。
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)
这种依赖关系也会发生类似的情况。如果我将它添加到 POM 文件中,即使有 Maven Surefire 插件,我也可以运行 JUnit4 测试。但是,我需要删除它才能运行 JUnit5 测试。
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是我完整的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hmhco</groupId>
<artifactId>tests</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<rest-assured.version>3.0.0</rest-assured.version>
<json-schema-validator.version>3.3.0</json-schema-validator.version>
<junit5.version>5.2.0</junit5.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId> …Run Code Online (Sandbox Code Playgroud) 我在 Spring Boot 应用程序中为我的服务编写了 JUnit 5 测试。我曾经@MockBean模拟PasswordEncoder和其他豆子,但我获得了NullPointerException.
我总是在通话过程中收到 NullPointerException when:
when(compteRepository.getByLogin(anyString())).thenReturn(Optional.of(acc));
服务
package com.compte.application.impl;
import com.compte.application.CompteService;
import com.compte.domain.exceptions.EntityNotFoundExcpetion;
import com.compte.domain.model.Compte;
import com.compte.domain.model.CompteUpdatedData;
import com.compte.domain.repository.CompteRepository;
import com.compte.domain.utils.CompteUtil;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.time.LocalDate;
import java.util.Optional;
/**
* @author mbint
*/
@AllArgsConstructor
public class CompteServiceImpl implements CompteService{
private final static Logger LOGGER = LoggerFactory.getLogger(CompteService.class);
private CompteRepository CompteRepository;
private PasswordEncoder passwordEncoder;
@Override
public Optional<Compte> getByLogin(String login) {
return CompteRepository.getByLogin(login);
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Spring Cloud 项目中创建非常简单的 JUnit 测试:
@SpringBootTest(classes = {ProductMapper.class })
public class TestingWebApplicationTests {
@Test
public void contextLoads() {
}
}
import org.mapstruct.Mapper;
@Mapper(config = BaseMapperConfig.class)
public interface ProductMapper {
ProductDTO toDTO(Product product);
ProductFullDTO toFullDTO(Product product);
Product map(ProductFullDTO productDTO);
ProductFilter toFilter(ProductFilterDTO dto);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在最新的 Intelij 中运行测试时,出现此错误。
java:由于错误元素 java.util.ArrayList 中存在问题,因此未为 ProductMapper 创建任何实现。提示:这通常意味着其他一些注释处理器应该处理错误的元素。您还可以通过设置 -Amapstruct.verbose=true 作为编译参数来启用 MapStruct 详细模式。
你知道我该如何解决这个问题吗?
我的 Gradle 配置和 JUnit 遇到了一个非常奇怪的问题,我尝试更新到 5.7+,但出现以下异常:
org/junit/jupiter/api/extension/ScriptEvaluationException
java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ScriptEvaluationException
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我的 Gradle 版本是 6.8.3。另一方面,这是我使用下面的当前配置得到的异常,我尝试了很多版本和库的组合,但似乎它们都不起作用:
Caused by: org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-vintage' failed to discover tests
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:111)
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:85)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:92)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
... 25 more
Caused by: org.junit.platform.commons.JUnitException: Failed to parse version of junit:junit: 4.13.2
at org.junit.vintage.engine.JUnit4VersionCheck.parseVersion(JUnit4VersionCheck.java:54)
at org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:37)
at org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:32)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:62)
Run Code Online (Sandbox Code Playgroud)
plugins {
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'com.palantir.docker' version '0.26.0'
}
group = …Run Code Online (Sandbox Code Playgroud) 当通过Gradle启动JUnit测试时,是否已经有可能生成HTML报告?任何提示或评论都表示赞赏.
我想在Eclipse中使用JUnit 5创建一个JUnit测试(Oxygen 4.7.1a).此JUnit测试应位于名为Test的单独src文件夹中.但是,由于我是JUnit和Java 9的新手,因此遇到了以下问题.
我不想使用像Gradle或Maven这样的构建工具.
因为我有两个不同的src文件夹,一个用于项目src,另一个用于测试用例:
我有一个带有数据库和 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 …
我想配置 Maven 以使用这些依赖项运行 Junit 5 测试:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.7.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但我得到了例外:
"C:\Program Files\Java\jdk-14\bin\java.exe"
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils
at org.junit.platform.launcher.core.LauncherFactory.loadAndFilterTestExecutionListeners(LauncherFactory.java:113)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:99)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:72)
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:46)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:31)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.util.ClassNamePatternFilterUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
Run Code Online (Sandbox Code Playgroud)
你知道我该如何解决这个问题吗?