模拟Logger时JUnit失败

时间:2017-09-27 12:14:50

标签: unit-testing logging junit mockito powermock

我要测试的课程是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 ###################\n\n"); //$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。

1 个答案:

答案 0 :(得分:0)

删除...

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

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