uKnit Quickstart

The preferred way to use uKnit is to add it as a dependency in your project’s pom.xml. We explain the steps involved with uknit-quickstart example, and you can use uKnit in your project in the same way.

uKnit is also distributed as release zip which contains uKnit core and a Quickstart example projects. For whatever reason, you don’t want to add uknit dependency to your project’s pom then you have to install uKnit from the release zip. Installation process is tad bit complicated, and it is explained in Install uKnit. We suggest you to stick with the easier option of adding uKnit dependency as explained below.

The uKnit Quickstart Project contains multiple branches main, maven, gradle etc., and this tutorial uses quickstart with maven build. The setup for Eclipse and IntelliJ IDEA are explained below, and the procedure is more or less same for other IDE.

In case you are interested in Gradle build, then refer Quickstart Gradle Build.


    uKnit needs Java 11 or higher, ensure that 
    Java 11 or higher is installed in the IDE.
    

Eclipse

In a terminal, cd to your Eclipse workspace, and clone the uknit-quickstart project from Github.

    cd <your eclipse workspace folder>  # normally $HOME/eclipse-workspace
         
    git clone git@github.com:maithilish/uknit-quickstart.git
    cd uknit-quickstart
    git checkout maven                  # checkout maven branch

Don’t forget to checkout maven branch after the clone; the main branch doesn’t contain any project!

Start Eclipse IDE and import uknit-quickstart as a Maven project with Import -> Maven -> Existing Maven Project. In the Root Directory field, browse and select the uknit-quickstart folder in the eclipse workspace; click finish to import the project. Next, edit uknit-quickstart/pom.xml and set the uknit-core dependency version.

    <dependency>
        <groupId>org.codetab.uknit</groupId>
        <artifactId>uknit-core</artifactId>
        <version>1.0.0-M2</version>
    </dependency>

Use project context menu Maven -> Update Project to rebuild the project. To run uKnit create a Run configuration. Select the uknit project in the Project or Package Explorer and invoke Run -> Run Configuration and add New Launch Configuration for Java Application. Enter fields as below and click Apply to save the configuration.

Main Tab
    Name: uknit
    Project: select uknit-quickstart
    Main Class: org.codetab.uknit.core.Uknit
uKnit Eclipse run configuration

Now run the uknit and it throws error “CriticalException: src file not found”, which indicates that installation is fine. In case of any other error, refer Troubleshooting section below.

Configure uKnit

The uknit-quickstart project contains uknit.properties located under uknit-quickstart/src/main/resources folder which configures the input java class file for which test has to be generated. Open the file eclipse-workspace/uknit-quickstart/src/main/resources/uknit.properties,

    uknit.source.base=/home/foo/eclipse-workspace/uknit-quickstart
    uknit.source.dir=src/main/java
    uknit.source.package=org.codetab.uknit.quickstart
    uknit.source.clz=Quickstart

The uknit.source.base is the absolute path to the quickstart project. For example if your workspace is /home/xyz/eclipse-workspace where you have extracted the uknit release, then set it as,

    uknit.source.base=/home/xyz/eclipse-workspace/uknit-quickstart

Finally, run uknit and if all goes well then test class QuickstartTest.java is generated under the folder uknit-quickstart/src/test/java/org/codetab/uknit/quickstart. Open the test class and hit Ctrl+Shift+O to auto import the required import statements which should resolve all the errors. Finally run the generated test class as JUnit test.

To generate tests for your project follow these steps,

  • Create a Run Configuration as explained above but in Project field browse and select your project.
  • Copy uknit.properties file from uknit-quickstart to your-project/src/main/java/resources folder and update the properties; point it to your project source folder and package. Alternatively you can create a new file and add required properties as detailed above.
  • Add org.codetab.uknit:uknit-core:1.0.0-M2 dependency to your project’s pom.xml.

IntelliJ IDEA

