在eclipse中不能运行 Junit
Can not run Junit in eclipse
我正在尝试 运行 spring 使用以下代码启动 crud 示例
@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
public class CreateUserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private CreateUserService createUserService;
@Test
public void whenSaveUser_shouldReturnUser() {
User user = new User();
user.setName("Test Name");
when(userRepository.save(ArgumentMatchers.any(User.class))).thenReturn(user);
User created = createUserService.createNewUser(user);
assertThat(created.getName()).isSameAs(user.getName());
verify(userRepository).save(user);
}
}
但在 运行 之后出现以下错误。
“未找到测试 运行ner 'junit5'”
任何人都请帮助我。
Please check the error message from this file.
编辑:我找到了根本原因。
JUnit 5 使用与 JUnit 4 不同的包中的注释。当您开始测试时,Eclipse 会自动为 JUnit 5 创建启动配置,但如果您的注释不在新包中,Eclipse 将找不到测试并将显示错误消息。
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
// ... other imports from org.junit.jupiter.api
旧注释:
import org.junit.Test;
// ... imports without "jupiter" in their name
您需要在 Maven 项目中添加以下依赖项才能使用 JUnit 5:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
解决方法:使用 JUnit 4。
您需要在启动配置中 select JUnit 4。否则会显示此错误。
从主菜单 select 运行 -> 运行 配置,然后 select 左侧导航栏中的 JUnit 测试配置。
我正在尝试 运行 spring 使用以下代码启动 crud 示例
@SpringBootTest @RunWith(MockitoJUnitRunner.class)
public class CreateUserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private CreateUserService createUserService;
@Test
public void whenSaveUser_shouldReturnUser() {
User user = new User();
user.setName("Test Name");
when(userRepository.save(ArgumentMatchers.any(User.class))).thenReturn(user);
User created = createUserService.createNewUser(user);
assertThat(created.getName()).isSameAs(user.getName());
verify(userRepository).save(user);
}
}
但在 运行 之后出现以下错误。
“未找到测试 运行ner 'junit5'”
任何人都请帮助我。
Please check the error message from this file.
编辑:我找到了根本原因。
JUnit 5 使用与 JUnit 4 不同的包中的注释。当您开始测试时,Eclipse 会自动为 JUnit 5 创建启动配置,但如果您的注释不在新包中,Eclipse 将找不到测试并将显示错误消息。
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
// ... other imports from org.junit.jupiter.api
旧注释:
import org.junit.Test;
// ... imports without "jupiter" in their name
您需要在 Maven 项目中添加以下依赖项才能使用 JUnit 5:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
解决方法:使用 JUnit 4。 您需要在启动配置中 select JUnit 4。否则会显示此错误。
从主菜单 select 运行 -> 运行 配置,然后 select 左侧导航栏中的 JUnit 测试配置。