标签:blog http io ar os 使用 java for sp
文章要点:
1. 为Controller编写测试,不需要应用服务器环境
2. 为Service编写测试,不需要应用服务器环境
Spring为我们提供了一个测试套件Spring-test,与JUnit结合,可以在运行测试时启动IoC容器测试Service,数据库,也可以在脱离web容器的环境下模拟http请求测试Controller,甚是给力。
@RequestMapping(value = {"/", "/shop"}, method = RequestMethod.GET) public String home() { return "index"; }
@Test public void testHome() throws Exception { MockMvc mock = MockMvcBuilders.standaloneSetup(homeController).build(); mock.perform(get("/")).andExpect(status().isOk()) .andExpect(forwardedUrl("index")); }
mock.perform(get("/").param("username", "password")) .andExpect(status().isOk());
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class HomeControllerTest { @Autowired private HomeController homeController; @Test public void testHome() throws Exception { MockMvc mock = MockMvcBuilders.standaloneSetup(homeController).build(); mock.perform(get("/")).andExpect(status().isOk()) .andExpect(forwardedUrl("index")); }
@RunWith(SpringJUnit4ClassRunner.class)
/** * 数据库测试配置类 * @author whf * */ @Configuration public class DatabaseConfigure { @Autowired private DataSource dataSource; @Autowired private EntityManagerFactory entityManagerFactory; @Bean public FactoryBean<EntityManagerFactory> entityManagerFactory() { LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean(); emfb.setDataSource(this.dataSource); emfb.setJpaVendorAdapter(jpaVendorAdapter()); emfb.setPackagesToScan("你的package"); Properties prop = new Properties(); prop.put("hibernate.hbm2ddl.auto", "create-drop"); prop.put("hibernate.show_sql", "true"); prop.put("hibernate.format_sql", "true"); prop.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); emfb.setJpaProperties(prop); return emfb; } @Bean public JpaVendorAdapter jpaVendorAdapter() { return new HibernateJpaVendorAdapter(); } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(this.entityManagerFactory); txManager.setDataSource(this.dataSource); return txManager; } @Bean public DataSource dataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); builder.setType(EmbeddedDatabaseType.H2); return builder.build(); } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {DatabaseConfigure.class, DefaultAccountService.class}) @TransactionConfiguration @Transactional(readOnly = true) public class AccountServiceTest { @Autowired private AccountService acService; @PersistenceContext private EntityManager em; private Member m; @Before @Transactional(readOnly = false) public void initAccountData() { m = new Member(); m.setUsername("bruce"); m.setPassword("3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d"); em.persist(m); } @After @Transactional(readOnly = false) public void cleanDatabase() { m = em.merge(m); em.remove(m); } @Test public void testFindByUsername() { Member m = acService.findMember("bruce"); Assert.assertNotNull(m); Assert.assertEquals("bruce", m.getUsername()); Assert.assertEquals("3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d", m.getPassword()); }
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency>
<!-- JDBC连接池 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.182</version> <scope>test</scope> </dependency>
标签:blog http io ar os 使用 java for sp
原文地址:http://blog.csdn.net/neosmith/article/details/40817203