Open a terminal, cd to $HOME/IdeaProjects folder and clone uknit-quickstart project from Github with,

    cd <path of intellij project folder>   # normally $HOME/IdeaProjects
         
    git clone git@github.com:maithilish/uknit-quickstart.git
    cd uknit-quickstart
    git checkout maven          # checkout maven branch

Don’t forget to checkout maven branch after the clone; the main branch doesn’t contain any project!

Start IntelliJ IDE and add uknit-quickstart as a project with New -> Add Project from existing sources. In the Import dialog, choose the uknit-quickstart folder you have just extracted and in the Import Project dialog choose Import Project From External Model and Maven; click Create to finish the import. Next, edit uknit-quickstart/pom.xml and set the uknit-core dependency version.

    <dependency>
        <groupId>org.codetab.uknit</groupId>
        <artifactId>uknit-core</artifactId>
        <version>1.0.0-M2</version>
    </dependency>

Click on Maven button on right side tool bar to open the Maven tool window and click Reload to reload all maven projects. Now you should see dependencies of quickstart are listed under uknit project dependencies.

uKnit IntelliJ Maven Tool Window

To run uKnit we need a run configuration, and to create it go to Run -> Edit Configuration and click + to Add New Configuration and choose type as Application. Enter fields as shown below and click OK to save the configuration.

    Name: uknit        
    Main class: org.codetab.uknit.core.Uknit     
uKnit IntelliJ run configuration

Now run the uknit and it throws error “CriticalException: src file not found”, which indicates that installation is fine. In case of any other error, refer Troubleshooting section below.

Configure uKnit

Here we use /home/xyz/IdeaProjects/uknit-quickstart as the IntelliJ project folder. Change it to the folder where you have cloned uKnit-quickstart. The uknit-quickstart project contains uknit.properties located under uknit-quickstart/src/main/resources folder which configures the input java class file for which test has to be generated. Open the file uknit.properties,

    uknit.source.base=/home/foo/eclipse-workspace/uknit-quickstart 
    uknit.source.dir=src/main/java
    uknit.source.package=org.codetab.uknit.quickstart
    uknit.source.clz=Quickstart

The uknit.source.base is the absolute path to the quickstart project. If uknit module folder is /home/xyz/IdeaProjects/uknit-quickstart then set it as

    uknit.source.base=/home/xyz/IdeaProjects/uknit-quickstart    

Run uknit and if all goes well then test class QuickstartTest.java is generated under the folder uknit-quickstart/src/test/java/org/codetab/uknit/quickstart.

uKnit generated test class doesn’t come with all the required imports. Use IntelliJ Auto Imports feature to fix the imports. After fixing the imports, run the generated test class as JUnit test.

To generate tests for your IntelliJ module follow these steps,

Above config is good for Maven projects, in case your project is a Gradle project then refer Quickstart Gradle Build. For any other build tool see uKnit Configuration for details.

Troubleshoot Installation

The run throws org.codetab.uknit.core.Uknit class not found error. This happens if org.codetab.uknit:uknit-core:1.0.0-M2 dependency is not in the classpath, resolve it as follows,

  • Eclipse - add org.codetab.uknit:uknit-core:1.0.0-M2 dependency to project’s pom.xml.
  • IntelliJ - add org.codetab.uknit:uknit-core:1.0.0-M2 dependency to modules’s pom.xml.

The run throws Properties file not found error. This happens if resources folder is not in the classpath, resolve it as follows,

  • Eclipse - go to Maven -> Update Project and rebuild the project.
  • IntelliJ - open Maven Tool Window and reload all Maven Projects.

The run throws src file not found error. This means you have not updated in uknit.source.base property in uknit.properties. Set is as follows,

  • Eclipse - if your eclipse workspace is /home/xyz/eclipse-workspace/uknit-quickstart then set uknit.source.base=/home/xyz/eclipse-workspace/uknit-quickstart
  • IntelliJ - if uknit-quickstart is cloned to /home/xyz/IdeaProjects/uknit-quickstart then set uknit.source.base=/home/xyz/IdeaProjects/uknit-quickstart