ืืฆืืจืช Assert ื-Java ืืฉืืฉืช ืืืืืงืช ืื ืืืช ืืกืืืืืช ืืืื ืืฆื ืชืืื ืืช ืืืืื ืืืืฆืืข. ืื ืขืืืจ ืืืืืช ืืงืืืช ืฉืืืืืช ืื ืืคืื ืืืืื ืืชืืื ืืช. ืืฆืืจืช ืืขื ื ื-Java ื ืืชืืช ืืืืฆืขืืช ืืืืช ืืืคืชื Java assert ืืืืจืื ืืืืื ืืืืืื ื. ืื ืืืืืื ืืืขืจื ืื ืืื, ืืืขื ื ืขืืืจืช ืืฉืงื ืืืชืืื ืืช ืขืืฉื ืืช ืขืืืืชื. ืื ืืื ืืืขืจื ื-false, ืืืขื ื ื ืืฉืืช ื- AssertionError ื ืืจืง. ืืืื ื ืืื ืืืืื ืืชืืื ืืช ืขื ืงืืืขืืช Java.
//Assertion in Java example
public class AssertDemo1 {
public static void main(String[] args) {
int x = 5;
assert x == 5 : "x should be 5";
System.out.println(x);
}
}
ืืื ืืืืืื ื ืืื, ืื ืืชืืื ืืช ืคืฉืื ืืืฉืืื ืืชืคืงืืื. ืืคืื ืืื ืืืืืง ืืคื ืฉืืื ืฆืคืื ืืืืืช:
5
AssertionError ืืืืื
ื-Java, AssertionError ืืื ืชืช ืืืืงื ืฉื ืืืืงื Error ืืืื ื ืืจืงืช ืืืฉืจ ืืขื ื ื ืืฉืืช. ืงืืืขืืช ื-Java ืืฉืืฉืืช ืืืจื ืืื ืืื ืืืืืง ืฉืชื ืืื ืืืงืืืื, ืชื ืืื ืคืืกื-ืชื ืืื ืื ืื-ืืจืืื ืืื ืืชืงืืืืื. ืืคื ืฉืืืจื ื ืืืื, AssertionError ื ืืจืงืช ืืืฉืจ ืืฆืืจืช ืืขื ื ืื ืืฆืืืื ืืืขืจืื ืืืืช. ืืขืช ื ืืชืื ืืืืฉ ืืช ืืชืืื ืืช ืฉืื ื ืื ืฉืืืืืื ืืืืืืื ื ืืืื ืฉืงืจื.//AssertionError example
public class AssertDemo1 {
public static void main(String[] args) {
int x = 5;
assert x == 7 : "x should be 7";
//here should be AssertionError
System.out.println(x);
}
}
ืืืื, ืื ืื ื ืืคืขืืืื ืืช ืื ืืืคืื ืืื...
5
ืืฉืื ืืืืื ืืฉืชืืฉ ืืืืืื ืฉืื ื! ืืืืจื ืืืืืช ืื ื AssertionError ืื ื-x ืฉืื ื ืืื 5 ืืืงืืืขื ืืฆืคื ืฉืืื ืืืื 7. ืื ืื ืืกืืจ ืืื? ืืฉืื ืืฆืืื ืฉื ืืชื ืคืฉืื ืืืฉืืืช ืงืืืขืืช Java ื-JVM, ืืืื ืืื ืืืฉืชืืฉ ืืื ืืืืฆืขื ืืืืื ืืืืืงืช ืฉืืืืืช ืืชืืื ืืช. ืื ืฉืืืืฉืืื ืขืืืจ ืืืชืืจ ืืืืื ืืืืืงืืช, ืื ืืื ืืืกืชืื ืขืืืื ืขืืืจ ืงืื ืืืฆืืจ. ืื ืืื ืืืฉืชืืฉ ืืืฆืืจืืช, ืืชื ืฆืจืื ืืืคืขืื ืืืชื ืืืืฆืขืืช ืืืคืฉืจืืช vm -ea (ืื -enableassertions). ืื ืืชื ืขืืื ืขื ืฉืืจืช ืืคืงืืื, ืืชื ืืืื ืืืฉืชืืฉ ืืคืงืืื ืืืื ืขืืืจ ืืชืืื ืืช ืฉืื AssertDemo1:
java -ea AssertDemo1
ืื ืชืคืขืื ืืฆืืจืืช ืืชืคืขืื ืืืืฉ ืืช ืืืชื ืชืืื ืืช, ืชืงืื ืืช ืืชืืื ืืช ืืืื:
ืืจืื ืืฉืจืฉืืจ "main" java.lang.AssertionError: x ืฆืจืื ืืืืืช 7 ื-AssertDemo1.main(AssertDemo1.java:4)
ืื ืื ืงืืื.
ืืืฆื ืืืคืฉืจ ืืจืืืื ื-IntelliJ IDEA
- ืขืืืจ ืืชืคืจืื ืืคืขืื โ ืขืจืื ืชืฆืืจืืช....
- ืืื ืืช ืืคืจืืืจ -ea ืืฉืืจื ืืคืฉืจืืืืช VM
ืงืืืขืืช ืืืืืงืช ืืืืืืช ื-Java
ื-Java, ืืืืงืช ืืืืืืช ืืื ืืื ืืงื ืืืืืืช ืืืืงืช ืืืืืื ืงืื ืืืืืื ืืื ืืืืื ืฉืื ืคืืขืืื ืืืืื. ืืฆืืจืืช ืืฉืืฉืืช ืืืจื ืืื ืืืืืงืช ืืืืืืช ื-Java ืืื ืืืืื ืฉืืงืื ืฉื ืืืง ืคืืขื ืืืฆืืคื. ืืืืืงืช ืืืืืืช, ืืืืืช ืงืื, ืืืฉื ืฉืืื ืืกืืืืช, ื ืืืงืช ืืื ืืชืง ืืื ืืืืื ืฉืืื ืคืืขืืช ืืืืื ืืชื ืืื ืืชืฉืืืืช ืฉืื ืืช. ืืืื ื ืืื ืืืืื. ืืื ืืฉ ืื ื ืืืฉืืื ืคืฉืื ืืืืืืจ, ืืืกืืจ, ืืคื ืืืืืืง 2 ืืกืคืจืื ืฉืืืื.public class MyMath {
public static int add (int a, int b) {
return a + b;
}
public static int sub (int a, int b) {
return a - b;
}
public static int mult (int a, int b) {
return a*b;
}
public static double div (int a, int b) {
return a/b;
}
}
ืืืืื, ืืฉ ืืจืื ืฉืืืืช ืืืื ืืืืฉืื ืืื (ืืืฉื, ืื ืงืืจื ืื ื ืืกืืฃ ืืช ื-int ืืืืื ืืืืชืจ ืืืกืคืจ ืืืจ), ืืื ืืขืช ืขืชื ื ืฉืืืจ ืืช ืืืืฉืื ืืื ืืื ืฉืืื. ืืขืช ื ืืชืื ืืืืงื ืื ื ืืืืง ืืช ืืฉืืืืช ืฉื ืืืืงืช myMath ืืขืืจืช ืืืื ื ืืืืื. ื ืืื, ืจืง ืฉื ืืื ืืื. ืืืขืฉื, ื ืืชื ืืืชืื ืืืื ืื ืืืืืงืื ืืืขืื ืื ืืชืงืคืืช, ืืื ืฉืืื ืืืคืืช ืืืงืจืื ืืืืืืื ืืื'. ืืื ืืื ื ืืืื ืืช ืขืฆืื ื ืืืื ืืืื ืื ืืืืจืืช ืืืืื.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MyMathTesting {
@Test
public void testAdd() {
MyMath calculator = new MyMath();
int sum = calculator.add(2, 3);
assertEquals(5, sum);
}
@Test
public void testSub() {
MyMath calculator = new MyMath();
int sub = calculator.sub(5, 2);
assertEquals(3, sub);
}
}
ืฉืืืช assertEquals ืืื ืืขื ื ืืืืืงืช ืื ืฉื ื ืขืจืืื ืฉืืืื. ืื ืืขืจืืื ืืื ื ืฉืืืื, ืชืืฉืง AssertionError , ืืืฆืืื ืฉืืืืื ื ืืฉื. ืฉืืืืฉ ืืืฆืืจืืช ืืืืื ื ืืืืืืช ืืืื ืืขืืืจ ืื ืืชืคืืก ืืืืื ืืืืืืื ืฉืืงืื ืฉืื ืคืืขื ืืืืื ืืชื ืืื ืืืื ืืกืืช ืฉืื ืืช. ื ืกื ืืืืกืืฃ ืืขืฆืืื ืืืื ืื ืืืื ืืืืืืง ืืืคื. ืื ืื ืงืฉื.
GO TO FULL VERSION