标签:使用 aec 概念 log arc int lease code art
[TOC]
References:
简单介绍一下概念。
下载 JUnit-xxx.jar (下载地址) 和 Hamcrest(点击直接下载) ,放在一个文件夹下
按照官方推荐,编写一个java类
public class Calculator {
public int evaluate(String expression) {
int sum = 0;
for (String summand: expression.split("\\+"))
sum += Integer.valueOf(summand);
return sum;
}
}
编译这个文件
javac Calculator.java
Calculator.class
然后编写一个Test.java
文件:
```java
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
}
以上。 4. 编译
Test.java:
bash
javac -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar CalculatorTest.java
javac -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar CalculatorTest.java
```
以上。
# Linux or MacOS
java -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
# Windows
java -cp .;junit-4.XX.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
# 以下为输出:
JUnit version 4.12
.
Time: 0.006
OK (1 test)
让测试用例失败
Test.java
中修改assert相关项,重复 4-5,输出如下:
JUnit version 4.12
.E
Time: 0.006
There was 1 failure:
1) evaluatesExpression(CalculatorTest)
java.lang.AssertionError: expected:<8> but was:<6>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
...
FAILURES!!!
Tests run: 1, Failures: 1:
package fc.learn.java.junit;
public class BaobaoFeng {
public void awei18shi(){
laohanTuiche();
guanyinzuolian();
zhongchu();
dulongzuan();
}
public void laohanTuiche(){
System.out.println("laohantuiche!");
}
private void guanyinzuolian(){
System.out.println("guanyinzuolian!");
}
private int zhongchu(){
return 1;
}
public String dulongzuan(){
return "dulongzuan!";
}
public String lianxuzhongchu(int param){
if (param < 5){
throw new IllegalArgumentException("test exception!");
}
return "lianxuzhongchu";
}
}
以上。
BaobaoFeng
出,点开 quickLips:BaobaoFengTest.java
。可以看到,这里不支持private方法的。import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class BaobaoFengTest {
BaobaoFeng fengbaobao;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void awei18shi() {
}
@Test
public void laohanTuiche() {
}
@Test
public void dulongzuan() {
}
@Test
public void lianxuzhongchu(){
}
}
``以上。 3. 要测试哪个方法,就在
Test.java中的对应方法编写测试用例,然后右键运行即可。 比如我要测试
dulongzuan()`方法的返回值,可以如下:
@RunWith
和@ContextConfiguration
:public class TryJunitDaoTest {
@Autowired
TryJunitDao tryJunitDao;
@Test
@Transactional // 此处加上Transactional注解,测试完成之后会回滚
public void insert() {
TryJunitDto dto = new TryJunitDto();
tryJunitDao.insert(dto)
}
@Test
public void select() {
TryJunitDto dto = tryJunitDao.select();
Assert.assertNotEquals(dto, null);
}
}
```
实际上,模拟请求,关键是Mock
,和JUnit关系不是特别大。
同样,生成 Test.java
文件,然后
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/config/spring/appcontext-*.xml",
"file:src/main/webapp/WEB-INF/servlet-context.xml"
})
public class MockRequestControllerTest {
@Autowired
private MockRequestController mockRequestController;
@Test
public void testAcceptRequest() throws Exception {
// 这里,构造 MockRequest
MockHttpServletRequest request = new MockHttpServletRequest();
request.setAttribute("user", "user");
mockRequestController.acceptRequest(request);
}
}
标签:使用 aec 概念 log arc int lease code art
原文地址:https://www.cnblogs.com/tflowingcloud/p/8799212.html