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.
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.
After more than half a day of searching and experimenting, I finally figured it out according to the FAQ install
After adding JUnit to the classpath:export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit.jar
, I finally could run the sample test successfully.
Then I ran my sample test successfully.
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.
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)
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:
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
:And this is the content of
CalculatorTest.java
:Now you need to compile the
Calculator.java
.Then you need to compile the
CalculatorTest.java
.Now you execute the tests with command below.
So you should see an output like this:
I followed the instructions from this page:
junit.org/junit5/docs/current/user...
It's so cool! I'll try it, thank you so much for you detail steps
The question arises why would you like to run the tests via cli instead in combination with you build (for example Maven).. ?
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