如何执行参数化 Junit 测试用例
How to perform Parameterized Junit Test Cases
我正在学习参数化 Junit 测试用例,我正在尝试通过 Id 获取用户。执行此操作时出现错误:
"Java.lang.IllegelArgumentException: Wrong number of arguments."
请告诉我这段代码有什么问题。
@RunWith(Parameterized.class)
public class UserTestCases {
@Mock
UserRepository userRepository;
@InjectMocks
private UserServiceImpl userService;
private User user;
private User expectedUser;
public UserTestCases(User user, User expectedUser) {
super();
this.user = user;
this.expectedUser = expectedUser;
}
@Parameters
public static Collection input() {
User u1=new User(1, "india", "shiva");
User u2=new User(2, "india", "shiva");
User u3=new User(3, "india", "shiva");
return Arrays.asList(new Object[][] {{new User(1L, "india", "shiva"), u1}, {new User(2, "india", "shiva"), u2},{new User(3, "india", "shiva"), u3}});
}
@Test
public void getUserByIdtest() {
when(userRepository.findOne(user.getId())).thenReturn(user);
User result=userService.findById(user.getId());
assertEquals(user.getCountry(), expectedUser.getCountry());
assertEquals(user.getName(), expectedUser.getName());
}
}
提前致谢
如果你正在学习使用JUnit,也许你可以试试最新版本:JUnit5。
最后一个版本可以更轻松地编写 外部功能,例如模拟、生成测试用例、加载Spring bean等等
此外,它作为参数化方法(测试)的内置支持,而 JUnit4 仅参数化 类(测试套件)并且需要 JUnitParams 及其 Runner
支持参数化测试。
你的测试在 JUnit5 中可以这样写:
@ExtendWith(MockitoExtension.class)
class UserTestCases {
@Mock UserRepository userRepository;
@InjectMocks UserServiceImpl userService;
@ParameterizedTest
@MethodSource("input")
void getUserByIdtest(User user, User expectedUser) {
when(userRepository.findOne(user.getId())).thenReturn(user);
User result = userService.findById(user.getId());
assertEquals(user.getCountry(), expectedUser.getCountry());
assertEquals(user.getName(), expectedUser.getName());
}
static Stream<Arguments> input() {
return Stream.of(
// static method of Arguments
arguments(
new User(1, "india", "shiva"),
new User(1L, "india", "shiva")),
arguments(
new User(2, "india", "shiva"),
new User(2L, "india", "shiva")),
arguments(
new User(3, "india", "shiva"),
new User(3L, "india", "shiva")),
);
}
}
然而,这个测试只是关于从UserServiceImpl
到UserRepository
的委托,所以你不需要在这里进行参数化测试。
如果你想检查授权(似乎没有映射),唯一需要的测试是这样的:
class UserTestCases {
@Mock UserRepository userRepository;
@InjectMocks UserServiceImpl userService;
@Test
void getUserByIdtest() {
User user = mock(User.class);
when(userRepository.findOne(any())).thenReturn(user);
assertEquals(userService.findById(1L), user);
}
}
这样的测试使用了 JUnit5 和 Mockito 库。
如果你使用 Maven,你的 POM 可以是这样的:
<properties>
<junit-jupiter.version>5.3.1</junit-jupiter.version>
<mockito.version>2.23.0</mockito.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</test>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
希望对您有所帮助!
我正在学习参数化 Junit 测试用例,我正在尝试通过 Id 获取用户。执行此操作时出现错误: "Java.lang.IllegelArgumentException: Wrong number of arguments." 请告诉我这段代码有什么问题。
@RunWith(Parameterized.class)
public class UserTestCases {
@Mock
UserRepository userRepository;
@InjectMocks
private UserServiceImpl userService;
private User user;
private User expectedUser;
public UserTestCases(User user, User expectedUser) {
super();
this.user = user;
this.expectedUser = expectedUser;
}
@Parameters
public static Collection input() {
User u1=new User(1, "india", "shiva");
User u2=new User(2, "india", "shiva");
User u3=new User(3, "india", "shiva");
return Arrays.asList(new Object[][] {{new User(1L, "india", "shiva"), u1}, {new User(2, "india", "shiva"), u2},{new User(3, "india", "shiva"), u3}});
}
@Test
public void getUserByIdtest() {
when(userRepository.findOne(user.getId())).thenReturn(user);
User result=userService.findById(user.getId());
assertEquals(user.getCountry(), expectedUser.getCountry());
assertEquals(user.getName(), expectedUser.getName());
}
}
提前致谢
如果你正在学习使用JUnit,也许你可以试试最新版本:JUnit5。
最后一个版本可以更轻松地编写 外部功能,例如模拟、生成测试用例、加载Spring bean等等
此外,它作为参数化方法(测试)的内置支持,而 JUnit4 仅参数化 类(测试套件)并且需要 JUnitParams 及其 Runner
支持参数化测试。
你的测试在 JUnit5 中可以这样写:
@ExtendWith(MockitoExtension.class)
class UserTestCases {
@Mock UserRepository userRepository;
@InjectMocks UserServiceImpl userService;
@ParameterizedTest
@MethodSource("input")
void getUserByIdtest(User user, User expectedUser) {
when(userRepository.findOne(user.getId())).thenReturn(user);
User result = userService.findById(user.getId());
assertEquals(user.getCountry(), expectedUser.getCountry());
assertEquals(user.getName(), expectedUser.getName());
}
static Stream<Arguments> input() {
return Stream.of(
// static method of Arguments
arguments(
new User(1, "india", "shiva"),
new User(1L, "india", "shiva")),
arguments(
new User(2, "india", "shiva"),
new User(2L, "india", "shiva")),
arguments(
new User(3, "india", "shiva"),
new User(3L, "india", "shiva")),
);
}
}
然而,这个测试只是关于从UserServiceImpl
到UserRepository
的委托,所以你不需要在这里进行参数化测试。
如果你想检查授权(似乎没有映射),唯一需要的测试是这样的:
class UserTestCases {
@Mock UserRepository userRepository;
@InjectMocks UserServiceImpl userService;
@Test
void getUserByIdtest() {
User user = mock(User.class);
when(userRepository.findOne(any())).thenReturn(user);
assertEquals(userService.findById(1L), user);
}
}
这样的测试使用了 JUnit5 和 Mockito 库。 如果你使用 Maven,你的 POM 可以是这样的:
<properties>
<junit-jupiter.version>5.3.1</junit-jupiter.version>
<mockito.version>2.23.0</mockito.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</test>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
希望对您有所帮助!