有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java模拟构造函数以查看它是否引发异常

Java 8

我想用Mockito测试以下构造函数

如果为存储库传递了null,我希望测试抛出一个异常

public PresenterImp(@Nonnull IRepository repository, IScheduler scheduler) {
        super(schedulerFactory);
        this.repository = Preconditions.checkNotNull(repository);
 }

最好的方法是什么?当我的演讲者不能用的时候

在我的设置中,我执行以下操作:

@Before
public void setup() throws Exception {
             
    repository = Mockito.mock(IRepository.class);
                    
    iScheduler = Mockito.mock(IScheduler.class);
    viewContract = Mockito.mock(ViewContract.class);
                    
    presenter = new PresenterImp(repository, iScheduler);
    
              optInNotificationPresenter.attachView(viewContract);  
}

非常感谢您的建议


共 (1) 个答案

  1. # 1 楼答案

    你可以这样做:

     @Test(expected=NullPointerException.class )
     public void testNullCheck() throws Exception
               new PresenterImp(null, mock);   
    
        }