美文网首页程序员
java 单元测试方法小结

java 单元测试方法小结

作者: 不惧未来 | 来源:发表于2019-03-14 16:32 被阅读0次

    基本框架

    被测类

    package com.nokia.imp.domainhandler.AtlHandler;
    
    public class AtlHandler extends Handler {
     private AtlHandler atlHandler;
     private AttachmentMaker attachmentMaker;
    
      public AtlHandler(Context context) {
        super(context);
        attachmentMaker = new AttachmentMaker();
      }
    
      @Override
      public void upload(GetRequest getRequest) {
      }
    }
    

    测试类

    package com.nokia.imp.domainhandler.atlHandler;
    
    public class AtlHandlerTest{
      private static final String VIRTUAL_AGENT_ID1 = "mrbts-1";
      private AttachmentMaker attachmentMakerMock;
    
      @Before
      public void setUp() throws Exception {
        attachmentMakerMock = mock(AttachmentMaker .class);
        atlHandler = new(...);
      }
    
      @After
        public void cleanup() {
      }
    
      @Test
      public void should_doSomeThing_when_givenCondition() {
        // given
    
        // when
    
        // then
      }
    
    }
    

    Q1:用 Whitebox 改变 private 成员的值

    Whitebox.setInternalState(AtlHandler, "auditTrailLogUploadTasks", auditTrailLogUploadTasks);
    

    表示将AtlHandler类中名字为 “auditTrailLogUploadTasks” 的变量赋值为 auditTrailLogUploadTasks;

    现有这样一个例子:
    在被测类 AtlHandler 中, 有一个SubscribedObjectsChangedManager 类型的 private 成员,这个成员在构造函数中被初始化。现在需要改变的是SubscribedObjectsChangedManager这个类的private 成员 Service,可以这么做:

    Object manager = Whitebox.getInternalState(troubleShootHandler, "subscribedObjectsChangedManager");
    Whitebox.setInternalState(manager, "service", service);
    

    表示先将 AtlHandler 类中名为 “subscribedObjectsChangedManager” 的private成员取出来,取名为manager,再将manager这个成员类里面名为“service”的成员取出来,赋上我们构建的 service;

    service可以是真实对象,也可以是mock或者spy的对象。mock的对象无法调用到真实的方法。而spy出来的类是真实的,并且可以使里面的方法返回你想要的值:

    Service service = spy(new Service());
    Whitebox.setInternalState(objectsChangedManager, "service", service);
    doReturn("ute").when(service).getName(anyString());
    

    当service调用getName方法时返回“ute”字符串;

    Q2:如何mock被测类中new出来的类

    如果 new 出来的类是在构造函数中赋给了被测类中的 private 变量,则可以参考Q1的方法;
    如果在代码中使用到了 new 方法,那就只能采用 PowerMock;
    首先要在测试类前面加上如下note:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({FeedbackMessageMaker.class, AtlHandler.class})
    public class AtlHandlerTest {
    }
    
    private AttachmentMaker attachmentMaker;
    attachmentMaker = mock(AttachmentMaker.class);
    whenNew(AttachmentMaker.class).withAnyArguments().thenReturn(attachmentMaker);
    when(attachmentMaker.asFeedbackAttachment().thenReturn(attachment));
    verify(attachmentMaker, times(1)).asFeedbackAttachment();
    

    Q3:如何在测试中获取局部变量的值?

    如需要验证 StatusReport 的内容,我们可以这样做:

    ArgumentCaptor<StatusReport> reportCaptor = ArgumentCaptor.forClass(StatusReport.class);
    verify(statusReportSender,times(1)).accept(reportCaptor.capture());
    verify(orchestratorMock).updateNeInfomodel(updateCaptor.capture());
    StatusReport report = reportCaptor.getValue();
    assertTrue(report.isCompleted());
    assertEquals(virtualAgentId, report.getVirtualAgentId());
    

    Q4: 如何mock static?

    PowerMockito.mockStatic(BufferManager.class); 
    

    Q5: 如何测试代码中抛出来的exception

    @Test(expected = CmConfigException.class)
    

    Q6:验证测试结果

    assertEquals(TroubleShootTestUtils.OPERATIONID_1, statusReport.getOperationId());
    assertTrue(statusReport.getContent().contains(TroubleShootErrorText.OPERATION_CONFLICT));
    verify(handlerExecutorMock).submit(Matchers.isA(TroubleShootTask.class));
    verify(provisionRequestManager, times(1)).onReceiveProvisionRequest(eq(setRequest));
    

    Q7:异步代码测试

    public class SnapshotTransferToLssTask extends SftpTask implements ElementTask {}
    public class SnapshotTransferInfoUpdateTask implements BiConsumer<Object, Throwable> {}
    ...
    SnapshotTransferToLssTask task = new SnapshotTransferToLssTask(neId, lssConfig, priKeyPath, zipFilePath, service);
    CompletableFuture transfer = handler.getExecutor().submit(task);
    transfer.whenCompleteAsync(new SnapshotTransferInfoUpdateTask(service));
    

    在这里,异步体现在只有当task里的事情做完了之后才创建 SnapshotTransferInfoUpdateTask, 再执行里面的任务。

    CountDownLatch countDown = new CountDownLatch(1);
    doAnswer(releaseLatch(countDown)).when(service).stopOperation(any());
    troubleShootHandler.onObjectsChanged(genericInfomodelUpdate);
    countDown.await(2, TimeUnit.SECONDS);
    

    只有当stopOperation这个方法被调用到才会退出case,否则就等待,等待时间最长2秒;

    // given
    when(snapshotHandler.getSnapshotUploadExecutor()).thenReturn(handlerExecutorMock);   when(handlerExecutorMock.submit(any())).thenReturn(CompletableFuture.completedFuture(""));
     // when
    snapshotCollectionTask.get();
     // then
    verify(handlerExecutorMock).submit(Matchers.isA(SnapshotTransferToLssTask.class));
    

    只能验证到 submit函数传进去的参数是 SnapshotTransferToLssTask.class;

    private Answer<Void> releaseLatch(CountDownLatch latch) {
      return invocation -> {
        latch.countDown();
        return null;
      };
    }  
    

    Q8:创建路径

    Files.createDirectories(Paths.get("../bin/check_trace_port.sh"));
    Files.deleteIfExists(Paths.get("../bin/check_trace_port.sh"));
    

    相关文章

      网友评论

        本文标题:java 单元测试方法小结

        本文链接:https://www.haomeiwen.com/subject/kzpruqtx.html