junit tests take different amounts of time when run multiple times on the same machine. Non junit, java apps take a constant time, why?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jyoti_s
Posted On:   Thursday, March 15, 2007 09:33 AM

Hi, We have tried a simple calculator sample Calculator.java, another test which tests the Calculator is CalculatorTest. java. In this we are trying to calculate the time taken to execute the test method. We observed varying times in msecs (2000, 2015, 2016). Can anybody tell what is the reason behind? Calculator.java package calculator; public class Calculator { private static int result; public void add(int n) { result = result + n; } } CalculatorTest. java package calculator; import calculator.Calculat or; import org.junit.Before; import org.junit.Test; import static org.junit.Assert. *; public   More>>

Hi,
We have tried a simple calculator sample Calculator.java, another test which tests the Calculator is CalculatorTest. java. In this we are trying to calculate the time taken to execute the test method. We observed varying times in msecs (2000, 2015, 2016). Can anybody tell what is the
reason behind?


Calculator.java

package calculator;

public class Calculator {
private static int result;

public void add(int n) {
result = result + n;
}
}

CalculatorTest. java

package calculator;
import calculator.Calculat or;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert. *;
public class CalculatorTest
{
private static Calculator calculator =
new Calculator() ;

@Before
public void clearCalculator( ) {
calculator.clear( );
}

@Test
public void add() throws InterruptedExceptio n {
long token = System.currentTimeM illis();
Thread.sleep( 2000);
calculator.add( 1);
calculator.add( 1);
assertEquals( calculator. getResult( ), 2);
System.out.println( " Time taken to execute:" +
(System.currentTime Millis()- token)) ;
}
}



On the other hand if we have tried another sample like this..

class simple{
public static void main(String args[]) throws InterruptedExceptio n {
long token = System.currentTimeM illis();
Thread.sleep( 2000);
System.out.println( " time taken:" + (System.currentTime Millis() - token));

}
}

It always gives me constant value. Why in this case we get the constant value and we get different results only when we use JUnit?


Any help will be higly appreciated.

regards,
Jyoti

   <<Less
About | Sitemap | Contact