1,创建项目之前先打开Settings--->Build--->Build Tools,在右侧的VM OPtions:右侧的输入框中添加 -DarchetypeCatalog=internal 运行参数 (不做上面操作的话会导致maven 骨架生成速度缓慢到令人发指,卡在running tmp archetypexxxtmp这一步)
2,创建maven web项目,new Project,选择Maven,然后选中Create from archetype,等待加载,加载完成后,选中webapp项目,按照下图顺序点击后点击下一步
然后如图所示:
然后点击下一步,到了maven和maven的setting配置的页面,
然后打开Project Structure,在下图的main文件夹上点击右键new floder,然后创建test,test下的com.xxx.xxx,然后设置为Test,或者Test Resources类型的包
创建测试用例:
@SpringBootApplication
@EnableEurekaServer
public class DemoEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEurekaApplication.class, args);
}
}
也可如下:
import java.util.Calendar;
import java.util.Date;
/**
* Created by asus1 on 2018/6/11.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={ProductServerApplication.class,ProductTest.class})
public class ProductTest {
@Autowired
private ProductService p;
@Test
public void testProdecutAdd() {
ProductDtoRequest productDtoRequest = new ProductDtoRequest();
ProductDto pdto = new ProductDto();
Calendar c = Calendar.getInstance();
c.setTime(date);
for (int i = 0; i < 20; i++) {
c.add(Calendar.DAY_OF_MONTH,1);
pdto.setTradingtTimeEnd(c.getTime());
pdto.setProductName("测试产品"+System.currentTimeMillis());
productDtoRequest.setProductDto(pdto);
try {
Boolean aBoolean = p.addProduct(productDtoRequest);
System.out.println(aBoolean);
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试用例:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Calendar;
import java.util.Date;
/**
* Created by asus1 on 2018/6/11.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={ProductServerApplication.class,ProductTest.class})
public class ProductTest {
@Autowired
private ProductService p;
@Test
public void testProdecutAdd() {
ProductDtoRequest productDtoRequest = new ProductDtoRequest();
ProductDto pdto = new ProductDto();
Calendar c = Calendar.getInstance();
c.setTime(date);
for (int i = 0; i < 20; i++) {
c.add(Calendar.DAY_OF_MONTH,1);
pdto.setTradingtTimeEnd(c.getTime());
pdto.setProductName("测试产品"+System.currentTimeMillis());
productDtoRequest.setProductDto(pdto);
try {
Boolean aBoolean = p.addProduct(productDtoRequest);
System.out.println(aBoolean);
} catch (Exception e) {
e.printStackTrace();
}
}
}
也可以如下:
新建单元测试的启动类
@SpringBootApplication
public class Run {
public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}
新建测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Run.class)
@EnableAutoConfiguration
@EnableAliOss
public class RunTests {
@Autowired
private OssService ossService;
@Test
public void test() throws Exception {
ossService.getToken("aa.sb3");
}
}