If I have a project in Eclipse, I can at any time create a class with a main and test anything having all the classes and libraries of my project available.
How do I do something like that in Android Studio?
If I have a big project and I would like to test a small method in isolation, how would I do that without needing to run the emulator or an Activity, etc?
To create a run/debug configuration for a class or method in your Java code, follow these steps:
Open a project in Android or Project view.
Open a Java file in the Code Editor.
Select a class or method in the code, and then press Ctrl+Shift+T (⌘⇧T).
Select Create New Test from the menu that appears.
In the Create Test dialog, optionally change the field values, and then click OK.
In the Choose Destination Directory dialog, select androidTest to create an instrumented test or test to create a local unit test. Click OK.
The new test appears in the Project window in the corresponding test source set.
To run the test, do one of the following:
In the Project window, right-click the test and select Run or Debug.
In the Code Editor, right-click a class definition in the test file and select Run or Debug to test all methods in the class.
In the Code Editor, right-click a method name in the test file and select Run or Debug to test just that method.
In the Code Editor, click Play in the left margin next to a class or method and select Run or Debug.
To save the configuration, select Save from the Select Run/Debug Configuration drop-down list within the toolbar.
The drop-down list is to the left of Run and Debug ; for example, .
Alternatively, right-click the item again and select Save.
Or, select the configuration in the Run/Debug Configurations dialog and click Save Configuration on the upper lef
SOURCE
Just right click on your class and select Run YourClassName.main()
If The Class you are using only use plain old Java no android.* You can make a Run Configuration just like you would do in Eclipse. Otherwise You need to Write JUnit tests and run them on the device.
Personally, I have a separate small, pure Java module that's mostly for that purpose and is included more or less as a library in my app. I also toss JUnit there for those classes I need to test, and Gradle executes those when the app is built.
Not sure if that applies to your scenario, as I am not sure what types of things you are testing - Java methods or Android libraries, but from what you describe, what is preventing you from running a Java class with a main method on its own? You'd right-click a class in Eclipse and select the Run option there. That also exists in Android Studio.
You can run your class file by clicking CTRL+SHIFT+F10
Related
I have an assignment for school where I need to download files from github, edit code, and run and test them. I downloaded the files into a general project folder in eclipse, but can't seem to figure out how to run the java files within eclipse. So far I've been getting by with just using the command line, but it would be much faster and more efficient to do everything within eclipse.
There are multiple java files each with a main class. They are all under the same project and I don't want to have to make individual projects for each. Most of my googling has had the answer "right click -> run as -> java project", but I am using eclipse neon and do not see that option. When I try to set up a run configuration, the project does not show up for the project field and if I manually enter it it says that the project does not exist. Any ideas on how to get this working? Thank you.
Edit- The way that I got the files from github was by clicking "File -> Import -> Projects from Git -> Clone URL -> Master (Next) -> Next ->
The simplest way to launch a Java program is to run it using a Java Application launch configuration. This launch configuration type uses information derived from the workbench preferences and your program's Java project to launch the program.
In the Package Explorer, select the Java compilation unit or class file with the main method you want to launch.
Press the Run [ The Run button ] button in the workbench toolbar or select Run > Run from the workbench menu bar. Alternatively, select Run As > Java Application in the Package Explorer pop-up menu, or select Run > Run As > Java Application in the workbench menu bar, or select Run As > Java Application in the drop-down menu on the Run tool bar button.
Your program is now launched, and text output is shown in the Console View.
I downloaded the files into a general project folder in eclipse
This is your problem - to launch java classes you want a java project
OK. It appears that I may have figured this out and have things the way that I want them. Thanks to #markspace for this link that describes a similar problem. The simple solution was to create a new java project that had the same name as the github folder that I was downloading from, then change the source from default to the local git folder where the files are located. I could then use the other recommended solutions to right click, then run as java application to run everything within eclipse. Thank you everyone for your help.
I have an application that generates an SWT UI (with a text entry field and two buttons, similar to eclipse workspace selector). I want to generate test cases for this using SWT bot. I get the application to run using java (by executing the double click event). Then my application will generate the UI of and I want to recognize this UI from SWT bot test class. How can I recognize this when the UI is up?
I have written a unit test by invoking the method that creates the UI separately, but this is an integration test in which i need to test the whole flow of the application similar to user experience.
thanks in advance
I hope you installed SWTBot ide integration from an update site like http://download.eclipse.org/technology/swtbot/releases/latest/
If you want to run the SWTBot testcase from Eclipse do following:
Open Run->Run Configurations
Find SWTBot Test node on left of the configurations dialog
Create new configuration with right click on the node.
Select your test class/method/package/folder on Test tab, choose JUnit 4
Select your application/product on Main tab.
Select the dependencies you application requires on Plugins tab. There you
go Run :)
It is highly probable you'll fail at first attempt.
In that case on Plugins tab click on "Validate Plug-ins" and make sure you get the message "No problems were detected" or something like that.
how do you compile in Eclipse? And how do you run the program using this given with the choices: Java Applet or Java Application?
If Project -> Build Automatically is enabled, you will compile when saving your work.
To run it, you have an arrow in a green circle in your toolbar (6th tool if you have a default benchmark). It will run the project using the currently showing class as a main class if it has a main method, or the last run main if it doesn't. Alternatively, you can use the keyboard shortcut CTRL+F11.
If you click on the little down arrow next to the green button, you can access to a history of classes you ran and to the Run configuration, which allows you for example to run a program with parameters or, what interrests you here, to choose to run it as an applet or an application.
Turn on the option: Project > Build Automatically
If you have a main method, run as Java Application. To create a main, type main then ctrl+space and select "main method" to have eclipse auto create it for you.
I'm developing an Eclipse RCP application and running automated GUI tests using SWTBot. When I want to run a test from within Eclipse I right click on the test method (or class), then click "Run As" -> "SWTBot Test". This then runs the test and creates a run configuration.
The problem is that when the run configuration is created for the first time, it always uses the "org.eclipse.sdk.ide" product by default (in the "Main" tab of the run configuration"). This means the test will run the Eclipse IDE product and try to run my SWTBot tests on it. I have to manually edit the run configuration to use my RCP application's product file and run it a second time.
How can I specify a default product to use for all newly-created SWTBot run configurations?
When running the test, bring up the run configuration dialog by choosing "Run as..."
In the dialog, under the tab "common", choose to make it a shared run configuration. It will now create a *.launch file in the directory you choose.
Set all other parameters as you which. The next time you want to run the SWTBot test again, you run the launch file instead.
I've been trying to understand how to start writing and running JUnit tests.
When I'm reading this article:
http://junit.sourceforge.net/doc/testinfected/testing.htm
I get the the middle of the page and they write, "JUnit comes with a graphical interface to run tests. Type the name of your test class in the field at the top of the window. Press the Run button."
I don't know how to launch this program. I don't even know which package it is in, or how you run a library class from an IDE.
Being stuck, I tried this NetBeans tutorial:
http://www.netbeans.org/kb/docs/java/junit-intro.html
It seemed to be going OK, but then I noticed that the menu options for this tutorial for testing a Java Class Library are different from those for a regular Java application, or for a Java Web App. So the instructions in this tutorial don't apply generally.
I'm using NetBeans 6.7, and I've imported JUnit 4.5 into the libraries folder. What would be the normal way to run JUnit, after having written the tests?
The JUnit FAQ describes the process from the Console, and I'm willing to do that if that is what is typical, but given all that I can do inside netbeans, it seems hard to believe that there isn't an easier way.
Thanks much.
EDIT: If I right-click on the project and select "Test" the output is:
init:
deps-jar:
compile:
compile-test:
test-report:
test:
BUILD SUCCESSFUL (total time: 0 seconds)
This doesn't strike me as the desired output of a test, especially since this doesn't change whether the test condition is true or not.
Any ideas?
One way is to right click on your project in the Projects pane and select "Tests". That will run the JUnit tests. You can also right click on the test file and select "Run Test" and that single file will be ran. The keyboard shortcuts depends on how you have your keymapping set, but you'll see them in the context menus.
You can also have NetBeans autogenerate tests for you by right clicking your source file and then "Tools > Create JUnit Tests".
Re-importing does not appear to be necessary. I had the same issue (imported project, right clicking did not bring up any JUnit test options). I took these steps, which resolved it, using NetBeans 6.8:
Add a folder called "tests" to your project.
Right-click your project and select Properties.
Select Sources.
Under Test Package Folders, click the Add Folder button, and select the "tests" folder.
Right clicking a file + Tools > Create JUnit Tests.
Once a test is created, right-clicking a file + Test File runs the test.
All the above answers are correct, but if you are using in mac little change needed.
Step 1 Write your junit class.
Step 2 Right click on the class ->Tools-> Create/Updates Tests-> Select framework as Junit-> click ok.
Step 3 Right click on the file again ->Test File.
Now it will run as junit, will show the Test Result.
Even though I've accepted an answer, I thought I should mention my difficulty, as someone else may encounter it.
When importing a project from existing sources into NetBeans, if you do not specify a folder for test packages, then NetBeans will not offer the JUnit options on the tools menu.
The only solution I found was to re-import the project. While primitive, it worked.
I had the same issue after imported a eclipse project into NetBeans.
To resolve it, I followed the above steps outlined by alangalloway, but instead of
creating a new folder, I just pointed to the imported test folder.
Maybe in future release, NetBeans can automatically recognize imported test cases.
Thanks.
Had a similar issue. In Netbeans 7.0.1, what worked for me was to locate the project.xml file (i.e. {project}/nbproject/project.xml) and change:
<test-roots/>
to:
<test-roots>
<root id="src.dir"/>
</test-roots>
(in my case the test files are in the same dir as the source dir)