My tests are being ignored in IntelliJ.

Hey, I'm trying ouy Java with spring boot. I have a simple guess the word game, with the word being a cocktail that is retreived with an API. User inputs their guess, and dpeending whether or not they are correct they can try again or quit. I wanted to write some tests and ran into a problem that my tests have the ignored status for whatever reason. The below code for example should test for the getcocktail method being called once for example. Tried googling but little info on the issue.
import com.google.gson.JsonObject;
import com.ridango.game.ApiService;
import com.ridango.game.GameService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Scanner;

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

public class GameServiceTest {

@InjectMocks
private GameService gameService;

@Mock
private ApiService apiService;

@Mock
private Scanner scanner;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this); // Initialize mocks
}

@Test
public void testStartGameCorrectGuess() {
JsonObject mockCocktail = new JsonObject();
mockCocktail.addProperty("strDrink", "Margarita");
when(apiService.GetCockTail()).thenReturn(mockCocktail);
String simulatedInput = "Margarita\nno\n";
InputStream inputStream = new ByteArrayInputStream(simulatedInput.getBytes());
System.setIn(inputStream);
gameService.StartGame();

verify(apiService, times(1)).GetCockTail();
}
}
import com.google.gson.JsonObject;
import com.ridango.game.ApiService;
import com.ridango.game.GameService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Scanner;

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

public class GameServiceTest {

@InjectMocks
private GameService gameService;

@Mock
private ApiService apiService;

@Mock
private Scanner scanner;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this); // Initialize mocks
}

@Test
public void testStartGameCorrectGuess() {
JsonObject mockCocktail = new JsonObject();
mockCocktail.addProperty("strDrink", "Margarita");
when(apiService.GetCockTail()).thenReturn(mockCocktail);
String simulatedInput = "Margarita\nno\n";
InputStream inputStream = new ByteArrayInputStream(simulatedInput.getBytes());
System.setIn(inputStream);
gameService.StartGame();

verify(apiService, times(1)).GetCockTail();
}
}
No description
52 Replies
JavaBot
JavaBot•3mo ago
⌛ This post has been reserved for your question.
Hey @Luxenbau! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st•3mo ago
Where does the "You guessed right" text etc come from? Can you run "all tests" in some way instead of running the specific test method? Can you try using a debugger?
JavaBot
JavaBot•3mo ago
It looks like you are having issues with debugging or issues that can be solved using a debugger. Check out this article on dev.java to see how debugging works and how to use a debugger. This Stack Overflow question and its answers also explain debugging in general. These links describe how to use the debugger in some IDEs: • Debugging in IntelliJ • Debugging in Eclipse
Luxenbau
LuxenbauOP•3mo ago
This comes up when I run the test. This is actually the sysouts inside of the game, but they are triggered when I run the test... Im new to UnitTesting ... And I tried running the entire file, or all tests, its the same thing. Tests are ignored
dan1st
dan1st•3mo ago
Do you have both JUnit 4 and 5 on the classpath?
Luxenbau
LuxenbauOP•3mo ago
You mean in gradle? I have for tests:
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

testImplementation 'org.mockito:mockito-core:5.13.0'

testImplementation 'net.bytebuddy:byte-buddy:1.15.1'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

testImplementation 'org.mockito:mockito-core:5.13.0'

