jeudi 14 janvier 2021

Mockito testing a function that throws log error

I am new to writing test cases. I need to test a part of a function that does not return anything. Could check if a log error is obtained or not

public void Myfunction(x){
Boolean containsHello;

if (x != null) {

    if ((x.contains(“hello"))
      {
        containsHello = true;
        
        if (AppConfig.isWord()
                && (Secondclass.isSentence() && containsHello)) {
            log.error("");
        }
    }
}

More code
}

Secondclass is a final class defined outside of the current .java file AppConfig is also a class that exists outside the java file being tested.

How to run a test for the above code using Mockito. I tried the below code But does not work:

@Test
public void TestingmyFunction() throws Exception {

String x=“hello"
Mockito.when(Secondclass.isSentence()).thenReturn(true);

Mockito.when(AppConfig.isWord()).thenReturn(true);


Myfunction(x);
}

But I am not able to access the AppConfig.isWord() or Secondclass.isSentence()

Aucun commentaire:

Enregistrer un commentaire