我遇到Junit5测试覆盖率问题

nr7wwzry  于 2022-12-18  发布在  其他
关注(0)|答案(1)|浏览(264)

我想为我的代码编写junit 5测试用例,但无法覆盖此方法中的第5-7行,请有人帮助我吗

1. public class StudnetIno{ 
2. public void log(Student student) throws JsonProcessingException
3.  {
4.    if(Loggin.isDebugEnabled(this) && Utility.isProduction()){
5.    ObjectMapper map = new ObjectMapper();
6.    map.getFactory().setCharacterEscapes(new HTMLCharacterEscapes());
7.    Loggin.debugMessage(this,map.writerWithDefaultPrettyPrinter().writeValueAsString(student));
8.     }
9.  }

***注意:***isDebugEnable()和isProduction()方法是公共静态方法

@Test
void testLog() throws JsonProcessingException{
  StudnetIno studnetIno = new StudnetIno();
  studnetIno.log(new Student());
  ObjectMapper map = new ObjectMapper();
  assertEquals("", map.writerWithDefaultPrettyPrinter().writeValueAsString(student))

}

这就是我的测试用例

wmvff8tz

wmvff8tz1#

您需要模拟静态方法isDebugEnabled()isProduction

@RunWith(PowerMockRunner.class)
@PrepareForTest(StudentInfo.class)
public class StudentInfoTest {
    @Test
    void testLog() throws JsonProcessingException{

        mockStatic(Loggin.class);
        mockStatic(Utility.class);
        when(Loggin.isDebugEnable(any())).thenReturn(true);
        when(Utility.isProduction()).thenReturn(true);

        ....
    }
}

相关问题