使用Mockito与JUnit的Java测试突然无法运行的问题求助
使用Mockito与JUnit的Java测试突然无法运行的问题求助
各位大佬好,我最近碰到个头疼的问题:我用Mockito和JUnit给我的石头剪刀布控制台应用写测试,之前一直运行得好好的,结果突然所有测试都没法正常执行了,而且完全没有任何错误提示!在IntelliJ IDEA里看,左下角的测试项都标着白色的"X",我完全摸不着头绪不知道哪里出了问题。
先给大家看看我的pom.xml里的测试依赖配置:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>5.14.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency>
下面是我的测试类代码:
package com.f_rafael.junitproject_lucas_moy; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.Random; import java.util.Scanner; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class GameTest { public static final int OPTION_ROCK = 0; public static final int OPTION_PAPER = 1; public static final int OPTION_SCISSORS = 2; private ByteArrayOutputStream out; @InjectMocks private Game game; @Mock // 这个对象会被注入到带有@InjectMocks注解的对象中 Scanner scanner; @Mock Random random; @Before public void setup(){ out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); } @org.junit.Test public void when_writeQuit_then_exitGame(){ when(scanner.nextLine()).thenReturn("Quit"); game.play(); Assert.assertTrue(out.toString().contains("Let's play Rock")); } @org.junit.Test public void when_chooseRock_then_beatsScissors(){ when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); when(random.nextInt(3)).thenReturn(OPTION_SCISSORS); game.play(); Assert.assertTrue(out.toString().contains("Computer chose scissors")); Assert.assertTrue(out.toString().contains("wins:1")); } @org.junit.Test public void when_chooseScissors_then_beatsPaper(){ when(scanner.nextLine()).thenReturn("Scissors").thenReturn("Quit"); when(random.nextInt(3)).thenReturn(OPTION_PAPER); game.play(); Assert.assertTrue(out.toString().contains("Computer chose paper")); Assert.assertTrue(out.toString().contains("wins:1")); } @org.junit.Test public void when_choosePaper_then_beatsRock(){ when(scanner.nextLine()).thenReturn("Paper").thenReturn("Quit"); when(random.nextInt(3)).thenReturn(OPTION_ROCK); game.play(); Assert.assertTrue(out.toString().contains("Computer chose rock")); Assert.assertTrue(out.toString().contains("wins:1")); } @Test public void when_chooseRockAndComputerChoosePaper_then_lose(){ when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); when(random.nextInt(3)).thenReturn(OPTION_PAPER); game.play(); Assert.assertTrue(out.toString().contains("Computer chose paper")); Assert.assertTrue(out.toString().contains("loses:1")); } }
这个应用就是个简单的控制台石头剪刀布游戏,我用的开发工具是IntelliJ IDEA。有没有大佬遇到过类似的情况,能给我指条明路吗?
内容来源于stack exchange
相关产品推荐
相关产品推荐

