嘲笑记录仪时的junit失败



我要测试的类是静态记录器的HostApi

public class HostApi {
     String baseUrl;
     private static Logger logger=LogFactory.getLogger(HostApi.class);
     /**
      * Constructor
      * 
      * @param baseUrl
      *            - protocol + "://" + dockerIp + ":" + dockerPort Example -
      *            http://192.168.99.100:2375
      */
     public HostApi(String baseUrl) {
      this.baseUrl = baseUrl;
     }
     HostRestClient client;
     public Container getContainerInfo(String containerId) throws IOException, AgentException {
       logger.debug("############# getContainerInfo start ###################"); //$NON-NLS-1$
        String output;
        String path = "/containers/" + containerId + "/json"; //$NON-NLS-1$ //$NON-NLS-2$
        client = new HostRestClient();
        output = client.processGetRequest(baseUrl + path);
        logger.trace(output);
        ObjectMapper mapper = new ObjectMapper();
        Container container = mapper.readValue(output, Container.class);
        logger.debug("############# getContainerInfo end ###################nn"); //$NON-NLS-1$
        return container;
     }
}

我的Junit测试课是HostApiTest

@RunWith(PowerMockRunner.class)
@PrepareForTest({HostApi.class,ObjectMapper.class,LogFactory.class})
public class HostApiTest {
     HostApi hp;
     static Logger logger;
     @BeforeClass
     public static void before()
     {
         System.out.println("Before Class");
     }
     @AfterClass
     public static void after() {
         System.out.println("After Class");
     }
     @Mock
     Logger loggermock;
     @Before()
     public void setUp() { 
     mockStatic(LogFactory.class);
         EasyMock.expect(LogFactory.getLogger(HostApi.class)).andReturn(loggermock    );
     //logger=createMock(Logger.class);
     // Whitebox.setInternalState(HostApi.class, logger);
       hp=new HostApi("skj"); //$NON-NLS-1$
     }
     @Test
     public void testgetContainerInfo() throws Exception{
       System.out.println("abc");
       HostRestClient client=PowerMock.createMock(HostRestClient.class);
       ObjectMapper obj=PowerMock.createMock(ObjectMapper.class);
       Container container=new Container();
       container.setId("234");
       String containerData=container.toString();    
       PowerMock.expectNew(ObjectMapper.class).andReturn(obj); 
       PowerMock.expectNew(HostRestClient.class).andReturn(client);
      EasyMock.expect(client.processGetRequest(EasyMock.isA(String.class))).andReturn(containerData);         
     EasyMock.expect(obj.readValue(EasyMock.isA(String.class),EasyMock.same(Container.class))).andReturn(container);  
         replayAll();    
      assertEquals("234",hp.getContainerInfo("25").getId());  
         EasyMock.verify();
     }
}

在代码中没有logger(即在HostApi中发表评论),但是添加logger后,它会引发断言错误

我添加了logFactory的静态模拟,但是它似乎不起作用。

我在嘲笑什么做错了?我只能使用PowerMock。

删除...

  • LogFactory.class来自@PrepareForTest({...})注释
  • @Mock Logger loggermock;的CC_8
  • HostApiTest的CC_11
  • EasyMock.expect(LogFactory.getLogger(HostApi.class)).andReturn(loggermock);来自HostApiTest

您的测试案例对HostApi的记录器的行为没有期望或断言,因此无需模拟它。

相关内容

  • 没有找到相关文章

最新更新