Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试使用spring服务的方法_Java_Spring_Junit_Junit4_Qa - Fatal编程技术网

Java 测试使用spring服务的方法

Java 测试使用spring服务的方法,java,spring,junit,junit4,qa,Java,Spring,Junit,Junit4,Qa,我在Java中测试一个方法时遇到问题(我使用jUnit 4),该方法用于测试调用存储库并应引发异常。如果出现问题,该方法如下所示: //Method located in a Spring service and use a repository to make the query public void updateCustomer(CustomerEntity customer) throws CustomException { try { getDa

我在Java中测试一个方法时遇到问题(我使用jUnit 4),该方法用于测试调用存储库并应引发异常。如果出现问题,该方法如下所示:

//Method located in a Spring service and use a repository to make the query
public void updateCustomer(CustomerEntity customer) throws CustomException {
        try {
            getDataDao().update(customer);
        } catch (Exception e) {
            throw new CustomException(e.getMessage());
        }
    }
问题是,如何让测试用例进入catch块并抛出异常?我正在将客户设置为null,但不起作用


谢谢您的帮助。

您需要模拟DAO调用并从那里抛出和异常

您将对@Mock、@InjectMock和framework有更多的了解


您可以使用mockito框架来实现这一点。

@Ed好极了。。!!
  Mockito.when(getDataDao().update(any()))
      .thenThrow(Exception.class);