Skip to content

Commit

Permalink
junit
Browse files Browse the repository at this point in the history
  • Loading branch information
LyeKios committed Oct 5, 2020
1 parent 5a3df9f commit cd38d12
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
14 changes: 14 additions & 0 deletions JavaLearn/.idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions JavaLearn/JavaLearn.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,21 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit5.4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.4.2/junit-jupiter-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.4.2/junit-jupiter-params-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
1 change: 1 addition & 0 deletions JavaLearn/src/math.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ public class math {
public static void main(String[] args){
System.out.println(Math.sin(Math.PI / 2));
}
public void Myprint(){System.out.println(Math.sin(Math.PI / 2));}
}
22 changes: 22 additions & 0 deletions JavaLearn/src/mathTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.junit.jupiter.api.*;

public class mathTest {
@BeforeEach
public void init(){
System.out.println("init...");
}

@AfterEach
public void close(){
System.out.println("close...");
}


@Test
public void testMath(){
math test = new math();
test.Myprint();

}

}
12 changes: 12 additions & 0 deletions JavaLearn/src/medium/Check/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package medium.Check;

public class Calculator {
// @Check
public int sum(int a, int b){
return (a + b);
}
@Check
public void p(){
System.out.println("NoAnnotation");
}
}
12 changes: 12 additions & 0 deletions JavaLearn/src/medium/Check/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package medium.Check;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

public @interface Check {
}
42 changes: 42 additions & 0 deletions JavaLearn/src/medium/Check/TestCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package medium.Check;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestCheck {
public static void main(String[] args) throws IOException {
Calculator c = new Calculator();
Class cls = c.getClass();
Method[] methods = cls.getMethods();

int num = 0;
BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));

for(Method method : methods){
if (method.isAnnotationPresent(Check.class)){
try {
method.invoke(c);
} catch (Exception e) {
//捕获异常
num ++;
bw.write(method.getName() + "方法出异常了");
bw.newLine();
bw.write("异常的名称:" + e.getClass().getSimpleName());
bw.newLine();
bw.write("异常原因:" + e.getCause().getMessage());
bw.newLine();
bw.write("------------------------------------");
bw.newLine();
}
}
}
bw.write("本次测试共有" + num + "次异常");
bw.flush();
bw.close();
}


}

0 comments on commit cd38d12

Please sign in to comment.