0

I am new to writing test cases. I need to test a part of a function that does not return anything. It should be able to validate containsHello is true or 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()

Tried with powerMockito which throws this error:

java.lang.IllegalStateException: Failed to transform class with name com.amazon.alfred.cli.action.DeployBundleTests. Reason: javassist.bytecode.InterfaceMethodrefInfo cannot be cast to javassist.bytecode.MethodrefInfo

@Test
public void TestingmyFunction() throws Exception {

String x=“hello"

PowerMockito.mockStatic(Secondclass.class)
PowerMockito.when(Secondclass.isSentence()).thenReturn(true);
PowerMockito.mockStatic(AppConfig.class);
PowerMockito.when(AppConfig.isWord()).thenReturn(true);


Myfunction(x);
}

raosa
  • 119
  • 1
  • 8

0 Answers0