Given, when, then约定和异常处理。使用 Mockito 和 JUnit
Given, when, then convention together with exception handling. With Mockito and JUnit
将测试用例分成 3 个部分是一个很好的做法:Given、When、Then。
但是在 JUnit 中处理异常的常用方法是使用 ExpectedException @Rule。
问题是 ExpectedException::expect() 必须在 //when 部分之前声明。
public class UsersServiceTest {
// Mocks omitted
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void signUp_shouldCheckIfUserExistsBeforeSign() throws ServiceException {
// given - its ok
User user = new User();
user.setEmail(EMAIL);
when(usersRepository.exists(EMAIL)).thenReturn(Boolean.TRUE);
// then ???
thrown.expect(UserAlreadyExistsServiceException.class);
// when???
usersService.signUp(user);
}
}
有谁知道一些好的约定或库可以更好地处理测试中的异常?
您可以在测试注释中定义您期望的异常类型:
@Test(expected=NullPointerException.class)
public void npeThrownWhenFooIsNull() {
String foo = null;
foo.contains("nothing");
}
或者,如果您想检查抛出的异常的详细信息:
@Test
public void npeIsThrownAndContainsNullMessageWhenFooIsNull() {
String foo = null;
try {
foo.contains("nothing");
fail();
} catch (NullPointerException npe) {
assertNull(npe.getMessage());
}
}
我发现这是测试异常处理的更好方法
首先,我认为您的测试还可以,即使有些测试没有完全遵循 given/when/then 顺序,但如果您想标准化测试组织以提高可读性,这对您来说是个好消息。
如 this Whosebug page 中所述,在 JUnit 中有许多有效的方法可以预期异常。我认为似乎适合 given/when/then 组织的是:
@Test
public void signUp_shouldCheckIfUserExistsBeforeSign() throws ServiceException {
// GIVEN
User user = new User();
user.setEmail(EMAIL);
when(usersRepository.exists(EMAIL)).thenReturn(Boolean.TRUE);
// WHEN
try {
usersService.signUp(user);
// THEN
// expecting exception - should jump to catch block, skipping the line below:
Assert.fail("Should have thrown UserAlreadyExistsServiceException");
}catch(UserAlreadyExistsServiceException e) {
// expected exception, so no failure
}
// other post-sign-up validation here
}
将测试用例分成 3 个部分是一个很好的做法:Given、When、Then。
但是在 JUnit 中处理异常的常用方法是使用 ExpectedException @Rule。
问题是 ExpectedException::expect() 必须在 //when 部分之前声明。
public class UsersServiceTest {
// Mocks omitted
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void signUp_shouldCheckIfUserExistsBeforeSign() throws ServiceException {
// given - its ok
User user = new User();
user.setEmail(EMAIL);
when(usersRepository.exists(EMAIL)).thenReturn(Boolean.TRUE);
// then ???
thrown.expect(UserAlreadyExistsServiceException.class);
// when???
usersService.signUp(user);
}
}
有谁知道一些好的约定或库可以更好地处理测试中的异常?
您可以在测试注释中定义您期望的异常类型:
@Test(expected=NullPointerException.class)
public void npeThrownWhenFooIsNull() {
String foo = null;
foo.contains("nothing");
}
或者,如果您想检查抛出的异常的详细信息:
@Test
public void npeIsThrownAndContainsNullMessageWhenFooIsNull() {
String foo = null;
try {
foo.contains("nothing");
fail();
} catch (NullPointerException npe) {
assertNull(npe.getMessage());
}
}
我发现这是测试异常处理的更好方法
首先,我认为您的测试还可以,即使有些测试没有完全遵循 given/when/then 顺序,但如果您想标准化测试组织以提高可读性,这对您来说是个好消息。
如 this Whosebug page 中所述,在 JUnit 中有许多有效的方法可以预期异常。我认为似乎适合 given/when/then 组织的是:
@Test
public void signUp_shouldCheckIfUserExistsBeforeSign() throws ServiceException {
// GIVEN
User user = new User();
user.setEmail(EMAIL);
when(usersRepository.exists(EMAIL)).thenReturn(Boolean.TRUE);
// WHEN
try {
usersService.signUp(user);
// THEN
// expecting exception - should jump to catch block, skipping the line below:
Assert.fail("Should have thrown UserAlreadyExistsServiceException");
}catch(UserAlreadyExistsServiceException e) {
// expected exception, so no failure
}
// other post-sign-up validation here
}