Skip to content

Commit

Permalink
mockenum doesn't throw what expected
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyucheng committed Jan 22, 2025
1 parent a59de0f commit 66bac9a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion software.test/mockito-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<version>5.15.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

public enum Direction {

NORTH,
EAST,
SOUTH,
WEST;

}
NORTH,
EAST,
SOUTH,
WEST;
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package cn.tuyucheng.taketoday.mockito.mockenum;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertThrows;

class DirectionUtilUnitTest {

@Test
void givenMockedDirection_whenGetDescription_thenThrowException() {
try (MockedStatic<Direction> enumMock = Mockito.mockStatic(Direction.class)) {
final Direction unsupported = Mockito.mock(Direction.class);
Mockito.doReturn(4)
.when(unsupported)
.ordinal();
@Test
void givenMockedDirection_whenGetDescription_thenThrowException() {
try (MockedStatic<Direction> directionMock = Mockito.mockStatic(Direction.class)) {
final Direction unsupported = Mockito.mock(Direction.class);
Mockito.doReturn(4)
.when(unsupported)
.ordinal();

enumMock.when(Direction::values)
.thenReturn(new Direction[] { Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, unsupported });
directionMock.when(Direction::values)
.thenReturn(new Direction[]{Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, unsupported});

assertThrows(IllegalArgumentException.class, () -> DirectionUtils.getDescription(unsupported));
}
}
assertThrows(IllegalArgumentException.class, () -> DirectionUtils.getDescription(unsupported));
}
}

@Test
void givenUnknownDirection_whenGetDescription_thenThrowException() {
assertThrows(IllegalArgumentException.class, () -> DirectionUtils.getDescription(Direction.UNKNOWN));
}
@Test
void givenUnknownDirection_whenGetDescription_thenThrowException() {
assertThrows(IllegalArgumentException.class, () -> DirectionUtils.getDescription(Direction.UNKNOWN));
}
}

0 comments on commit 66bac9a

Please sign in to comment.