简体   繁体   English

线程的Junit4测试用例

[英]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. 嗨,我正在尝试为此编写Junit测试用例,因为我无法在此处返回任何内容以在我的测试用例中捕获和测试assertrue(....)。 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); SendMailTLS是一个单独的类(没有主类),它将类发送邮件到从mysql获取的电子邮件id.Updates是一个单独的类,它从网站获取一些值并将它们存储到mysql中。如果我通过调用run方法编写正常而没有任何asserttrue /断言相等,即使我注释Thread.sleep(1000 * 60 * 1);该程序也可以继续运行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();
        }
}

One of the thing you could test is if the content of DB saved by class Updates is correct after the method has run. 您可以测试的一件事是在方法运行后,类Updates保存的数据库内容是否正确。 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. 我最近也遇到类似的情况,但是电子邮件发送类将电子邮件的内容保存在数据库中,所以我所做的是检查邮件数据是否正确保存在DB中。 Is your SendMailTLS leaves any information about its job? 您的SendMailTLS是否留下有关其工作的任何信息? 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). 在JUnit线程完成后,也应该分离线程(如果JUnit线程不再运行,我认为其他线程将被杀死)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM