大項目,即使微服務,用@PostConstructor 或者,寫main ,編譯耗時,代碼不純潔。最號的方法Junit測試
這里介紹快捷的測試方法,

生成代碼如下:
package com.uxsino.commons.cache;
import org.junit.Test;
public class ItemTest {
@Test
public void test(){
Item item = new Item();
item.setDatas(new String ("dousil"));
System.out.println(item.getDatas());
}
}
快捷方式2 ,Alt+shift +t 可以直接生成測試類。
Junit 缺陷,junit 在測試的時候,無法知道我們是否使用spring 框架。更不用說,幫我們創建spring 容器了。
利用spring test 就可以很好的實現。
1. 整合junit的必備jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
2. 使用@RunWith注解
@RunWith(SpringJUnit4ClassRunner.class)
public class AccountServiceTest {
}
3. 使用ContextConfiguration 指定配置文件位置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
public class AccountServiceTest {
}
@ContextConfiguration 注解:
- locations 屬性:用於指定配置文件的位置。如果是類路徑下,需要用 classpath:表明
- classes 屬性:用於指定注解的類。當不使用 xml 配置時,需要用此屬性指定注解類的位置
4. 使用@Autowired 注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
@Autowired
private IAccountService as ;
}
示例如下:
package com.uxsino.simo.collector;
import static org.junit.Assert.fail;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers=ConfigFileApplicationContextInitializer.class)
public class TestExecEnvironment {
@Configuration
@ComponentScan("com.uxsino.simo.collector")
public static class Config {
@Autowired
ExecEnvironment env;
}
@Test
public void testExecEnvironment() {
//assert (env != null);
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testGetNameSpace() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testGetEntityDomain() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testInit() {
//NEDBStorage entitydb = new NEDBStorage();
ExecEnvironment execEnvironment = new ExecEnvironment();
assert (execEnvironment != null);
execEnvironment.init();
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testDumpTasks() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testRunPatrolTask() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testStoreToDb() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testRunImmediateQueryEntityInfoIndicator() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testRunImmediateQueryStringEntityInfoIndicator() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testExecQuery() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testSubscribeValueConsumerOfIndicatorValue() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testSubscribeValueSubscriberOfQsuperIndicatorValue() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testAddTask() {
fail("Not yet implemented");
}
@Ignore("Test is ignored before immplemenation")
@Test
public void testDeleteTask() {
fail("Not yet implemented");
}
@Test
public void testX() {
// SSHTarget target = new SSHTarget();
// target.host = "localhost";
// target.port = 22;
// target.passwordEncrypted=false;
// target.setUsername("testcon");
// target.setPassword("Pdf1scape");
//
// SSHConnection conn = new SSHConnection();
//
// conn.connect(target);
//
// for (int i = 0; i < 5; i++) {
// conn.execCmd("date");
// }
//
// conn.close();
}
}
————————————————
版權聲明:本文為CSDN博主「不愛我就寫代碼」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_41744145/article/details/100125690