testImplementation 'net.bytebuddy:byte-buddy:1.15.1'
JavaBot
JavaBot•3mo ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
dan1st
dan1st•3mo ago
weird
Luxenbau
LuxenbauOP•3mo ago
yes I have no clue why =/
dan1st
dan1st•3mo ago
In your screenshots, there are a few icons above the test results see the tick icon?
Luxenbau
LuxenbauOP•3mo ago
You mean the show only passed tests icon?
dan1st
dan1st•3mo ago
sure it's passed only? What happens when enabling it?
Luxenbau
LuxenbauOP•3mo ago
The tick mark is the show passed. The crossed o is the show ignored
dan1st
dan1st•3mo ago
yes I know
Luxenbau
LuxenbauOP•3mo ago
nothing. if i disable the show ignored, the Test Results disappear from the left pane
dan1st
dan1st•3mo ago
ok Can you try making the test fail in some way? by temporarily changing some code if you do that, does it still show as disabled?
Luxenbau
LuxenbauOP•3mo ago
I tried changing verify to some ridiculous number but it still gets ignored
john
john•3mo ago
a simple test like: @Test
public void testIntegerEquality() {
int expected = 5;
int actual = 2 + 3; // This could be a result from your service method

assertEquals(expected, actual, "The actual value should match the expected value.");
}
public void testIntegerEquality() {
int expected = 5;
int actual = 2 + 3; // This could be a result from your service method

assertEquals(expected, actual, "The actual value should match the expected value.");
}
however works
This message has been formatted automatically. You can disable this using /preferences.
Luxenbau
LuxenbauOP•3mo ago
so its something with the test
dan1st
dan1st•3mo ago
so other tests work as expected? Can you try making the problematic test fail?
Luxenbau
LuxenbauOP•3mo ago
if i write tests that do not touch the GameService class. I think its something with it
dan1st
dan1st•3mo ago
Did you use @Ignore or @Disabled in the past? Can you step through the test with a debugger? Does anything in it throw any exception?
Luxenbau
LuxenbauOP•3mo ago
Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
Connected to the target VM, address: 'localhost:53292', transport: 'socket'
WARNING: A Java agent has been loaded dynamically (/Users/mkerd/.gradle/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy-agent/1.12.11/78439af17fa17e4336242d1c4a6aa36282c89139/byte-buddy-agent-1.12.11.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
Guess the name of the cocktail: _________Margarita
You have : 5 attempts left! Type 'Exit' without quotes whenever you wish to quit.
Enter the name of the cocktail:
You guessed right! You scored: 5
Try again? (yes/no)
Game ended!
Your final score is: 5
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
Disconnected from the target VM, address: 'localhost:53292', transport: 'socket'
> Task :test
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.5/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 3s
4 actionable tasks: 2 executed, 2 up-to-date
Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
Connected to the target VM, address: 'localhost:53292', transport: 'socket'
WARNING: A Java agent has been loaded dynamically (/Users/mkerd/.gradle/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy-agent/1.12.11/78439af17fa17e4336242d1c4a6aa36282c89139/byte-buddy-agent-1.12.11.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
Guess the name of the cocktail: _________Margarita
You have : 5 attempts left! Type 'Exit' without quotes whenever you wish to quit.
Enter the name of the cocktail:
You guessed right! You scored: 5
Try again? (yes/no)
Game ended!
Your final score is: 5
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
Disconnected from the target VM, address: 'localhost:53292', transport: 'socket'
> Task :test
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.5/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 3s
4 actionable tasks: 2 executed, 2 up-to-date
the warnings should not be affecting the test at all. maybe i mocked it somehow wrong but it just gets ignored for whatever reason
dan1st
dan1st•3mo ago
.
Luxenbau
LuxenbauOP•3mo ago
those are results of the debugger? you mean the one at top right of IntelliJ?
dan1st
dan1st•3mo ago
No. That's just stdout and stderr
JavaBot
JavaBot•3mo ago
It looks like you are having issues with debugging or issues that can be solved using a debugger. Check out this article on dev.java to see how debugging works and how to use a debugger. This Stack Overflow question and its answers also explain debugging in general. These links describe how to use the debugger in some IDEs: • Debugging in IntelliJ • Debugging in Eclipse
dan1st
dan1st•3mo ago
I am talking about actually stepping through the code
Luxenbau
LuxenbauOP•3mo ago
hold on need to figure out how this works
Luxenbau
LuxenbauOP•3mo ago
No description
Luxenbau
LuxenbauOP•3mo ago
so this is when i break at inputstream
dan1st
dan1st•3mo ago
can you step over the lines in your test one after the other? Is there anything throwing an exception?
Luxenbau
LuxenbauOP•3mo ago
No description
Luxenbau
LuxenbauOP•3mo ago
hmm, when i set breakpoint at the verify line, test is already ignored
dan1st
dan1st•3mo ago
maybe something in your code exits in some way Are you using System.exit() anywhere?
Luxenbau
LuxenbauOP•3mo ago
hmm, the exitgame method uses it yes wait, does it exit the application and test?
dan1st
dan1st•3mo ago
that kills all tests
Luxenbau
LuxenbauOP•3mo ago
no way let me check this
dan1st
dan1st•3mo ago
there are maybe some ways around it but it's annoying
Luxenbau
LuxenbauOP•3mo ago
wow, i just temp disabled it and now it passes i did not know the sysexit also affects the test...
dan1st
dan1st•3mo ago
tests are also just java programs
Luxenbau
LuxenbauOP•3mo ago
and it terminate the jvm right
dan1st
dan1st•3mo ago
kinda yes
Luxenbau
LuxenbauOP•3mo ago
ooohhhh, so now i need to figure out how to write tests, since my default public method calls it thanks for the help man
dan1st
dan1st•3mo ago
The best way is not using System.exit() in your code
Luxenbau
LuxenbauOP•3mo ago
hmm well need to close the program somehow without throwing exceptions then. i mean its a standalone program so sysexit seems to make sense
dan1st
dan1st•3mo ago
if you can't do that, you might be able to mock System.exit well in your application you probably have some sort of loop checking for input etc you could also make sure that stops
Luxenbau
LuxenbauOP•3mo ago
yea i do have a while loop
dan1st
dan1st•3mo ago
you could also make sure that loop stops looping when you want to exit
Luxenbau
LuxenbauOP•3mo ago
yeah, i think so.
Scanner scanner = new Scanner(System.in);
boolean keepPlaying = true;
while (keepPlaying) {
String cocktailName;
revealedKeys.clear();
Scanner scanner = new Scanner(System.in);
boolean keepPlaying = true;
while (keepPlaying) {
String cocktailName;
revealedKeys.clear();
i insta call exit game whenever user exits or says no. i can just cancel the loop instead. wont exit the app but i can get the tests working can instead just change the keepplaying value most importantly i now know the issue 😄 thanks a lot man idk if i can give u rep or karma here or something @dan1st | Daniel
JavaBot
JavaBot•3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot•3mo ago
Post Closed
This post has been closed by <@290601931611701249>.
Want results from more Discord servers?
Add your server