0

So I'm trying to implement Mockito and JUnit5, but I can't seem to make it work. It always shows this error:

enter image description here

This is the test file:

package test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import javax.swing.*;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import com.mycompany.helloworldapp.Activity1;
import utils.TestUtils;

@ExtendWith(MockitoExtension.class)
public class Activity1Test {
  @InjectMocks
  Activity1 activity1;

  @Mock
  List<Integer> list;
  
  @Test
  public void testUpdateTextButton() {
    when(list.add(100)).thenReturn(true);
  }
}

Libraries used:

  1. junit-platform-console-standalone-1.8.2
  2. mockito-core-4.1.0
  3. mockito-junit-jupiter-4.1.0

This is the folder structure:

enter image description here

And these are the commands I used in the Terminal:

1. Compiling Java files except Test files:

cd src; javac -target 17 -source 17 com/mycompany/helloworldapp/Activity1.java

cd src; javac -target 17 -source 17 utils/TestUtils.java

2. Compiling Test files:

cd src; javac -target 17 -source 17 -sourcepath . -classpath libraries/JUnit-Standalone.jar:libraries/Mockito-Core.jar:libraries/Mockito-JUnit-Jupiter.jar test/Activity1Test.java

3. Running the test:

cd src; java -jar libraries/JUnit-Standalone.jar --classpath . --scan-classpath

Nimantha
  • 6,405
  • 6
  • 28
  • 69
David Maranga
  • 149
  • 1
  • 2
  • 12
  • Check please https://www.baeldung.com/mockito-junit-5-extension – Valijon Dec 07 '21 at 08:43
  • @Valijon I've already gone through the process of what the site gave (e.g., installing other libraries like Surefire Plugin), but it still gave me the same error as the one I've posted in here. – David Maranga Dec 07 '21 at 08:46
  • What IDE do you use, if any? – Adriaan Koster Dec 07 '21 at 09:05
  • What are you trying to run exactly? Is your `Activity1.class` located in `Junit-Standalone.jar`? Is your jar file an executable jar? Please see Solution 3 on how to run a Jar file, while also providing required libraries.... https://timjansen.github.io/jarfiller/javabasics/jar/libraries.xhtml – JCompetence Dec 07 '21 at 09:06
  • https://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file and see that for how to make an executable jar. – JCompetence Dec 07 '21 at 09:06
  • @AdriaanKoster Visual Studio Code – David Maranga Dec 07 '21 at 09:06
  • And see this also, https://www.baeldung.com/java-run-jar-with-arguments Your issue is NOT dependencies....its that you are not putting those dependencies on the classpath as you want to run your JAR, plus I have a feeling that you are not running the correct jar that has your TEST files... – JCompetence Dec 07 '21 at 09:07
  • 1. Mockito-Junit-Jupiter is not on your classpath when you run the test. `--classpath` is not recursive. 2. Do you have any specific reason for not using a build tool (Maven and Gradle are popular choices, but there are more). 3. Your project layout is highly unconventional. It is not typical for java projects to mix java and class files in the same directory. It is not common to have 3rd party libs under src. – Lesiak Dec 07 '21 at 09:08
  • Indeed @Lesiak I agree. Maybe this answer of mine can give some hints for that: https://stackoverflow.com/questions/1510291/eclipse-java-project-folder-organization/1510717#1510717 Manage the dependencies using Maven or Gradle and use an IDE which supports them. – Adriaan Koster Dec 07 '21 at 09:11
  • @everyone I've decided to use Maven now, and it works perfectly fine already. Also, the project layout is now much better than before. Thanks for the suggestion and the help! :) – David Maranga Dec 07 '21 at 13:14
  • 1
    Consider adding the solution as an answer and accepting it. Thanks! – João Dias Dec 08 '21 at 01:23

0 Answers0