No JUnit tests found in Eclipse using Groovy + Cucumber - eclipse

I have a Cucumber test in Groovy as follows
import org.junit.runner.RunWith
import cucumber.junit.Cucumber
import cucumber.junit.Cucumber.Options
#RunWith(Cucumber.class)
#Options(features = ["classpath:CarDrive.feature"])
public class FuelCarTest {
}
import cucumber.annotation.en.Given
import cucumber.annotation.en.Then
import cucumber.annotation.en.When
public class FuelCarSteps {
public FuelCarSteps() {
println "FuelCarSteps::FuelCarSteps"
}
#Given("I have a car")
def givenCar() {
println "I have a car"
}
#When("^you fill it with 50 litres of fuel")
def addFuel() {
println "add fuel"
}
#Then("^the tank contains 60 litres")
def checkBalance() {
println "TODO: add check here"
}
}
I can run the test fine using mvn test, but when I try to run it in Eclipse I get
No JUnit tests found
Tried cleaning, rebuilding & restarting

Hi you need to create run configuration for the each test in junit
Provide full path ( like package1.subpackege1.testclassname ) in class input in run config

Control, if you have any errors in the Groovy code, such as Type of expression is statically unknown: or similar. They don't show off as usual errors and could be almost invisible.

Related

citrus waitFor().condition() statement not waiting when used with ftpServer

I'm trying to use the citrus-framework to test an integration that writes some files on a FTP server.
I need to wait until some file is uploaded to the ftp (I'm using waitFor().condition() statement to accomplish that) and then receive the messages sent and do some assertions.
import com.consol.citrus.annotations.CitrusTest;
import com.consol.citrus.condition.Condition;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;
import com.consol.citrus.ftp.server.FtpServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.testng.annotations.Test;
import java.io.File;
#ActiveProfiles(value = "ftpTest")
#Test
public class FtpTest extends TestNGCitrusTestDesigner {
#Autowired
FtpServer ftpServer;
#Autowired
TestContext context;
#CitrusTest(name = "ftpTest")
public void ftpTest() {
// here I start my integration that uses a cron to upload the file
// this code is irrelevant for the example
Condition waitUntilFileIsUploaded = new Condition() {
#Override
public String getName () {
return "Check files on FTP";
}
#Override
public boolean isSatisfied (TestContext testContext){
return new File("/tmp/foo_dir").listFiles().length != 0;
}
#Override
public String getSuccessMessage (TestContext testContext){
return "Files found in FTP!";
}
#Override
public String getErrorMessage (TestContext testContext){
return "No file was found in FTP";
}
};
waitFor().condition(waitUntilFileIsUploaded).seconds(120L).interval(500L);
ftpServer.createConsumer().receive(context);
}
}
When I try to run this test looks like the waitFor() is never executed and ftpServer.createConsumer().receive(context); is executed before any file could be uploaded to the FTP.
This is the error that I'm getting:
ftpTest>TestNGCitrusTest.run:57->TestNGCitrusTest.run:111->TestNGCitrusTestDesigner.invokeTestMethod:73->TestNGCitrusTest.invokeTestMethod:133->ftpTest:49 ยป ActionTimeout
Any idea how I could fix this?
Also any complete example for using FTP Java DSL with Citrus would be more than welcome!
Please use test designer receive method instead of creating the consumer on your own.
receive(ftpServer)
.header("some-header", "some-value")
.payload("some payload");
Only then test designer can arrange the test actions in proper order. This is because test designer constructs the complete test action logic first and execution takes place at the very end of the test method.
As an alternative to that you could also use test runner instead of test designer. The runner will execute each test action immediately giving you the opportunity to add custom statements as you did before.

When I run tests with scalaTestPlus and Play framework, I get an error "no started application"

I'm trying to make scalaTestPlus work in my play application (I'm using Play! 2.2). It works well until I need a function coming from my application. For instance, if I run this very simple test (by launching in a sbt console "test-only TestName"):
import org.scalatestplus.play._
import org.scalatest._
import Matchers._
class Test extends PlaySpec {
"This test" must {
"run this very simple test without problem" in {
1 mustEqual 1
}
}
}
There is no problem, but as soon as I call a function from my app, like in this code:
class Test extends PlaySpec {
"This test" must {
"run this very simple test without problem" in {
models.Genre.genresStringToGenresSet(Option("test")) //Here is the problem
1 mustEqual 1
}
}
}
I get an error: java.lang.ExceptionInInitializerError: at... Cause: java.lang.RuntimeException: There is no started application (even if my application is running).
I'm probably missing something simple since I'm brand new to ScalaTest, so any help abut what I'm doing wrong would be apreciated ;)
You may need an application in scope when using PlaySpec, as some operations assume that there is a Play application available via Play.current:
class Test extends PlaySpec {
implicit override lazy val app: FakeApplication = FakeApplication(...)
"This test" must {
"run this very simple test without problem" in {
models.Genre.genresStringToGenresSet(Option("test")) //Here is the problem
1 mustEqual 1
}
}
}
Check the functional testing documentation for more details on FakeApplication.
However, I don't think you need should need this for model testing. In the normal ScalaTest docs for play, it seems to just mix inMockitoSugar. But your method call chain may invoke some global state of Play that does require an Application, in which case the FakeApplication is the way to go
As asked by #akauppi, here is a method that works perfectly for me:
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
class A extends PlaySpec with OneAppPerSuite {
"a" must {
"return true" in {
//thanks to with OneAppPerSuite, it now works
models.Genre.genresStringToGenresSet(Option("test"))
1 mustBe 1
}
"return false" in {
1 mustBe 2
}
}
}
And I simply launch the test with sbt ~testOnly a

