Friday, 15 August 2014

java - Strange behavior when testing multi-threading in junit -


I collided with a problem that caused me to get stumped for some time. I am sure this is something about Junty which I do not quite understand about.

Let's take a very simple class:

  package package. Import java.util.logging.Level; Import java.util.logging.Logger; Public square sleeper {@Override Public Zero Run ()) {try {thread} sleep (5000l) attempts; System.out.println ("snoozed"); } Hold (Prerupted East) East) {Logger.getLogger (Sleeper Class .getName ()). Log (level .spee, blank, east); }}}  

Write a very simple test for thread

  package. Import org.junit.Test; Public Class Sleeperast {@Test Public Zero Test Snooze ()) throws an exception (new thread (new sleeper). Start ();} Public static zero main (string ... args) {new thread (new sleeper) ). Start ();}}  

You will see that I have included a "main" method inside the test class when you invite Junit to execute "testSnooze" , Then the test immediately goes out without thread.

If If you call the main method, the thread runs fine;

> <>> To the north of Thomastets, I would suggest to consider testing the method without calling Start () of the thread. .

The purpose of testing the code written by us, We should not test the code which we have not written - hence for unit testing, run () The method should be directly enough.

However, we should check that the code we have written works with the code that we did not write . As simple as the thread in the case, I think it will be included in the acceptance test (black box test at the entry point on the system), there is no real way to write the integration test.


No comments:

Post a Comment