Java junit测试时需要自定义异常而不是空指针异常

Java junit测试时需要自定义异常而不是空指针异常,java,unit-testing,exception,nullpointerexception,mockito,Java,Unit Testing,Exception,Nullpointerexception,Mockito,我在java类中有一个方法: @Context UriInfo uriInfo; public void processRequest(@QueryParam ("userId") @DefaultValue("") String userId) { String baseURI = uriInfo.getBaseUri().toString(); if(userId == null) { //UserIdNotFoundException is

我在java类中有一个方法:

@Context
UriInfo uriInfo;
public void processRequest(@QueryParam ("userId") @DefaultValue("") String userId)
{
     String baseURI = uriInfo.getBaseUri().toString();
     if(userId == null)
     {
         //UserIdNotFoundException is my custom exception which extends Exceptition
         throw new UserIdNotFoundException();
     }
 }
当我在junit测试上面的方法时,当userId参数为Null时,我期望UserIdNotFoundException出现以下断言错误:
期望UserIdNotFoundException的实例,但它是java.lang.NullPointerException

@Test
public void testProcessRequest_throws_UserIdNotFoundException()
{
     expectedException.expect(UserIdNotFoundException.class);
     processRequest(null);
}
我的自定义异常类:

public class UserIdNotFoundException extends Exception
{

     public UserIdNotFoundException()
     {

     }

     public UserIdNotFoundException(String message)
     {
          super(message);
     }
}

您必须编写自定义异常类,这可能会对您有所帮助

示例代码:

class UserIdNotFoundException extends Exception{  
 UserIdNotFoundException  (String s){  
  super(s);  
 }  
}  
 public void processRequest(String userId)
{
     if(userId == null)
     {
         //UserIdNotFoundException is my custom exception which extends Exception
         throw new UserIdNotFoundException("SOME MESSAGE");
     }
 } 
测试异常:

class UserIdNotFoundException extends Exception{  
 UserIdNotFoundException  (String s){  
  super(s);  
 }  
}  
 public void processRequest(String userId)
{
     if(userId == null)
     {
         //UserIdNotFoundException is my custom exception which extends Exception
         throw new UserIdNotFoundException("SOME MESSAGE");
     }
 } 

从异常类中删除默认构造函数,JVM会隐式地为您创建它/

您可能没有使用值设置
uriInfo
,并且您正在对空值调用方法。您确定要将测试设置为给
uriInfo
赋值吗?或者
getBaseUri()
可能返回
null
,对其调用
toString()
可能会抛出
NullPointerException
。这可以通过在调试器中检查
getBaseUri()
的返回值来完成

通常,您可以使用带有bean的配置来运行测试,也可以添加setter来设置测试类中的值以模拟它,或者在测试中给出一个值。这将有助于避免出现
NullPointerException


无论哪种方式,在方法中执行任何实际工作之前,您都应该始终执行失败验证。

我更喜欢注释:

@Test(expected = UserIdNotFoundException.class)
public void testProcessRequest_throws_UserIdNotFoundException() {
     processRequest(null);
}
问题可能是您的
processRequest
实现可能在您有机会检查用户id之前命中了NPE

这是一件好事:您的测试表明实现不满足您的需求。你现在可以永远修理它了


这就是TDD的好处。

我已经编写了自定义异常类,但仍然遇到相同的断言错误。更新答案,试试看。删除构造函数将如何解决上述问题?我没有告诉它将解决问题,但它不是必需的。这不是一个如何扩展异常的好例子。只有一个构造函数;我建议重写所有四个。在检查之前,我如何才能使我的实现遇到空指针异常。我没有对userId调用任何字符串方法。或者我不明白你的意思。另外,我将userId作为Url中的查询参数。唯一可能的是,它可能涉及一些空检查。您必须发布processRequest的实现,让我为您详细说明。一个更好、更具教育意义的想法可能是在调试器中逐步完成该过程。在processRequest的第一行放一个断点,看看观察到的行为与您的期望不符。我按照您的建议进行了调试。错误显示在行uriInfo.getBaseUri().toString()上,因此我将该行放在if检查之后。现在测试显示绿色条。但我仍然不明白原因。我仍然不明白为什么在使用uriInfoYou时它会击中NPE。你仍然没有发布实现,所以我帮不了你。你应该能弄明白这一点。