error when running groovy test suite

created two tests in groovy able to run them indecently as groovy test cases but when I create a test suite and run them like groovy MyTestSuite.groovy on cmd line I get the below error:
F.F
Time: 0
There were 2 failures:
1) warning(junit.framework.TestSuite$1)junit.framework.AssertionFailedError: No tests found in mypack.ArithmeticTest
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1318)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:927)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:77)
at groovy.lang.GroovyShell.runJUnit3TestSuite(GroovyShell.java:370)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:277)
at groovy.lang.GroovyShell.run(GroovyShell.java:502)
at groovy.lang.GroovyShell.run(GroovyShell.java:491)
"a.txt" 53L, 3408C
The test suite class is as follows
package mypack
import junit.framework.TestSuite
import junit.framework.JUnit4TestAdapter
public class myTestSuite extends TestSuite {
// Since Eclipse launches tests relative to the project root,
// declare the relative path to the test scripts for convenience
private static final String TEST_ROOT = "src/mypack/";
public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite();
GroovyTestSuite gsuite = new GroovyTestSuite();
suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArithmeticGroovy.groovy"));
suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArrayTest.groovy"));
return suite;
}
}
ArithmeticGroovy.groovy
package mypack
import org.junit.Test
import static org.junit.Assert.assertEquals
class ArithmeticTest {
#Test
void additionIsWorking() {
assertEquals 4, 2+2
}
#Test(expected=ArithmeticException)
void divideByZero() {
println 1/0
}
}
when i run it through eclipse i get initializationerror. I am new to groovy any help is appreciated
Groovy seems to be confused if you confuse (pardon the pun :-) JUnit3 and JUnit4 style. Using TestSuite and addTestSuite is JUnit3 style and it should be coupled with classes derived from TestCase (or ultimately Test). Mixing JUnit4 annotation style won't work in this setup as exemplified in this post.
You should select either JUnit3 or JUnit4 style for your tests (where I personally tend to prefer JUnit4).

Mock assertEquals not found

I am new to Mockito and need to learn it for work.
I made a very simple class that has one method that returns a string.
I then made the following test class in eclipse.
import static org.junit.Assert.*;
import org.junit.Test;
import org.mockito.Mockito;
public class No_1Test {
#Test
public void testNo_1() {
No_1 myTest = Mockito.mock(No_1.class);
Mockito.when(myTest.HelloWorld()).thenReturn("Hello World");
String result = myTest.HelloWorld();
Mockito.assertEquals("Hello World", myTest.HelloWorld());
}
}
My understanding of what I have made so far is:
I made a mock class of my No_1 class.
I specified that whenever the HelloWorld() method is called it should return the string ("Hello World")
I stored the results of HelloWorld() into the variable result (which should be "Hello World")
I want to assert that it does what it was meant to do.
The problem is that in eclipse it says that the assertEquals method is undefined for Mockito.
Can someone please point out where I am going wrong here.
You are getting the error like assertEquals method is undefined for mockito because we can't use mockito as in mockito.assertEquals as in your codes try changing it with junit.assertEquals()
And What my experience on mockito says that you should avoid mock classes of the same project,we use to mock classes for which we are dependent on other projects or module,so don't mock No_1 class in your codes and try these codes::
import static org.junit.Assert.*;
import org.junit.Test;
import org.mockito.Mockito;
public class No_1Test {
#Test
public void testNo_1() {
Mockito.when(myTest.HelloWorld()).thenReturn("Hello World");
Junit.assertEquals("Hello World", myTest.HelloWorld());
}
}
And mockito is for mocking java classes or method results but try using junit for your testing as in junit.assertequals

Parameterized Groovy JUnit test-cases in Eclipse

I'm having trouble running a Parameterized Groovy JUnit test-case in Eclipse (see below for test code and environment details).
Symptoms
Right-clicking on the class in Package Explorer and doing Run As -> JUnit Test Case just brings up a dialog claiming "No JUnit tests found".
Right-clicking on the project and doing Run As -> JUnit Test Case runs all test-cases except the parameterized Groovy one.
Things I've tried
Ensuring a "normal" Groovy JUnit test-case runs. This works.
Ensuring a parameterized Java test-case runs. This works.
Manually creating a JUnit run configuration for this test-case. This works.
So
So I have an inconvenient workaround (3). But this isn't scalable, as this test-case still won't be included when I run all test-cases in the project.
Any ideas how I can get Eclipse/Groovy plugin/JUnit to automatically recognise my test-case?
Test-case code
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
#RunWith(Parameterized)
public class TestParams {
final int a
public TestParams(int a) { this.a = a }
#Parameters
public static Collection<Object[]> data() {
def cases = new Object[2][1]
cases[0][0] = 3
cases[1][0] = 4
Arrays.asList(cases)
}
#Test public void test() { println "a = $a" }
}
Environment
Eclipse Juno Service Release 2 (OSX)
Groovy-Eclipse 2.8.0
JUnit 4.10.0
this code works on my juno eclipse, junit 4.10 and groovy 2.0.6. i started with your code, but had to fool around with the imports as some of the annotations were red. i also had to add the .class to parameterized.
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
#RunWith(Parameterized.class) public class TestParams {
final int a
public TestParams(int a) { this.a = a }
#Parameters
public static Collection<Object[]> data() {
def cases = new Object[2][1]
cases[0][0] = 3
cases[1][0] = 4
Arrays.asList(cases)
}
#Test public void test() { println "a = $a" }
}