How do I install TestFX? - java

I'd like to put together a set of jUnit tests under Eclipse (Neon) on Windows to automate the testing of JavaFX GUIs. It seems that TestFX is the bee's knees for this kind of thing, but having looked around the internet quite a bit, I'm still not sure how to install TestFX without using Maven or Gradle.
I am not familiar with Maven and Gradle, and trying to follow the simple instructions to install TestFX via Maven was unsuccessful. This was done under Eclipse Mars, after which my simple GUI program threw up a compile error about not being able to find or load main class and a run-time error that Selection does not contain a main type. (The simple GUI program ran without errors previously.) After this, I downloaded the latest Eclipse Neon and tried to start afresh.
This is what I did:
Download and install Eclipse Neon from eclipse.org.
Create a Java Project called TestProject (execution environment JavaSE-1.8).
Grant access to javafx/** (right-click on project -- properties -- Java Build Path -- Libraries -- JRE System Library -- Access Rules -- add).
Create MyClass with minimal contents:
package test;
import javafx.application.Application;
import javafx.stage.Stage;
public class MyClass extends Application {
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("Hello World");
stage.show();
}
}
Copy the file testfx-core-4.0.0-20150226.214553-8.jar from the testfx repository (linked from the Via direct download section of How to use TestFX in your project) into my eclipse project, sitting at the same level as the JRE System Library;
Add the jar file to the build path (right-click on project -- properties -- Java Build Path -- Libraries -- Add JARS) - this automatically created a directory called Referenced Libraries and copied the jar file into it; and
Create a JUnit test (right-click on source folder -- New -- JUnit Test Case), filling in the appropriate package and Class under test fields, and giving the test case a name (MyTest).
Then I tried to extend the class MyTest to use testfx:
class MyTest extends GuiTest {} as advised in this link; and (separately)
class MyTest extends ApplicationTest{} as advised in this link.
Here's the code in the second case:
package test;
import org.junit.Test;
import org.testfx.framework.junit.ApplicationTest;
public class MyTest extends ApplicationTest { }
In each case, eclipse complains that the superclass GuiTest / ApplicationTest cannot be resolved to a type.
I suspect that the problem is that I haven't properly installed testfx. Can anyone help?

There are multiple dependencies; the following jar files must be added to the Referenced Libraries (after copying the jar files into the project, right-click on the project -- Properties -- Java Build Path -- Libraries -- Add JARs...).
testfx-core-4.0.4-alpha.jar, source: http://mavensearch.io/repo/org.testfx/testfx-junit/4.0.4-alpha
testfx-junit-4.0.4-alpha.jar, source: http://mavensearch.io/repo/org.testfx/testfx-core/4.0.4-alpha
guava-18.0.jar, source: http://mavensearch.io/repo/com.google.guava/guava/18.0
hamcrest-core-1.3.jar, source: http://mavensearch.io/repo/org.hamcrest/hamcrest-all/1.3
The above is independent of Gradle and Maven. Alternatively, the files can also be pulled using Gradle:
Create a Gradle project in Eclipse.
In the dependencies block in build.gradle, insert the following lines (source):
testCompile "org.testfx:testfx-core:4.0.+"
testCompile "org.testfx:testfx-junit:4.0.+"
Right-click the project -- Gradle -- Refresh Gradle Project
This places the required files into the Project and External Dependencies folder.
Useful tutorial for TestFX 4: https://www.youtube.com/watch?v=NG03nNpSmgU.

Related

How do I resolve "the type is not accessible" error in Eclipse when using an external JAR? JAR in question is algs4 from Princeton [duplicate]

I'm new to the whole programming stuff but here's my problem:
I used to add my JUnit test cases in Eclipse by right clicking on the project, and just add New > JUnit Test Case.
Currently, I am not able to implement any test methods because Eclipse tells me on the line
import static org.junit.jupiter.api.Assertions.*;
the error message
The type org.junit.jupiter.api.Assertions is not accessible.
Error I get in the IDE:
I tried the following:
Reinstalling Eclipse, using a fresh workplace.
Adding the JUnit to Build path
Nothing helped.
It worked and works in older projects just fine.
Here is how the Package Explorer looks:
What am I missing?
You use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

How to solve Could not find or load main class?

I would like to build a java application.
System reported error message:
Error: Could not find or load main class com.autoparts.autoeshop.Application
My controller:
package com.autoparts.autoeshop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
#SpringBootApplication
#EnableJpaAuditing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And I have written JAVA_HOME on system property.
Your environment is not completly correct as the JAVA_HOME should point to the installation directory (jdk1.8.0_161) instead of bin and the PATH should include %JAVA_HOME%/bin.
Your trial to compile with javac has been done from the wrong directory, you need to compile when you are in the java directory (including the relative path to the java file like com/autoparts/autoeshop/Application.java). Maybe your project has a build system prepared in the project directory (autoeshop) like Maven (look for a pom.xml) or Gradle (find a build.gradle)? If so, install the required build tool and run it, it will download all required dependencies and compile all the java files for you which is needed before you can run the application using the created jar file (typically found in a target (Maven) or build (Gradle) folder after the build tool ran.
Anyway, if you struggle with this kind of problems you may consider to start with some basic Java or at least Spring Boot tutorials.

Exporting Scala project as jar from Eclipse

I have a Scala project and I would like to export it as a jar.
*1. At first I tried creating a Java class for the project as an entry point
public class JMain {
public static void main(String[] args) {
System.out.println("Java main calling Scala main");
SMain.main(new String[] {""}); //SMain.main is the actual *main*
and this worked fine and dandy when launched from Eclipse, but when I export it as jar it'll give me 18 exceptions or so. I do now know how to replicate then "environment" in which Eclipse manages to launch this and I'm prety sure it relies on the fact that Scala is on my system already - I need a self contained jar with everything packed in there.
*2. My second try consisted of trying what lach suggested here How to deploy a Scala project from Eclipse?
namely:
public class JMain {
public static void main(String[] args) {
System.out.println("Java Main");
List<String> argList = new ArrayList<String>();
argList.add("fully.qualified.ClassName"); //???
for (String s : args) argList.add(s);
scala.tools.nsc.MainGenericRunner.main(argList.toArray(new String[0]));
This time it won't even run from Eclipse, although it gives only 6 or so exceptions starting with the famous NoClassDefFoundError. I have a feeling I'm not getting fully.qualified.ClassName right. *3. If the main Scala class is called "Dis.scala" and is located in package "pack" shouldn't this fully.qualified.ClassName be "pack.Dis"?
I'm using Jre 1.6 and Scala 2.9.2
EDIT: I have included all external imported jars, even scala-library.jar - everything is nice and packed in the jar
P.S. I am not familiar with Ant or Maven or Sbt. I just want my Scala project jared - if possible without getting into hairy things.
Here is what worked for me:
1. Create scala project
2. Create Wrapper java project
3. Add the scala-library.jar to you java project build path.
So you only need the 3rd step in addition since the rest looks similar to what I did. Then you can happily use: java - jar file.jar
EDIT:
How to create a JAR File which contains Scala/Code which can be consumed by another Java Project, using Scala - Eclipse IDE.
Create a new Scala Project and define an object with a main method as entry point.
Now create a new Java Project and add your Scala Project to the new ones buildpath. Additionally add the scala-library.jar to the Java project.
Now create a Wrapper class in the java project which calls your entry point class from the scala lib. Run the wrapper class to create a eclipse run configuration and test if you can call the scala project.
Use the Export->Java->Runnable JAR file, Wizard now on the wrapper project.The eclipse run configuration will be used as entrypoint into the JAR. Depending on your needs you may want to :
extract required libraries into generated JAR
or
Package required libraries into generated JAR
Finally you get a complete packaged JAR which you can use like this:
java - jar wrapped.jar
For me, it was relatively straightforward.
Develop and test the project using the scala IDE (or eclipse for java).
once ready, generate the jar for the project using file -> export method.
for submitting the spark (i was writing something for spark), i just had to mention --class option for specifying the main class for the jar.
hope to help.

How do you create a jar file of classes that can be re-used by multiple Android projects?

Here are the steps I have followed so far, with no luck. I am extremely new to Java projects so I suspect I may be missing something obvious.
Using Eclipse, I have created a simple Java project called TestSDK, created within that a package called com.test.testsdk, and within that the following class:
package com.test.testsdk;
public class TestClass {
public void TestMethod() {
}
}
This compiles without errors or warnings.
I then export this as a JAR file (TestSDK.jar) using Eclipse and the standard export options (export generated class files and resources, compress the contents of the JAR, generate manifest file). I have tried both sealing and not sealing the JAR which makes no difference.
I then create a new Android application project from File->New->Project in the Wizards list. This compiles and runs without warnings or errors on both the Android emulator and my test device (I get the hello world message).
I then add a reference to my TestSDK.jar file (using a variety of different methods as I will expand on shortly), import it into the main (and only) Android activity, and try to instantiate my TestClass and call TestMethod on it, like so:
package com.apptest.mobilesdktestapp;
import android.app.Activity;
import android.os.Bundle;
import com.test.testsdk.TestClass;
public class MobileSDKTestAppActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TestClass test = new TestClass();
test.TestMethod();
}
}
This compiles fine without warnings or errors. When trying to run it on the emulator or the device, however, I get the following error in my LogCat window:
AndroidRuntime Uncaught handler: thread main exiting due to uncaught exception
AndroidRuntime java.lang.NoClassDefFoundError: com.test.testsdk.TestClass
Searching the web for the NoClassDefFoundError results in a lot of suggestions on how to import the JAR file such that the class path is correct. As a result, I have tried all of the following methods of importing the JAR file:
"Add External JARs..." from the Libraries tab of Java Build Path in the project properties, followed by checking (or not checking, I tried both) the JAR in the Order and Export tab. Also tried moving the JAR to the top of the Order and Export list, which made no difference.
Creating a "libs" folder in the project, and adding the JAR there. I confirmed that the JAR is then also added to the "Android Dependencies" thing in the project list. Also tried right-clicking the JAR file and selecting Build Path->Add to Build Path which made no difference.
Moving the JAR into my Android Application project directory and doing "Add JARs..." instead of external JARs as in step 1, also all permutations of exporting or not and moving it to the top of the order list or not.
I have subsequently downloaded other 3rd party SDKs that are packaged as JAR files and included those in the very same Android application project, and those have all worked fine using any of the 3 methods above (I am able to instantiate classes from those SDKs and use them without error), which leads me to believe I am missing something or doing something wrong in my TestSDK project and/or class which is preventing it from being used in the Android Application project.
As I said, I am very new to Java, so I'm hoping it's something simple that I've overlooked. Any help or guidance would be appreciated.
If you are on R17 or higher version of the Android tools and ADT in Eclipse, then the first sentence of #2 is the correct answer; everything else listed in your question is unnecessary at best or harmful at worst.
I would recommend that you create a clean project, create the test activity, create the libs/ folder, copy the JAR into the libs/ folder, code to the JAR's API, compile, and run. If that works, then your original project still has stuff lingering around from your previous efforts that is causing you grief. If it fails, then something fairly strange is going on. The JAR itself is presumably fine, otherwise you would get compile errors.
I think I figured out what the issue was (or at least how to fix the issue, I'm still not 100% sure what I did)--
When I created the original TestSDK java project, I let it target the default JRE in the project creation dialog (jre7).
Checking the project properties of a new Android Application project, the Java Compiler section has "Compiler compliance level" set to 1.5. So, I tried recreating my TestSDK project again, but told it to use J2SE-1.5 as the execution environment instead of the defalut jre7.
After doing this, exporting the JAR and importing it to the Android project's libs directory, I am now able to instantiate the TestSDK classes and use them just fine without the NoClassDefFoundError exception.
Best guess is that the Android application was being compiled against an older version of the JRE than my TestSDK class (which I believe was targeting JavaSE-1.7), causing the issues. Matching the two versions up has solved it.

Can't run JUnit in Eclipse Indigo

I create simple Java Application project with src and test folders. src folder consists of the class which I test and test folder consists of the class where I describe test. When I start testing, using JUnit 4 I get: Could not find the main class: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. Program will exit. error. All JAR files are presented in my project.
What did I miss in my project?
That is a class that is part of the Eclipse plugin, it sounds like you have a corrupted installation of Eclipse. Try with a fresh installation.
Far shot: is the test-folder containing the .java-files for your tests also declared as a source-folder in the project's Build Path settings?
All JUnit tests should be written as follows:
#Test //This is very very important
public void testAbc()
{
//Test Abc() here....
}

Categories