简体   繁体   English

在两个不同的 JVM 中测试通信软件

[英]Testing communications software in two different JVMs

I've written a module in Java that communicates over a socket.我用 Java 编写了一个通过套接字进行通信的模块。 I'd like to write a unit test that tests sending and receiving.我想编写一个单元测试来测试发送和接收。

To really test the system, I'd like to put the sender in one JVM and the receiver in another and test them together.为了真正测试系统,我想将发送方放在一个 JVM 中,将接收方放在另一个 JVM 中,然后一起测试它们。

From what I've read here and in various blog posts, this isn't really unit testing, it's integration testing.从我在这里和各种博客文章中读到的内容来看,这并不是真正的单元测试,而是集成测试。 Ok, fine, but I still want it to be part of the build process.好的,很好,但我仍然希望它成为构建过程的一部分。

Is there an official, recommended way to launch a second JVM from within a Junit test case?是否有官方推荐的方法可以从 Junit 测试用例中启动第二个 JVM? If not, is there another standard testing framework that is designed for this?如果没有,是否有另一个为此设计的标准测试框架?

I use some shell scripts in my build.我在构建中使用了一些 shell 脚本。 Not sure how to verify the results, but whatever runs Maven in your automated build could check some output file or something.不确定如何验证结果,但是在自动构建中运行 Maven 的任何内容都可以检查某些输出文件或其他内容。 Under build/plugins I do this:在构建/插件下我这样做:

<plugin>
    <artifactId>exec-maven-plugin</artifactId>
    <groupId>org.codehaus.mojo</groupId>
    <version>1.3.2</version>
    <executions>
        <execution>
            <id>Validate English and Term Customization</id>
            <phase>test</phase>
            <goals><goal>exec</goal></goals>
            <configuration>
                <executable>${project.basedir}/validate.sh</executable>
                <workingDirectory>${project.basedir}</workingDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

You are essentially going to have to create a separate project to deploy and test your code over the network.您基本上必须创建一个单独的项目来通过网络部署和测试您的代码。 This is going to be a pain in many ways.这在很多方面都将是痛苦的。 You might consider "integration testing" and "pain in many ways" to be somewhat synonymous.您可能认为“集成测试”和“许多方面的痛苦”在某种程度上是同义词。

Keeping integration tests up to date is probably the world's best inspiration for doing better unit testing.使集成测试保持最新可能是世界上进行更好的单元测试的最佳灵感。 I'd still try to Unit-Test as well as possible.我仍然会尽可能地尝试单元测试。

  • Put your unit test in a different package from the code you are testing to assure you aren't accessing anything package-scoped.将您的单元测试放在与您正在测试的代码不同的包中,以确保您不会访问包范围内的任何内容。
  • Every time something breaks on 2 servers, try very hard to write a unit test that fails in your build before fixing the bug, then verify that it passes once fixed.每次在 2 个服务器上出现问题时,在修复错误之前努力编写一个在构建中失败的单元测试,然后验证它在修复后是否通过。
  • Thorough unit testing is usually possible if you're willing to stub out enough things.如果您愿意剔除足够多的东西,通常可以进行彻底的单元测试。 In my experience, it's been easier to undergo a Herculean effort to stub things out, than it is to have a whole separate (yet totally dependent) project to do unit testing that will break your build if it doesn't work.根据我的经验,比起用一个完整的独立(但完全依赖)项目来进行单元测试,如果它不起作用,它会破坏你的构建,更容易进行艰巨的努力来解决问题。
  • Could you find those bugs with a code-inspection tool (either off-the shelf, or a grep/sed script, or a custom program) instead of an integration test?您能否使用代码检查工具(现成的、grep/sed 脚本或自定义程序)而不是集成测试来找到这些错误? I'd much rather see you run that from your build.我更愿意看到你从你的构建中运行它。

Even if you have a whole separate integration testing project that's part of your build, you might want to run that once/day once/week instead of every check-in.即使您有一个完整的独立集成测试项目,它是您构建的一部分,您可能希望每天一次/每周运行一次,而不是每次签入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM