【问题标题】:Why doesn't JUnit test work in Play 2.3.1/Java project?为什么 JUnit 测试在 Play 2.3.1/Java 项目中不起作用?
【发布时间】:2014-07-10 14:15:15
【问题描述】:

在我全新的 Play 2.3.1 Java 应用程序中,我编写了第一个测试,但它们根本不起作用。我启动activator test,一个简单的测试类直接放在test目录下的默认包中没有到达。

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

public class TestTest {

    @Test
    public void testTest() {
        Assert.assertEquals(true, false);
    }
}

当我启动 activator testOnly TestTest 时,我有以下输出:

[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for test:testOnly
[success] Totaltime: 6 s, completed 11 juil. 2014 09:08:38

可能是什么原因?

【问题讨论】:

    标签: java junit playframework junit4 playframework-2.3


    【解决方案1】:

    遇到了同样的问题,没有找到测试; activator clean 解决了这个问题。

    【讨论】:

    • 解决了,请问是什么原因?
    【解决方案2】:

    它对我有用,因此您的问题需要更多详细信息才能找到有用的解决方案。

    运行activator --version 来检查版本。我的基于 sbt 启动器 0.13.5。

    ➜  sandbox  activator --version
    sbt launcher version 0.13.5
    

    关于激活器本身的版本并没有说太多(但是激活器 1.2.x 可能是 sbt 0.13.5)。如果您的年龄较大,请升级。

    运行activator new my-brand-new-play-app play-java 创建一个 Play/Java 应用程序。

    ➜  sandbox  activator new my-brand-new-play-app play-java
    
    Fetching the latest list of templates...
    
    OK, application "my-brand-new-play-app" is being created using the "play-java" template.
    
    To run "my-brand-new-play-app" from the command line, "cd my-brand-new-play-app" then:
    /Users/jacek/sandbox/my-brand-new-play-app/activator run
    
    To run the test for "my-brand-new-play-app" from the command line, "cd my-brand-new-play-app" then:
    /Users/jacek/sandbox/my-brand-new-play-app/activator test
    
    To run the Activator UI for "my-brand-new-play-app" from the command line, "cd my-brand-new-play-app" then:
    /Users/jacek/sandbox/my-brand-new-play-app/activator ui
    

    然后我将您的 TestTest 课程复制到 test/TestTest.java 没有任何更改:

    ➜  my-brand-new-play-app  cat test/TestTest.java
    import org.junit.Assert;
    import org.junit.Test;
    
    public class TestTest {
    
        @Test
        public void testTest() {
        Assert.assertEquals(true, false);
        }
    }
    

    在创建测试文件的唯一更改后,我运行了activator test。经过大量文件下载后,结果是:

    ➜  my-brand-new-play-app  activator test
    [info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
    [info] Loading project definition from /Users/jacek/sandbox/my-brand-new-play-app/project
    [info] Set current project to my-brand-new-play-app (in build file:/Users/jacek/sandbox/my-brand-new-play-app/)
    [info] ApplicationTest
    [info] + ApplicationTest.simpleCheck
    [info] + ApplicationTest.renderTemplate
    [info]
    [info]
    [info] Total for test ApplicationTest
    [info] Finished in 0.015 seconds
    [info] 2 tests, 0 failures, 0 errors
    [error] Test IntegrationTest.test failed: <''> should contain the String:<'Your new application is ready.'>, took 1.742 sec
    [info] IntegrationTest
    [info] x IntegrationTest.test
    [error] sbt.ForkMain$ForkError: <''> should contain the String:<'Your new application is ready.'>
    [error]     at org.fest.assertions.Fail.failure(Fail.java:228)
    [error]     at org.fest.assertions.Assert.failure(Assert.java:149)
    [error]     at org.fest.assertions.StringAssert.contains(StringAssert.java:73)
    [error]     at IntegrationTest$1.invoke(IntegrationTest.java:23)
    [error]     at IntegrationTest$1.invoke(IntegrationTest.java:20)
    [error]     at play.test.Helpers.running(Helpers.java:549)
    [error]     at play.test.Helpers.running(Helpers.java:536)
    [error]     at IntegrationTest.test(IntegrationTest.java:20)
    [info]
    [info]
    [info] Total for test IntegrationTest
    [info] Finished in 0.005 seconds
    [info] 1 tests, 1 failures, 0 errors
    [error] Test TestTest.testTest failed: expected:<true> but was:<false>, took 0.0 sec
    [info] TestTest
    [info] x TestTest.testTest
    [error] sbt.ForkMain$ForkError: expected:<true> but was:<false>
    [error]     at org.junit.Assert.fail(Assert.java:88)
    [error]     at org.junit.Assert.failNotEquals(Assert.java:743)
    [error]     at org.junit.Assert.assertEquals(Assert.java:118)
    [error]     at org.junit.Assert.assertEquals(Assert.java:144)
    [error]     at TestTest.testTest(TestTest.java:8)
    [info]
    [info]
    [info] Total for test TestTest
    [info] Finished in 0.002 seconds
    [info] 1 tests, 1 failures, 0 errors
    [error] Failed: Total 4, Failed 2, Errors 0, Passed 2
    [error] Failed tests:
    [error]     IntegrationTest
    [error]     TestTest
    [error] (test:test) sbt.TestsFailedException: Tests unsuccessful
    [error] Total time: 3 s, completed Jul 10, 2014 11:27:04 PM
    

    TestTest 测试确实执行了。

    我也只使用'testOnly TestTest' 执行了TestTest,如下所示:

    ➜  my-brand-new-play-app  activator 'testOnly TestTest'
    [info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
    [info] Loading project definition from /Users/jacek/sandbox/my-brand-new-play-app/project
    [info] Set current project to my-brand-new-play-app (in build file:/Users/jacek/sandbox/my-brand-new-play-app/)
    [error] Test TestTest.testTest failed: expected:<true> but was:<false>, took 0.003 sec
    [info] TestTest
    [info] x TestTest.testTest
    [error] sbt.ForkMain$ForkError: expected:<true> but was:<false>
    [error]     at org.junit.Assert.fail(Assert.java:88)
    [error]     at org.junit.Assert.failNotEquals(Assert.java:743)
    [error]     at org.junit.Assert.assertEquals(Assert.java:118)
    [error]     at org.junit.Assert.assertEquals(Assert.java:144)
    [error]     at TestTest.testTest(TestTest.java:8)
    [info]
    [info]
    [info] Total for test TestTest
    [info] Finished in 0.012 seconds
    [info] 1 tests, 1 failures, 0 errors
    [error] Failed: Total 1, Failed 1, Errors 0, Passed 0
    [error] Failed tests:
    [error]     TestTest
    [error] (test:testOnly) sbt.TestsFailedException: Tests unsuccessful
    [error] Total time: 1 s, completed Jul 10, 2014 11:29:37 PM
    

    总而言之,它工作得很好。我不确定您的问题到底是什么,因为我按照问题描述允许的方式严格遵循了您的步骤。

    【讨论】:

    • 好的,我从头开始重新启动我的项目并且它工作正常。我想我做了坏事。
    • 很高兴它成功了。如果有帮助,请接受答案。谢谢!
    【解决方案3】:

    activator clean 的解决方案有时在 Eclipse 运行时不起作用。关闭 Eclipse 然后执行 activator clean 对我有用。

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2015-05-17
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多