??????????SpringTest
???????????? ???????[ 2013/11/11 16:11:15 ] ????????
<pre class="java" name="code">/**
* JUnit????????????????Spring??????????? <br/>
* ???????Controller??Service??Dao???????????
*
* @author leiwen@fansunion.cn
*/
public class JUnitTestBase {
public static XmlWebApplicationContext context = null;
private static HandlerMapping handlerMapping;
private static HandlerAdapter handlerAdapter;
// /public static String[] CONFIG_FILES = { "classpath:spring-*.xml" };
// public static String[] configs = { "file:src/main/resources/spring-*.xml"
// };
public static String[] CONFIG_FILES = { "file:src/main/resources/spring-*.xml" };
/**
* ???spring?????????????????????
*/
@BeforeClass
public static void setUp() {
System.out.println("Test start...");
context = new XmlWebApplicationContext();
context.setConfigLocations(CONFIG_FILES);
MockServletContext msc = new MockServletContext();
context.setServletContext(msc);
context.refresh();
msc.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE??
context);
handlerMapping = (HandlerMapping) context
.getBean(DefaultAnnotationHandlerMapping.class);
handlerAdapter = (HandlerAdapter) context.getBean(context
.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
}
// ???request?????????action
public ModelAndView excuteAction(HttpServletRequest request??
HttpServletResponse response) throws Exception {
// ???????????request?????????????????
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING?? true);
HandlerExecutionChain chain = handlerMapping.getHandler(request);
Object handler = chain.getHandler();
final ModelAndView model = handlerAdapter.handle(request?? response??
handler);
return model;
}
@AfterClass
public static void tearUp() {
System.out.println("Test end!");
}
}
//????Controller??2?????
/**
* CampaignGroupController????????
*
* @author leiwen@fansunion.cn
*/
public class FansUnionControllerTestextends JUnitTestBase {
@Test
public void prevAddCampaignGroup() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
String requestURI = "/campaignGroup/prevAdd";
request.setRequestURI(requestURI);
request.setMethod("POST");
final ModelAndView mav = this.excuteAction(request?? response);
Assert.assertEquals("createCampaignGroup"?? mav.getViewName());
}
@Test
public void prevAddCampaignGroup2() throws Exception {
CampaignGroupController userController = context.getBean(CampaignGroupController.class);
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(userController)
.build();
ResultActions perform = mockMvc.perform(MockMvcRequestBuilders.post("/campaignGroup/prevAdd"));
ResultActions andExpect = perform
.andExpect(MockMvcResultMatchers.status().is(200));
andExpect
.andExpect(
MockMvcResultMatchers.view()
.name("createCampaignGroup"));
}
}
----???????2??bean??????Controller??????
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean>
????Service(Dao????)
/**
* ?????? ???????????????????? ????????????????? ?鷳??????????????????bean??
*
* @author leiwen@fansunion.cn
*/
public class FansUnionServiceTest extends JUnitTestBase {
private static CampaignGroupService campaignGroupService = context
.getBean(CampaignGroupService.class);
// ????add??remove??update??????????????????find??
@Test
public void addCampaignGroup() {
CampaignGroup cg = buildOneGroup();
campaignGroupService.add(cg);
CampaignGroup dbCampaignGroup = campaignGroupService.find(cg.getId());
Assert.assertEquals(cg?? dbCampaignGroup);
campaignGroupService.delete(cg.getId());
}
???????????
/**
*
* ???????????????
*
* @author leiwen@fansunion.cn
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-mvc-context.xml"??
"classpath:spring-common-context.xml" })
public class FansUnionServiceTestWithAnnotation {
@Autowired
private CampaignGroupService campaignGroupService;
/**
* ????campaignId???campaignGroup??name????<br>
* ?????????<br>
*
*/
@Test
public void testListCampaignGroupNameByCampaignId(){
System.out.println("**********");
System.out.println(campaignGroupService.listCampaignGroupNameByCampaignId(1005899));
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11