JAVA - can't import src/test/java to src/main/java





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I have created an E2E testing project, initially, all the java classes were in 'src/test/java", but now I want to move all the files that do not contain tests to the "src/main/java" folder, the problem is after I do it:
I can't import packages from "src/test/java" to "src/main/java" (but can do the opposite). like the packages do not exist at all for import.



Cleaned the project and tried to run via IDE and via maven and it does not change the result.



enter image description here



What am I doing wrong?










share|improve this question




















  • 3





    You shouldn't need to import test classes into production code, only vice versa.

    – Slaw
    Nov 29 '18 at 1:26






  • 1





    The error is telling you that your main code can’t depend on test files. When your main code is packaged, it won’t include tests - if you want it to work, it cannot depend on the tests.

    – Krease
    Nov 29 '18 at 1:28











  • Are you getting any red lines in the IDE?

    – AutomatedOwl
    Nov 29 '18 at 9:20











  • @AutomatedOwl - yes

    – Nir Tal
    Nov 29 '18 at 15:41


















2















I have created an E2E testing project, initially, all the java classes were in 'src/test/java", but now I want to move all the files that do not contain tests to the "src/main/java" folder, the problem is after I do it:
I can't import packages from "src/test/java" to "src/main/java" (but can do the opposite). like the packages do not exist at all for import.



Cleaned the project and tried to run via IDE and via maven and it does not change the result.



enter image description here



What am I doing wrong?










share|improve this question




















  • 3





    You shouldn't need to import test classes into production code, only vice versa.

    – Slaw
    Nov 29 '18 at 1:26






  • 1





    The error is telling you that your main code can’t depend on test files. When your main code is packaged, it won’t include tests - if you want it to work, it cannot depend on the tests.

    – Krease
    Nov 29 '18 at 1:28











  • Are you getting any red lines in the IDE?

    – AutomatedOwl
    Nov 29 '18 at 9:20











  • @AutomatedOwl - yes

    – Nir Tal
    Nov 29 '18 at 15:41














2












2








2








I have created an E2E testing project, initially, all the java classes were in 'src/test/java", but now I want to move all the files that do not contain tests to the "src/main/java" folder, the problem is after I do it:
I can't import packages from "src/test/java" to "src/main/java" (but can do the opposite). like the packages do not exist at all for import.



Cleaned the project and tried to run via IDE and via maven and it does not change the result.



enter image description here



What am I doing wrong?










share|improve this question
















I have created an E2E testing project, initially, all the java classes were in 'src/test/java", but now I want to move all the files that do not contain tests to the "src/main/java" folder, the problem is after I do it:
I can't import packages from "src/test/java" to "src/main/java" (but can do the opposite). like the packages do not exist at all for import.



Cleaned the project and tried to run via IDE and via maven and it does not change the result.



enter image description here



What am I doing wrong?







java eclipse maven testing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 1:29









Thusitha Thilina Dayaratne

4,69442953




4,69442953










asked Nov 29 '18 at 1:24









Nir TalNir Tal

538




538








  • 3





    You shouldn't need to import test classes into production code, only vice versa.

    – Slaw
    Nov 29 '18 at 1:26






  • 1





    The error is telling you that your main code can’t depend on test files. When your main code is packaged, it won’t include tests - if you want it to work, it cannot depend on the tests.

    – Krease
    Nov 29 '18 at 1:28











  • Are you getting any red lines in the IDE?

    – AutomatedOwl
    Nov 29 '18 at 9:20











  • @AutomatedOwl - yes

    – Nir Tal
    Nov 29 '18 at 15:41














  • 3





    You shouldn't need to import test classes into production code, only vice versa.

    – Slaw
    Nov 29 '18 at 1:26






  • 1





    The error is telling you that your main code can’t depend on test files. When your main code is packaged, it won’t include tests - if you want it to work, it cannot depend on the tests.

    – Krease
    Nov 29 '18 at 1:28











  • Are you getting any red lines in the IDE?

    – AutomatedOwl
    Nov 29 '18 at 9:20











  • @AutomatedOwl - yes

    – Nir Tal
    Nov 29 '18 at 15:41








3




3





