HkFreaKuser1673718
HkFreaKuser1673718

Reputation: 747

Junit4 test case for Thread

Hi I am Trying to write Junit test case for this because i cant return anything here to catch and test assertrue(....) in my test case. SendMailTLS is a separate classs(without main) which sends mail to email ids fetch from mysql.Updates is a separate class which get some values from websites and stores them into the mysql.If i write normal by calling the run method without any asserttrue/assertequals the program keeps on running even if i comment Thread.sleep(1000 * 60 * 1);. In this case how would i test this?Any Suggestions would be appreciated.

public class Scheduler implements Runnable {

    public void run() {
        while(true)
        {
            SendMailTLS mail= new SendMailTLS();
            Updates Employee= new Updates();

            try {
                mail.Mail();
                Employee.update();              
                Thread.sleep(1000 * 60 * 1);
            } 
            catch{
            //catch code here

            }

            System.out.println("Scheduler Task Complete");
        }
    }

    public static void main(String args[]) {
            (new Thread(new Scheduler())).start();
        }
}

Upvotes: 0

Views: 527

Answers (1)

Kamil
Kamil

Reputation: 431

One of the thing you could test is if the content of DB saved by class Updates is correct after the method has run. I think it would be hard to test if email got send. I've got similar situation recently but the email sending class saved the content of the email in database so what I did was checking if mails data is saved properly in DB. Is your SendMailTLS leaves any information about its job? If so you could try testing that. The fact that your method runs indefinitely not necessary need to be a problem. Prepare some data in your test (in DB I assume), wait some time in test and than check if state of the system (for example data in DB) is correct. After the JUnit thread finishes so should separate thread (I think other threads get killed if JUnit thread is not running anymore).

Upvotes: 1

Related Questions