预期的异常规则和模拟静态方法– JUnit

本文介绍了在实施RESTful服务时遵循TDD规则,如何使用JUnit测试预期的EmployeeServiceException异常并验证错误消息。同时展示了如何使用PowerMockito模拟静态方法进行测试,包括设置输入参数、验证静态方法调用以及断言测试结果。
摘要由CSDN通过智能技术生成

今天,我被要求使用RESTful服务,所以我开始遵循Robert Cecil Martin的TDD规则实施该服务,并遇到了一种测试预期异常以及错误消息的新方法(对我来说至少是这样),因此考虑共享我的实现方式作为这篇文章的一部分。

首先,让我们编写一个@Test并指定规则,我们的代码将为我们的示例抛出特定的异常,即EmployeeServiceException ,我们将使用ExpectedException对其进行验证,这将为我们提供有关预期抛出的异常的更精确信息,并具有验证的能力错误消息,如下所示:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithStaticMethod.class)
public class EmployeeServiceImplTest {

	@InjectMocks
	private EmployeeServiceImpl employeeServiceImpl;

	@Rule
	public ExpectedException expectedException = ExpectedException.none();

	@Before
	public void setupMock() {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void addEmployeeForNull() throws EmployeeServiceException {
		expectedException.expect(EmployeeServiceException.class);
		expectedException.expectMessage("Invalid Request");
		employeeServiceImpl.addEmployee(null);
	}

}

现在,我们将为@Test创建一个实现类,该类将在请求为null时抛出EmployeeServiceException ,对我来说,它是EmployeeServiceImpl ,如下所示:

EmployeeServiceImpl.java

public class EmployeeServiceImpl implements IEmployeeService {

	@Override
	public String addEmployee(final Request request)
			throws EmployeeServiceException {
		if (request == null) {
			throw new EmployeeServiceException("Invalid Request");
		}
		return null;
	}
}

下一步,我们将写一个@Test,我们将使用嘲笑其接受输入参数,返回类型的静态方法PowerMockito.mockStatic() ,验证它使用PowerMockito.verifyStatic(),最后做一个断言来记录测试通过或失败状态,如下:

@Test
	public void addEmployee() throws EmployeeServiceException {
		PowerMockito.mockStatic(ClassWithStaticMethod.class);
		PowerMockito.when(ClassWithStaticMethod.getDetails(anyString()))
				.thenAnswer(new Answer<String>() {
					@Override
					public String answer(InvocationOnMock invocation)
							throws Throwable {
						Object[] args = invocation.getArguments();
						return (String) args[0];
					}
				});
		final String response = employeeServiceImpl.addEmployee(new Request(
				"Arpit"));
		PowerMockito.verifyStatic();
		assertThat(response, is("Arpit"));
	}

现在,我们将在EmployeeServiceImpl自身中提供@Test的实现。 为此,让我们修改EmployeeServiceImpl使其具有静态方法调用,作为addEmployee的else语句的一部分 ,如下所示:

public class EmployeeServiceImpl implements IEmployeeService {

	@Override
	public String addEmployee(final Request request)
			throws EmployeeServiceException {
		if (request == null) {
			throw new EmployeeServiceException("Invalid Request");
		} else {
			return ClassWithStaticMethod.getDetails(request.getName());
		}
	}
}

其中getDetailsClassWithStaticMethod内部的静态方法:

public class ClassWithStaticMethod {

	public static String getDetails(String name) {
		return name;
	}
}

完整的源代码托管在github上

翻译自: https://www.javacodegeeks.com/2017/01/expected-exception-rule-mocking-static-methods-junit.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值