javatestingjunitdependenciesdevelopment-environment

Can someone help me set up junit correctly? My program is not compiling


My code:

import org.junit.Test;
import static org.junit.Assert.*;

public class AListTest {
    @Test
    public void testEmptySize() {
        AList L = new AList();
        assertEquals(0, L.size());
    }

    @Test
    public void testAddAndSize() {
        AList L = new AList();
        L.addLast(99);
        L.addLast(99);
        assertEquals(2, L.size());
    }
    ...rest of testing
}

I have both junit 4.13.2 and also 5.9.1 jupiter included in my dependencies because I was confused about what package to put in my project stucture/dependencies. I am following this class by the way and I am trying to follow the commands at this part of this video... The commands I am trying to run are

javac *.java

-and also-

time java speedtestAList 

I have tried compiling with every permutation of those packages and none in my dependencies. I keep getting the same errors no matter what:

AListTest.java:1: error: package org.junit does not exist
import org.junit.Test;
                ^
AListTest.java:2: error: package org.junit does not exist
import static org.junit.Assert.*;
                       ^
SpeedTestSLList.java:1: error: package org.junit does not exist
import org.junit.Test;
                ^
AListTest.java:9: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class AListTest
AListTest.java:15: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class AListTest
AListTest.java:24: error: cannot find symbol
    @Test
....on like that through my file

I don't know why testing is always so hard for me. I use C# normally and Xunit and moq give me so so much trouble too. Can someone help me understand what I am doing wrong?

I have tried including different dependencies and using ai to troubleshoot. I have asked a couple friends who have been unable to help so far. I googled of course. I tried to rebuild the project every time I changed dependencies. I tried to "invalidated the caches" in intellij.


Solution

  • You need to add the .jar file for JUnit to your build and execution process.

    javac -classpath <path_to_junit.jar>:/your_system_libraries *.java
    

    then (assuming you have a void main(String args[]) defined somewhere that executes the junit tests, otherwise you need to call a different execution engine)

    java -classpath .:<path_to_junit.jar>:/your_system_libraries AListTest
    

    In practice, nobody really executes on the command line like this. Virtually everyone uses a maven or gradle configuration to manage the build and execution process, so that you don't have to worry about managing downloads, and classpaths, and versions and stuff.

    Once you install maven, you can add a configuration that looks like this to identify that you want to use junit:

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
    

    which instructs maven to add this to the build (during the test phase, in this case, as noted by the scope.

    Then, build and execution is much simpler:

    mvn package
    mvn test
    

    I recommend one of the following tools:

    There are others, but these two are most commonly used in my experience.