日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

如何在Spring和Spring MVC项目中进行测试

發布時間:2025/7/14 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在Spring和Spring MVC项目中进行测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring框架概述

Spring大約包含了20個模塊,這些模塊組成了核心容器(Core Container)、數據訪問/集成(Data Access/Integration)、Web、AOP(面向切面編程,Aspect Oriented Programming)、Instrumentation、消息處理(Messaging)和測試(Test),如下圖:

spring-test模塊通過JUnit和TestNG組件支持單元測試和集成測試。它提供了一致性地加載和緩存Spring上下文,也提供了用于單獨測試代碼的模擬對象(mock object)。

Spring和Spring MVC的區別

spring 是是一個開源框架,是為了解決企業應用程序開發,功能如下

  • 目的:解決企業應用開發的復雜性
  • 功能:使用基本的JavaBean代替EJB,并提供了更多的企業應用功能
  • 范圍:任何Java應用

簡單來說,Spring是一個輕量級的控制反轉(IoC)和面向切面(AOP)的容器框架。
Spring的兩大核心AOP與IOC,可以單獨用于任何應用,包括與Struts等MVC框架與Hibernate等ORM框架的集成,目前很多公司所謂的輕量級開發就是用 Spring + Struts(2)+Hibernate。

spring mvc類似于struts的一個MVC開框架,其實都是屬于spring,spring mvc需要有spring的架包作為支撐才能跑起來

測試Spring項目

開發環境:

  • jdk1.8
  • IDEA 2017
  • maven 3.5

項目結構如下:

首先創建Maven項目,添加Spring Test支持:

<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.1.5.RELEASE</version> </dependency> <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version> </dependency>

TestBean

package com.fsj.ex01;public class TestBean {private String content;public TestBean(String content) {super();this.content = content;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}

TestConfig

package com.fsj.ex01;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile;@Configuration public class TestConfig {@Bean // 聲明當前方法的返回值是一個bean@Profile("dev")public TestBean devTestBean() {return new TestBean("from development profile");}@Bean@Profile("prod")public TestBean prodTestBean() {return new TestBean("from production profile");}}

Main

package com.fsj.ex01;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args) {//使用AnnotationConfigApplicationContext實例化Spring容器AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext();context.getEnvironment().setActiveProfiles("dev"); //激活profilecontext.register(TestConfig.class);// 注冊bean配置類。context.refresh(); //刷新容器TestBean demoBean = context.getBean(TestBean.class);System.out.println(demoBean.getContent());context.close();} }

DemoBeanIntegrationTest

package com.fsj.ex01;import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) //表示該測試用例是運用junit4進行測試,也可以換成其他測試框架 @ContextConfiguration(classes = {TestConfig.class}) //此注解用來加載配置ApplicationContext @ActiveProfiles("prod") //聲明活動的profile public class DemoBeanIntegrationTests {@Autowired //注入beanprivate TestBean testBean;@Test //@Test標注在方法前,表示其是一個測試的方法 無需在其配置文件中額外設置屬性.public void prodBeanShouldInject(){String expected = "from production profile";String actual = testBean.getContent();Assert.assertEquals(expected, actual);}@Beforepublic void beforeMethod(){System.out.println("before all tests");}@Afterpublic void afterMethod(){System.out.println("after all tests.");} }

其中,RunWith注解表示JUnit將不會跑其內置的測試,而是運行所引用的類中的所有測試

http://junit.sourceforge.net/javadoc/org/junit/runner/RunWith.html

@Retention(value=RUNTIME)
@Target(value=TYPE)
@Inherited
public @interface RunWith

When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.

啟動Main運行項目。

啟動DemoBeanIntegrationTests測試本項目。

測試Spring MVC項目

和Spring項目類似,項目完成后,在src/test/java下編寫對應的測試用例。

不同的是,為了測試web項目,需要一些Servlet相關的模擬對象,比如:MockMVC / MockHttpServletRequest / MockHttpServletResponse / MockHttpSession等等。

TestControllerIntegration

package com.fsj.ex02; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpSession; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext;import com.fsj.ex02.MyMvcConfig; import com.fsj.ex02.service.DemoService;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {MyMvcConfig.class}) @WebAppConfiguration("src/main/resources") //1 此注解指定web資源的位置,默認為src/main/webapp public class TestControllerIntegrationTests {private MockMvc mockMvc; //2 模擬MVC對象@Autowiredprivate DemoService demoService;//3 在測試用例注入spring的bean@Autowired WebApplicationContext wac; //4 注入WebApplicationContext@Autowired MockHttpSession session; //5 注入模擬的http session@Autowired MockHttpServletRequest request; // 模擬request@Before //7 測試開始前的初始化工作public void setup() {mockMvc =MockMvcBuilders.webAppContextSetup(this.wac).build(); //2}@Testpublic void testNormalController() throws Exception{String exp_str = demoService.saySomething(); // expect strmockMvc.perform(get("/normal")) //8 模擬GET /normal.andExpect(status().isOk())//9 預期返回狀態為200.andExpect(view().name("page"))//10 預期view的名稱.andExpect(forwardedUrl("/WEB-INF/classes/views/page.jsp"))//11 預期頁面轉向的真正路徑.andExpect(model().attribute("msg", exp_str));//12 預期model里的值}@Testpublic void testRestController() throws Exception{mockMvc.perform(get("/testRest")) //13 GET.andExpect(status().isOk()).andExpect(content().contentType("text/plain;charset=UTF-8"))//14.andExpect(content().string(demoService.saySomething()));//15} }

完整項目在: https://github.com/shenjiefeng/spring-fortest

運行結果:

拾遺

使用AnnotationConfigApplicationContext實例化Spring容器

AnnotationConfigApplicationContext是在Spring 3.0中新增的。這個多功能的ApplicationContext實現即可接收@Configuration類作為輸入,也可接收普通的@Component類,及使用JSR-330元數據注解的類。

當將@Configuration類作為輸入時,@Configuration類本身被注冊為一個bean定義,并且該類中所有聲明的@Bean方法也被注冊為bean定義。

當將@Component和JSR-330類作為輸入時,它們被注冊為bean定義,并且在需要的地方使用DI元數據,比如@Autowired或@Inject。

構造器實例化跟實例化一個ClassPathXmlApplicationContext時將Spring XML文件用作輸入類似,在實例化一個AnnotationConfigApplicationContext時可以使用@Configuration類作為輸入。這就允許Spring容器完全零XML配置:

public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);MyService myService = ctx.getBean(MyService.class);myService.doStuff(); }

如上所述,AnnotationConfigApplicationContext不局限于僅僅使用@Configuration類。不論什么@Component或JSR-330注解的類都能夠作為AnnotationConfigApplicationContext構造器的輸入。比如:

public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);MyService myService = ctx.getBean(MyService.class);myService.doStuff(); }

參考

  • http://blog.csdn.net/tangtong1/article/details/51326887
  • Spring Boot 實戰
  • Spring測試框架JUnit4.4
  • 轉載于:https://www.cnblogs.com/lawlietfans/p/7667518.html

    總結

    以上是生活随笔為你收集整理的如何在Spring和Spring MVC项目中进行测试的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。