atom2ueki
atom2ueki

Reputation: 837

Junit test can't pass all test case once

I have a very strange problem, when i try to run a JUnit test with multiple test case, it will only pass the first test case and shown IndexOut of Bound error

public class ABCTest {
    @Test
    public void basicTest1(){...}
    @Test
    public void basicTest2(){...}
    ...

but if i commend the rest test case, test them one by one, it will pass all of them.

public class ABCTest {    
    @Test
    public void basicTest1(){...}
    //@Test
    //public void basicTest2(){...}
    //...

Upvotes: 1

Views: 838

Answers (2)

Thomas
Thomas

Reputation: 12009

Since you do not provide the complete testcase and implementation class, I have to make some assumptions.

Most likely you are mutating the state of the tested object by the testcase.

Usually you try to get a clean test fixture for each unit test. This works by having a method with the @Before annotation which creates a new instance of the class under test. (This was called 'setUp()' in older versions of junit.)

This ensures that the order of test method execution as well as the number of executions does not matter and each method is working isolated.

Upvotes: 7

ford prefect
ford prefect

Reputation: 7388

Look at what you are doing inside of the test case and see if you are changing data that may be used by the other test cases and not restoring it to the original state. For example you have a text file that you read and write to in basicTest1 that you then read again in basicTest2 but assume the file is the same as it was before you ran basicTest1.

This is just one possible problem. would need to see the code for more insight

Upvotes: 0

Related Questions