DEV Community

WangGithub0
WangGithub0

Posted on

Adding testing for my java project

This week I leaned and practise to add testing for my project ConvertTxtToHtml.

First, I chose a testing framework for my java project. JUnit is the most pupular testing framework for java.

Image description

Because I only wanna use the cmd java test, so I chose Junit4 which supports Plain-old JAR. I did test according to the Getting started document.
But when I ran the javac -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar CalculatorTest.java I got the error message CalculatorTest.java:4: error: package main does not exits. I tried to reorganize the folder and set Path according to others' experience 1, 2, but it still did not work.

Image description

After more than half a day of searching and experimenting, I finally figured it out according to the FAQ install

Image description

After adding JUnit to the classpath:export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit.jar, I finally could run the sample test successfully.

Image description

Then I ran my sample test successfully.

Image description

Then I wrote my First Test. This is my first time to do java testing, I chose the simplest area of my code, input parameter. I used the assertEquals to check the output of the version command.

At last, I wrote testing for the Core of my app. I created a textfile and using processFile function to create the html result file, then read the file to check the basic html element.

Luckily my two tests are both pasted and I updated the whole testing process in my CONTRIBUTING file.

Image description

By doing this testing, I know strengthening my knowledge of Java fundamentals was crucial to improving my programming skills. Understanding Java basics and becoming familiar with various types of errors are important steps in improving my problem-solving skills. Embracing these elements not only gave me a solid foundation, but also equipped me with the skills to effectively tackle complex coding challenges.

Top comments (4)

Collapse
 
wldomiciano profile image
Wellington Domiciano

Nice post. Congratulations for your progress.

It's also possible to run tests from the command line using JUnit 5. If you're interested, here are the instructions:

Let's say you have this structure:

my-project
├── lib
│   └── junit-platform-console-standalone-1.10.0.jar
└── src
    ├── main
    │   └── example
    │       └── Calculator.java
    └── test
        └── example
            └── CalculatorTest.java
Enter fullscreen mode Exit fullscreen mode

You can download junit-platform-console-standalone-1.10.0.jar from this page:

repo1.maven.org/maven2/org/junit/p...

This is the content of Calculator.java:

package example;

public class Calculator {
  public int evaluate(final String expression) {
    int sum = 0;

    for (final String summand : expression.split("\\+"))
      sum += Integer.valueOf(summand);

    return sum;
  }
}
Enter fullscreen mode Exit fullscreen mode

And this is the content of CalculatorTest.java:

package example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CalculatorTest {
  @Test
  public void evaluatesExpression() {
    final Calculator calculator = new Calculator();
    final int sum = calculator.evaluate("1+3+3");
    Assertions.assertEquals(6, sum);
  }
}
Enter fullscreen mode Exit fullscreen mode

Now you need to compile the Calculator.java.

javac -d build src/main/example/Calculator.java
Enter fullscreen mode Exit fullscreen mode

Then you need to compile the CalculatorTest.java.

javac -d build -cp 'build:lib/*' src/test/example/CalculatorTest.java
Enter fullscreen mode Exit fullscreen mode

NOTE: It's also possible compile CalculatorTest.java and Calculator.java in one step with this command:

javac -d build -cp 'lib/*' -sourcepath src/main src/test/example/CalculatorTest.java

Now you execute the tests with command below.

java -cp 'build:lib/*' org.junit.platform.console.ConsoleLauncher execute --scan-classpath
Enter fullscreen mode Exit fullscreen mode

So you should see an output like this:

Image description

I followed the instructions from this page:

junit.org/junit5/docs/current/user...

Collapse
 
wanggithub0 profile image
WangGithub0

It's so cool! I'll try it, thank you so much for you detail steps

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise

The question arises why would you like to run the tests via cli instead in combination with you build (for example Maven).. ?

Collapse
 
wanggithub0 profile image
WangGithub0

Yes, this is a good question 😂

When I first started this project, I only needed a small function, so I only wrote a java file.
Next time I will initialize a complete java project