简体   繁体   English

如何使用jruby将rspec用于Java应用程序?

[英]How to use rspec for a java application using jruby?

I have a simple spring mvc application that I want to test with using rspec and cucumber (or with rspec's integration testing features). 我有一个简单的spring mvc应用程序,我想使用rspec和Cucumber(或rspec的集成测试功能)进行测试。

How do I go about importing my java war file so rspec can use it? 如何导入Java war文件,以便rspec可以使用它?

Any suggestions on folder structure also? 还有关于文件夹结构的建议吗?

I'm using maven also, so I have this so far: 我也正在使用Maven,因此到目前为止:

/project_name/src/main
/project_name/src/main/java
/project_name/src/main/webapp
/project_name/src/main/test
/project_name/src/main/resources

Should I add rspec and cucumber like: 我应该像这样添加rspec和黄瓜吗:

/project_name/spec
/project_name/features

I'm confused how rspec will be able to import my java libs? 我很困惑,rspec将如何导入我的Java库?

Make sure the package is built, then: 确保软件包已构建,然后:

require 'java'
require "/some/path/\*.jar" 
instance = Java::my.package.MyClass.new
instance.should be

https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby

Why don't you try Ginkgo4j ? 您为什么不尝试Ginkgo4j It a Java port of RSpec that allows you to test the same way as RSpec, just in Java. 它是RSpec的Java端口,可让您使用Java测试与RSpec相同的方式。

Add this dependency to your POM: 将此依赖项添加到您的POM中:

    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>ginkgo4j</artifactId>
        <version>1.0.9</version>
        <scope>test</scope>
    </dependency>

Create a JUnit test case specifying the Ginkgo4jRunner runner: 创建一个指定Ginkgo4jRunner运行器的JUnit测试用例:

@RunWith(Ginkgo4jRunner.class)
@Ginkgo4jConfiguration(threads = 1)
public class MyTest {
  {
    Describe("ClassUnderTest", () -> {
      Context("#Method", () -> {
        BeforeEach(() -> {
          // before test logic          
        }); 
        JustBeforeEach(() -> {
          ClassUnderTest.Method();          
        }); 
        Context("given a context", () -> {
          It("should do something", () -> {
            // assertions
          });
        });
        Context("given a different context", () -> {
          It("should do something else", () -> {
            // assertions
          });
        });
        AfterEach(() -> {
          // after test logic
        });
      });
    }); 
  }
}

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

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