Java Unit Test Fibonacci series

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.