Java Mockito 抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22172538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito throw Exception
提问by user2375298
@Test(expectedExceptions=DataAccessException.class)
public void testUpdateSubModuleOrderDateExceptionCheck() {
//some code to initialize//
UserSubModuleDao userSubModuleDao = mock(UserSubModuleDao.class);
userModuleServiceImpl.setUserSubModuleDao(userSubModuleDao);
UserSubModule userSubModule=new UserSubModule();
UserSubModuleId userSubModuleId=new UserSubModuleId();
when(userSubModuleDao.findById(any(UserSubModuleId.class),eq(false))).thenThrow(DataAccessException.class);
userModuleServiceImpl.updateSubModuleOrder(data, moduleSysId, userId);
I want to throw the Db exception for code coverage . its working if i give expected exception as : Exception.class
but not for DataAccessException.class
我想为代码覆盖率抛出 Db 异常。如果我将预期的异常作为:Exception.class
但不是为了DataAccessException.class
My method in original class is as following:
我在原始类中的方法如下:
public void updateSubModuleOrder(Long[] data, Long moduleSysId, Long userId) {
try {
for (int i = 0; i < data.length; i++) {
SubModule subModule=new SubModule();
subModule.setSubModuleId(data[i]);
UserSubModuleId userSubModuleId = new UserSubModuleId();
userSubModuleId.setSubModuleId(subModule);
userSubModuleId.setUserId(userId);
userSubModuleId.setUserModuleId(moduleSysId);
UserSubModule userSubmodule = new UserSubModule();
userSubmodule = userSubModuleDao.findById(userSubModuleId,
false);
catch (DataAccessException ewmsDataExp) {
LOGGER.error(
"Database Exception while updateSubModuleOrder because of {}",
ewmsDataExp.getMessage());
throw new EWMSServiceException(
"Database Exception while updateSubModuleOrder"
+ ewmsDataExp.getMessage());
} catch (Exception exp) {
LOGGER.error(
"System Exception while updateSubModuleOrder because of {}",
exp.getMessage());
throw new EWMSServiceException(
"Database Exception while updateSubModuleOrder"
+ exp.getMessage());
}*
i get the error
我得到了错误
FAILED: testUpdateSubModuleOrderDateExceptionCheck
org.testng.TestException:
**Expected exception org.springframework.dao.DataAccessException but got org.testng.TestException:**
**Expected exception org.springframework.dao.DataAccessException but got java.lang.InstantiationError: org.springframework.dao.DataAccessException**
at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
and some error lines....
和一些错误行。...
Default test
Tests run: 1, Failures: 1, Skips: 0
测试运行:1,失败:1,跳过:0
===============================================
================================================
===============================================
================================================
回答by Jen S.
Change this:
改变这个:
thenThrow(DataAccessException.class)
to
到
thenThrow(new DataAccessException("..."){ })
Example:
例子:
when(userSubModuleDao.findById(any(UserSubModuleId.class),eq(false))).thenThrow(new DataAccessException("..."){});
You can only pass a Class reference when that Exception type has a No-Arg constructor, and the Spring exception does not have one.
您只能在该异常类型具有 No-Arg 构造函数时传递 Class 引用,而 Spring 异常没有。
回答by Shyam_coder
Try
尝试
Mockito.doThrow(new Exception()).when(mockedObject).methodName(...);
回答by Seamus
As stated by Jen S:
正如 Jen S 所说:
You can only pass a Class reference when that Exception type has a No-Arg constructor, and the Spring exception does not have one.
您只能在该异常类型具有 No-Arg 构造函数时传递 Class 引用,而 Spring 异常没有。
My solution was using Mockito:
我的解决方案是使用 Mockito:
Mockito.when(mockedObject.method(Mockito.anyString())).thenThrow(Mockito.mock(DataAccessException.class));