You shouldn't need to import test classes into production code, only vice versa.

– Slaw
Nov 29 '18 at 1:26





You shouldn't need to import test classes into production code, only vice versa.

– Slaw
Nov 29 '18 at 1:26




1




1





The error is telling you that your main code can’t depend on test files. When your main code is packaged, it won’t include tests - if you want it to work, it cannot depend on the tests.

– Krease
Nov 29 '18 at 1:28





The error is telling you that your main code can’t depend on test files. When your main code is packaged, it won’t include tests - if you want it to work, it cannot depend on the tests.

– Krease
Nov 29 '18 at 1:28













Are you getting any red lines in the IDE?

– AutomatedOwl
Nov 29 '18 at 9:20





Are you getting any red lines in the IDE?

– AutomatedOwl
Nov 29 '18 at 9:20













@AutomatedOwl - yes

– Nir Tal
Nov 29 '18 at 15:41





@AutomatedOwl - yes

– Nir Tal
Nov 29 '18 at 15:41












1 Answer
1






active

oldest

votes


















1














You're confusing two different concepts here, source folders and packages.



A source folder is just that, a folder that contains sources. In most modern projects, you will find two such source folders, one for production code, the other for test code. In your example (standard Maven directory layout, that's src/main/java and src/test/java, respectively).



A package is a way of organizing classes, which is usually reflected in your source also. By convention, test and production code use the same package, e.g. a production class com.mypackage.Foo would usually be tested by a class named com.mypackage.FooTest . By convention, these package hierarchies also map to directory hierarchies in your sources. Note: while in 99.99999% of cases, you should stick with this convention for sanity purposes, it's not actually a requirement. You can organize your packages independent of the file system arangement (on the source side).



Anyway, in a standard setup, your directory layout may look like this:



src/main/java/com/mycompany/mypackage/SomeClass.java
src/test/java/com/mycompany/mypackage/SomeClassTest.java


So production and test class share a package, but live in different source folders. This setup is very powerful, because it lets your build tool export only your production code to the final output, while giving your test code package-level access to the code it's testing. Test and production code are compiled independently, the production code is unaware of the test code, which is how it should be.



In your situation, it seems you have production code in the test folder. To change that, moving the code to a different package is not the solution. Instead, you want to move the code to the same package, but in the production folder. If you're using IntelliJ, the move dialog allows you to select a different source root, which is exactly what you want, other IDEs will over similar features.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53530533%2fjava-cant-import-src-test-java-to-src-main-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You're confusing two different concepts here, source folders and packages.



    A source folder is just that, a folder that contains sources. In most modern projects, you will find two such source folders, one for production code, the other for test code. In your example (standard Maven directory layout, that's src/main/java and src/test/java, respectively).



    A package is a way of organizing classes, which is usually reflected in your source also. By convention, test and production code use the same package, e.g. a production class com.mypackage.Foo would usually be tested by a class named com.mypackage.FooTest . By convention, these package hierarchies also map to directory hierarchies in your sources. Note: while in 99.99999% of cases, you should stick with this convention for sanity purposes, it's not actually a requirement. You can organize your packages independent of the file system arangement (on the source side).



    Anyway, in a standard setup, your directory layout may look like this:



    src/main/java/com/mycompany/mypackage/SomeClass.java
    src/test/java/com/mycompany/mypackage/SomeClassTest.java


    So production and test class share a package, but live in different source folders. This setup is very powerful, because it lets your build tool export only your production code to the final output, while giving your test code package-level access to the code it's testing. Test and production code are compiled independently, the production code is unaware of the test code, which is how it should be.



    In your situation, it seems you have production code in the test folder. To change that, moving the code to a different package is not the solution. Instead, you want to move the code to the same package, but in the production folder. If you're using IntelliJ, the move dialog allows you to select a different source root, which is exactly what you want, other IDEs will over similar features.






    share|improve this answer




























      1














      You're confusing two different concepts here, source folders and packages.



      A source folder is just that, a folder that contains sources. In most modern projects, you will find two such source folders, one for production code, the other for test code. In your example (standard Maven directory layout, that's src/main/java and src/test/java, respectively).



      A package is a way of organizing classes, which is usually reflected in your source also. By convention, test and production code use the same package, e.g. a production class com.mypackage.Foo would usually be tested by a class named com.mypackage.FooTest . By convention, these package hierarchies also map to directory hierarchies in your sources. Note: while in 99.99999% of cases, you should stick with this convention for sanity purposes, it's not actually a requirement. You can organize your packages independent of the file system arangement (on the source side).



      Anyway, in a standard setup, your directory layout may look like this:



      src/main/java/com/mycompany/mypackage/SomeClass.java
      src/test/java/com/mycompany/mypackage/SomeClassTest.java


      So production and test class share a package, but live in different source folders. This setup is very powerful, because it lets your build tool export only your production code to the final output, while giving your test code package-level access to the code it's testing. Test and production code are compiled independently, the production code is unaware of the test code, which is how it should be.



      In your situation, it seems you have production code in the test folder. To change that, moving the code to a different package is not the solution. Instead, you want to move the code to the same package, but in the production folder. If you're using IntelliJ, the move dialog allows you to select a different source root, which is exactly what you want, other IDEs will over similar features.






      share|improve this answer


























        1












        1








        1







        You're confusing two different concepts here, source folders and packages.



        A source folder is just that, a folder that contains sources. In most modern projects, you will find two such source folders, one for production code, the other for test code. In your example (standard Maven directory layout, that's src/main/java and src/test/java, respectively).



        A package is a way of organizing classes, which is usually reflected in your source also. By convention, test and production code use the same package, e.g. a production class com.mypackage.Foo would usually be tested by a class named com.mypackage.FooTest . By convention, these package hierarchies also map to directory hierarchies in your sources. Note: while in 99.99999% of cases, you should stick with this convention for sanity purposes, it's not actually a requirement. You can organize your packages independent of the file system arangement (on the source side).



        Anyway, in a standard setup, your directory layout may look like this:



        src/main/java/com/mycompany/mypackage/SomeClass.java
        src/test/java/com/mycompany/mypackage/SomeClassTest.java


        So production and test class share a package, but live in different source folders. This setup is very powerful, because it lets your build tool export only your production code to the final output, while giving your test code package-level access to the code it's testing. Test and production code are compiled independently, the production code is unaware of the test code, which is how it should be.



        In your situation, it seems you have production code in the test folder. To change that, moving the code to a different package is not the solution. Instead, you want to move the code to the same package, but in the production folder. If you're using IntelliJ, the move dialog allows you to select a different source root, which is exactly what you want, other IDEs will over similar features.






        share|improve this answer













        You're confusing two different concepts here, source folders and packages.



        A source folder is just that, a folder that contains sources. In most modern projects, you will find two such source folders, one for production code, the other for test code. In your example (standard Maven directory layout, that's src/main/java and src/test/java, respectively).



        A package is a way of organizing classes, which is usually reflected in your source also. By convention, test and production code use the same package, e.g. a production class com.mypackage.Foo would usually be tested by a class named com.mypackage.FooTest . By convention, these package hierarchies also map to directory hierarchies in your sources. Note: while in 99.99999% of cases, you should stick with this convention for sanity purposes, it's not actually a requirement. You can organize your packages independent of the file system arangement (on the source side).



        Anyway, in a standard setup, your directory layout may look like this:



        src/main/java/com/mycompany/mypackage/SomeClass.java
        src/test/java/com/mycompany/mypackage/SomeClassTest.java


        So production and test class share a package, but live in different source folders. This setup is very powerful, because it lets your build tool export only your production code to the final output, while giving your test code package-level access to the code it's testing. Test and production code are compiled independently, the production code is unaware of the test code, which is how it should be.



        In your situation, it seems you have production code in the test folder. To change that, moving the code to a different package is not the solution. Instead, you want to move the code to the same package, but in the production folder. If you're using IntelliJ, the move dialog allows you to select a different source root, which is exactly what you want, other IDEs will over similar features.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 1:38









        Sean Patrick FloydSean Patrick Floyd

        233k47394531




        233k47394531
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53530533%2fjava-cant-import-src-test-java-to-src-main-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks