0
public class SomeclassTobeTested {

    public int doSomethingAndSendMail(){
        //... doing something
        SomeStaticClass.sendEmail(args);
    }
}

Public static SomeStaticClass implements Runnable{

    public void run(){
        sendMail();
    }

    public static void senMail(args){

     //starts the thread

    }
}

public class TestSomeClassToBeTested{

}

So now I have to verify whether sendEmail was called from doSomethingAndSendMail()? Which class should I mock? And not actually send the email.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
user1364861
  • 301
  • 1
  • 2
  • 16

1 Answers1

1

You cannot verify static methods with Mockito . You have to use Powermockito to do that.

However, I suggest you refactor your code so the sendEmail method is a member of an object, perhaps of a dependency. This dependency can then be mocked and the sendEmail method can be stubbed and verified with Mockito. Having to use Powermockito usually means bad practice.

More information can be found in these answers: Mocking static methods with Mockito and Why is using static helper methods in Java bad?

Community
  • 1
  • 1
Tom Verelst
  • 15,324
  • 2
  • 30
  • 40
  • How would you test it if the class under test used an email sending API such as Apache Commons EMail? Such APIs are object-oriented (no statics, with true objects), therefore not usually regarded as "bad practice"; yet, they can't be mocked with plain Mockito. – Rogério May 27 '15 at 21:54