File tree Expand file tree Collapse file tree
core-java-numbers-conversions-2
src/test/java/com/baeldung/inttoshort Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ### Relevant Articles:
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
2+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-conversions.0.0.xsd" >
4+ <modelVersion >4.0.0</modelVersion >
5+ <artifactId >core-java-numbers-conversions-2</artifactId >
6+ <packaging >jar</packaging >
7+ <name >core-java-numbers-conversions-2</name >
8+
9+ <parent >
10+ <groupId >com.baeldung.core-java-modules</groupId >
11+ <artifactId >core-java-modules</artifactId >
12+ <version >0.0.1-SNAPSHOT</version >
13+ </parent >
14+ </project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .inttoshort ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+
5+ import org .junit .jupiter .api .Test ;
6+
7+ class ConvertIntToShortUnitTest {
8+
9+ @ Test
10+ void whenUsingCasting_thenCorrect () {
11+ short expected = 42 ;
12+ int i = 42 ;
13+ short result = (short ) i ;
14+ assertEquals (expected , result );
15+ }
16+
17+ @ Test
18+ void whenUsingIntegerShortValue_thenCorrect () {
19+ short expected = 42 ;
20+ Integer intObj = 42 ;
21+ short result = intObj .shortValue ();
22+ assertEquals (expected , result );
23+ }
24+
25+ @ Test
26+ void whenIntOutOfShortRange_thenGetTheUnexpectedResult () {
27+ int oneMillion = 1_000_000 ;
28+ short result = (short ) oneMillion ;
29+ assertEquals (16960 , result );
30+
31+ int twoMillion = 2_000_000 ;
32+ result = (short ) twoMillion ;
33+ assertEquals (-31616 , result );
34+ }
35+
36+ }
Original file line number Diff line number Diff line change 3636 <!-- <module>core-java-modules/core-java-18</module> --> <!-- JAVA-26056 -->
3737 <!-- <module>core-java-modules/core-java-19</module> --> <!-- JAVA-26056 -->
3838 <module >core-java-numbers-conversions</module >
39+ <module >core-java-numbers-conversions-2</module >
3940 <module >core-java-9-improvements</module >
4041 <module >core-java-9-streams</module >
4142 <module >core-java-9</module >
You can’t perform that action at this time.
0 commit comments