简体   繁体   中英

Why doesn't JUnit test work in Play 2.3.1/Java project?

In my brand new Play 2.3.1 Java application, I write the first tests, but they don't work at all. I launch activator test , a simple test class put directly in the default package in the test directory isn't reached.

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

public class TestTest {

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

When I launch activator testOnly TestTest , I have the following output :

[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

What could be the cause ?

Ran into the same issue where no tests were found; activator clean resolved the problem.

It works for me so your issue need more details to have a useful solution.

Run activator --version to check the version. Mine is based on sbt launcher 0.13.5.

➜  sandbox  activator --version
sbt launcher version 0.13.5

It doesn't say much about the version of activator itself (however activator 1.2.x is likely sbt 0.13.5). Upgrade if yours is older.

Ran activator new my-brand-new-play-app play-java to create a Play/Java application.

➜  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

I then copied your TestTest class to test/TestTest.java with no changes:

➜  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);
    }
}

With the only change to create the test file, I ran activator test . After a whole lot of file downloads, the results was:

➜  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

The TestTest test was indeed executed.

I also executed the TestTest only with 'testOnly TestTest' as follows:

➜  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

All in all, it worked fine. I'm unsure what might be the issue of yours exactly since I followed your steps as closely as the description of the issue allowed.

The solution with activator clean sometimes doesn't work when Eclipse is running. Closing Eclipse and then peforming activator clean afterwards worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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