Before I wrote an algorithm for calculating the Fibonacci sequence, I was curious to calculate how long it would take to add so many additions, and today I tested it with JUnit.
The test code is given below
The test data is the remainder of the 5000w Fibonacci and 10007
When writing test methods, it is often found that some methods need to create the same object before execution
Using @before annotations A public void method causes the method to execute before the @test annotation method is executed (then you can create the same object in the method)
The @before annotation method of the parent class executes before the @before annotation method of the subclass executes
If additional resources are allocated in the @before annotation method, the allocated resources need to be freed after the test is executed.
Using @after annotations A public void method causes the method to be executed after the @test annotation method executes
Even if an exception is thrown in the @before annotation method, @Test annotation method, all @after annotation methods will still be executed
The @after annotation method in the parent class is executed after the child class @after annotation method executes
The public void method for @Test annotations will be treated as a test case
JUnit creates a new test instance each time and then calls the @test annotation method
Any exception thrown will be considered a test failure
@Test annotations provide 2 parameters:
1, "expected", defines the exception that the test method should throw, and if the test method does not throw an exception or throws a different exception, the test fails
2, "Timeout", if the test runs longer than the defined time, the test fails (in milliseconds)
PackagePOJ;Importorg.junit.*; Public classTestrunner {intN; @Before Public voidSetUp ()throwsException {n=50000000; } @After Public voidTearDown ()throwsException {n=0; } @Test (Timeout= 300000) Public voidTest () {NewFibonacci (n); }}
Code for the algorithm:
Public classFibonacci { PublicFibonacci (intN) {LongA = 1; Longb = 1; Longnum = 0; inti; if(n = = 1 | | n = = 2) {System.out.println (1); return; } Else { for(i = 3; I <= N; i++) {num= (A + b)% 10007; A=b; b=num;//try {//Thread.Sleep (1);//} catch (Interruptedexception e) {// //TODO Auto-generated catch block//e.printstacktrace ();// }} System.out.println (num); } }}
Java Unit Test Fibonacci series