Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we test plain Java code in Android Studio?

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?

like image 949
Jim Avatar asked Sep 02 '25 06:09

Jim


2 Answers

To create a run/debug configuration for a class or method in your Java code, follow these steps:

  1. Open a project in Android or Project view.
  2. Open a Java file in the Code Editor.
  3. Select a class or method in the code, and then press Ctrl+Shift+T (⌘⇧T).
  4. Select Create New Test from the menu that appears.
  5. In the Create Test dialog, optionally change the field values, and then click OK.
  6. 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.

  1. 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.
  2. 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

like image 112
Luc Geven Avatar answered Sep 04 '25 21:09

Luc Geven


Just right click on your class and select Run YourClassName.main()

like image 24
antonio Avatar answered Sep 04 '25 21:09

antonio