同一项目中的 Eclipse junit 测试

发布于 2024-09-16 05:54:02 字数 292 浏览 6 评论 0原文

这是一个相对开放的问题。如果我在 Eclipse 的项目中构建了一个应用程序,然后想要测试该项目,我应该在同一项目中创建 JUnit 代码还是创建一个单独的项目。例如...

ShopSystem 可能是我的主项目的名称 - 我应该创建一个名为 ShopSystemTest 的项目吗?

一般来说 - 测试代码应该存储在距主项目文件夹多“远”的地方?如果我将测试代码存储在主项目中,然后将主项目导出为可运行的 jar,它将携带测试代码,这并不理想......

有建议吗?

This is a relatively open question. If I have built an application in a project in Eclipse and I then want to test this project, should I create the JUnit code within the same project or create a separate project. For instance...

ShopSystem maybe the name of my main project - should I create a project called say, ShopSystemTest?

In general - how far "away" should the testing code be stored from the main project folder? If I store the testing code within the main project and then export the main project as a runnable jar it will take the testing code with it, which isn't ideal...

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

驱逐舰岛风号 2024-09-23 05:54:02

虽然没有唯一正确的方法,但通常的方法是将单元测试保留在同一个项目中。

您可以创建第二个源文件夹(如 test),在其中将测试类放入与被测类相同的包中。这还允许您测试包私有类,同时不会用测试类淹没您的主源包。

您的源文件夹/包结构将如下所示:

-sources
   -main
       -my.package
             -MyClass.java
   -test
       -my.package
             -MyClassTest.java

然后,您可以将构建配置为在打包 JAR 时不包含 test 源文件夹。

While there is no only right way, the usual approach is to keep unit tests in the same project.

You can create a second source folder (like test), where you put your test classes into the same packages as the classes under test. This also allows you to test package-private classes while not flooding your main source packages with test classes.

Your source folder/package structure would then look like this:

-sources
   -main
       -my.package
             -MyClass.java
   -test
       -my.package
             -MyClassTest.java

You can then configure your build to not include the test source folder when packing the JAR.

骄傲 2024-09-23 05:54:02

我非常喜欢 Maven 约定:同一个项目中的 main 和 test 有一个单独的源代码树,部署了主代码,但没有部署测试代码。包结构可以(但不必)相同。

project
    src
        main
             java      // source files
             resources // xml, properties etc
        test
             java      // source files
             resources // xml, properties etc

在 Eclipse 中,当您选择 new ->; JUnit 测试用例,只需将源文件夹更改为 src/test/java 并保留建议的包不变。

(保留在同一个包中的好处之一是可以访问受保护的和包范围的成员,尽管这不是“正确的”单元测试行为)


更新:下面是一些代码来说明我的最后一点:

主类(在 src/main/java 中):

package com.test;
public class Foo{

    static class Phleem{
        public Phleem(final String stupidParameter){
        }
    }

    String bar;
    protected String baz;
    protected Object thingy;

}

测试类(在 src/test/java 中):

package com.test;
import org.junit.Test;

public class FooTest{

    @Test
    public void testFoo(){
        final Foo foo = new Foo();
        foo.bar = "I can access default-scoped members";
        foo.baz = "And protected members, too";
        foo.thingy = new Foo.Phleem("And I can access default-scoped classes");
    }

}

I like the maven convention a lot: There is a separate source tree for main and test in the same project, main code gets deployed, test code doesn't. Package structures can be (but don't have to be) identical.

project
    src
        main
             java      // source files
             resources // xml, properties etc
        test
             java      // source files
             resources // xml, properties etc

And in eclipse, when you choose new -> JUnit test case, you just change the source folder to src/test/java and leave the suggested package as is.

(One of the benefits of remaining in the same package is having access to protected and package scoped members, although this is not 'proper' unit test behavior)


Update: Here's some code to illustrate my last point:

Main class (in src/main/java):

package com.test;
public class Foo{

    static class Phleem{
        public Phleem(final String stupidParameter){
        }
    }

    String bar;
    protected String baz;
    protected Object thingy;

}

Test class (in src/test/java):

package com.test;
import org.junit.Test;

public class FooTest{

    @Test
    public void testFoo(){
        final Foo foo = new Foo();
        foo.bar = "I can access default-scoped members";
        foo.baz = "And protected members, too";
        foo.thingy = new Foo.Phleem("And I can access default-scoped classes");
    }

}
£噩梦荏苒 2024-09-23 05:54:02

通常你有 -

/src/main/java   (for codes)

/src/test/java   (for tests)

Typically you have -

/src/main/java   (for codes)

/src/test/java   (for tests)
牵强ㄟ 2024-09-23 05:54:02

考虑maven方式:在maven项目中,源是这样组织的

src
 |--main
 |     |--java
 |--test
       |--java

你的源代码放在src / main / java中,你的junit测试代码放在src / test / java中,它们都是源文件夹(因此你可以将 jUnit 代码与 Java 代码放在同一个包中,但放在不同的源文件夹中)。

有趣的是,对于通常的编码,您的 jUnit 类位于代码包中,但在创建 jar 时,您可以仅使用来自 src/main/java 的类,而不发布您的测试。

Consider the maven way : In a maven project, soruces are organized this way

src
 |--main
 |     |--java
 |--test
       |--java

Your source code goes in src/main/java, your junit test code goes in src/test/java, they both are source folder (and as a consequence you can put your jUnit code in the same package as your Java code, but in a different source folder).

The interest is that for usual coding, your jUnit classes are in code packages, but on jar creation, you can take classes coming only from src/main/java and not release your tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文