From 2e9f829afb7dbcccaca076ddb8e226defb718b1d Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Mon, 27 Feb 2023 14:38:05 +0800 Subject: [PATCH] add junit demo --- pom.xml | 6 +- .../bridge/service/CalculatorService.java | 36 ++++++++++ .../bridge/CalculatorServiceTest.java | 65 +++++++++++++++++++ .../java/com/educoder/bridge/MainTest.java | 26 ++++++++ 4 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/educoder/bridge/service/CalculatorService.java create mode 100644 src/test/java/com/educoder/bridge/CalculatorServiceTest.java create mode 100644 src/test/java/com/educoder/bridge/MainTest.java diff --git a/pom.xml b/pom.xml index ce4b6c6..24cb7ab 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 1.0-SNAPSHOT webssh Maven Webapp http://maven.apache.org - + 2.6.1 4.3.6.RELEASE @@ -27,7 +27,7 @@ - + org.freemarker freemarker @@ -172,7 +172,7 @@ maven-surefire-plugin 2.18.1 - true + false diff --git a/src/main/java/com/educoder/bridge/service/CalculatorService.java b/src/main/java/com/educoder/bridge/service/CalculatorService.java new file mode 100644 index 0000000..3c89704 --- /dev/null +++ b/src/main/java/com/educoder/bridge/service/CalculatorService.java @@ -0,0 +1,36 @@ +package com.educoder.bridge.service; + +/** + * @Author: youys + * @Date: 2023/2/27 + * @Description: + */ +public class CalculatorService { + + public int add(int a, int b) { + return a + b; + } + + public int subtract(int a, int b) { + return a - b; + } + + public int multiply(int a, int b) { + return a * b; + } + + public double divide(double a, double b) { + if (b == 0) { + throw new IllegalArgumentException("Cannot divide by zero!"); + } + return a / b; + } + + public double sqrt(double a) { + if (a < 0) { + throw new IllegalArgumentException("Cannot calculate square root of negative number!"); + } + return Math.sqrt(a); + } + +} diff --git a/src/test/java/com/educoder/bridge/CalculatorServiceTest.java b/src/test/java/com/educoder/bridge/CalculatorServiceTest.java new file mode 100644 index 0000000..e2e9d4f --- /dev/null +++ b/src/test/java/com/educoder/bridge/CalculatorServiceTest.java @@ -0,0 +1,65 @@ +package com.educoder.bridge; + +import com.educoder.bridge.service.CalculatorService; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @Author: youys + * @Date: 2023/2/27 + * @Description: + */ +public class CalculatorServiceTest { + + @Test + public void testAdd() { + CalculatorService calculator = new CalculatorService(); + int result = calculator.add(1, 2); + assertEquals(3, result); + } + + @Test + public void testSubtract() { + CalculatorService calculator = new CalculatorService(); + int result = calculator.subtract(5, 2); + assertEquals(3, result); + } + + @Test + public void testMultiply() { + CalculatorService calculator = new CalculatorService(); + int result = calculator.multiply(3, 4); + assertEquals(12, result); + } + + @Test + public void testDivide() { + CalculatorService calculator = new CalculatorService(); + double result = calculator.divide(10, 2); + assertEquals(5.0, result, 0); + + try { + calculator.divide(10, 0); + fail("Expected an IllegalArgumentException to be thrown"); + } catch (IllegalArgumentException e) { + assertEquals("Cannot divide by zero!", e.getMessage()); + } + } + + @Test + public void testSqrt() { + CalculatorService calculator = new CalculatorService(); + double result = calculator.sqrt(16); + assertEquals(4.0, result, 0); + + try { + calculator.sqrt(-1); + fail("Expected an IllegalArgumentException to be thrown"); + } catch (IllegalArgumentException e) { + assertEquals("Cannot calculate square root of negative number!", e.getMessage()); + } + } + +} diff --git a/src/test/java/com/educoder/bridge/MainTest.java b/src/test/java/com/educoder/bridge/MainTest.java new file mode 100644 index 0000000..9a07b17 --- /dev/null +++ b/src/test/java/com/educoder/bridge/MainTest.java @@ -0,0 +1,26 @@ +package com.educoder.bridge; + +import org.junit.Test; + +/** + * @Author: youys + * @Date: 2023/2/27 + * @Description: + */ +public class MainTest { + + + @Test + public void test(){ + int a = 1; + int b = 0; + assert b > 0 : String.format("%s/%s, 除数不能为0",a, b); + } + + @Test + public void test2(){ + int i=10; + assert i > 10 : String.format("变量i=%s,需要大于10", i); + } + +}