-1

I used to only have a default package to do my coursework. Now I am currently learning JUnit testing, so I created another package called test. However, I was unable to access a public class called QuickSort in another package.

Below is the structure of the project and the line of the code where the problem occurred enter image description here

Code:

package test;

import org.junit.Test;
import org.junit.Assert;

public class TestQuickSort {

    int[] numbers = new int[] { 1, 5, 3, 6, 7, 84, 2, 4, 3 };
    int[] expectedNumbers = new int[] { 1, 2, 3, 3, 4, 5, 6, 7, 84 };

    @Test
    public void test() {
        QuickSort qs = new QuickSort(numbers);
        Assert.assertEquals(expectedNumbers, qs.sort());
    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Xuzheng Wang
  • 531
  • 5
  • 17
  • Show us also the `QuickSort` code (at least the class declaration), – Krzysztof Krasoń Oct 17 '15 at 11:23
  • Normally, a JUnit test has the same package as the class it is testing, however so that the test code is e.g. under `src/test` and the main code under `src/main`, in parallel folder hierarchies. You just need to adjust the build path of your IDE to accommodate this. – Mick Mnemonic Oct 17 '15 at 11:28

2 Answers2

5

Dont do that. Never use the default package unless you absolutely have to. You want to use two projects; one containing the "production" code; the other to contain the test code; both using identical directory layout:

"real code project": src/some/package

"test project": src/some/package

You put your "code to test" into the first project/directory; and your test code into the second one. Then you have to make sure that the project settings of the test project are setup so that it "see" the other project.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

You cannot import a class from an unnamed package (such as the default package). It results in a compile time error. This is why it's not suggested to use the default package; however, I can completely see why this error is made if you are new to using multiple packages.