| | 112 | |
| | 113 | 要测试doPost()方法的话,首先要准备applicationContext,我们可以通过继承AbstractTransactionalJUnit4SpringContextTests,以获得ApplicationContext[[BR]] |
| | 114 | 然后,我们要模拟HttpServletRequest,HttpServletResponse |
| | 115 | {{{ #!java |
| | 116 | @RunWith(SpringJUnit4ClassRunner.class) |
| | 117 | @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) |
| | 118 | public class AdRegistedServletTest extends AbstractTransactionalJUnit4SpringContextTests{ |
| | 119 | |
| | 120 | @Before |
| | 121 | public void init(){ |
| | 122 | Env env = EnvUtils.getEnv(); |
| | 123 | MockHttpServletRequest request = new MockHttpServletRequest(); |
| | 124 | request.addHeader("X-Forwarded-For", "192.168.20.92"); |
| | 125 | request.addHeader("X-Real-IP", "192.168.20.92"); |
| | 126 | request.setRemoteAddr("192.168.20.92"); |
| | 127 | |
| | 128 | env.setRequest(request); |
| | 129 | } |
| | 130 | |
| | 131 | @After |
| | 132 | public void destory(){ |
| | 133 | EnvUtils.removeEnv(); |
| | 134 | } |
| | 135 | |
| | 136 | @Autowired |
| | 137 | CacheClient cacheClient; |
| | 138 | |
| | 139 | @Autowired |
| | 140 | private AdTypeDAO adTypeDAO; |
| | 141 | |
| | 142 | /** |
| | 143 | * 测试正常流程 |
| | 144 | * @throws Exception |
| | 145 | */ |
| | 146 | @Test |
| | 147 | public void doPost() throws Exception { |
| | 148 | |
| | 149 | final AdRegistedServlet adRegistedServlet = new AdRegistedServlet(); |
| | 150 | adRegistedServlet.setApplicationContext(applicationContext); |
| | 151 | MockServletConfig config = new MockServletConfig(); |
| | 152 | adRegistedServlet.init(config); |
| | 153 | |
| | 154 | final MockHttpServletRequest request = new MockHttpServletRequest(); |
| | 155 | request.addHeader("Referer", "http://g2.pcgames.com.cn/popularize/sxd_register.jsp"); |
| | 156 | request.addHeader("X-Forwarded-For", "192.168.20.92"); |
| | 157 | request.addHeader("X-Real-IP", "192.168.20.92"); |
| | 158 | request.setRemoteAddr("192.168.20.92"); |
| | 159 | |
| | 160 | AdType adType = new AdType(); |
| | 161 | adType.setSrc("wltg"); |
| | 162 | |
| | 163 | long srcid = adTypeDAO.create(adType); |
| | 164 | |
| | 165 | request.addParameter("srcid", ""+srcid); |
| | 166 | request.addParameter("accountId", "393648562"); |
| | 167 | request.addParameter("referer", "http://www.sina.com.cn"); |
| | 168 | request.addParameter("return", "http://g2.pcgames.com.cn"); |
| | 169 | |
| | 170 | final MockHttpServletResponse response = new MockHttpServletResponse(); |
| | 171 | |
| | 172 | Mockery ctx = new Mockery(); |
| | 173 | final EnvInterface env = ctx.mock(EnvInterface.class); |
| | 174 | request.setAttribute("env", env); |
| | 175 | |
| | 176 | ctx.checking(new Expectations(){ |
| | 177 | { |
| | 178 | one(env).getRoot(); |
| | 179 | will(returnValue("http://g2.pcgames.com.cn")); |
| | 180 | } |
| | 181 | }); |
| | 182 | |
| | 183 | adRegistedServlet.doPost(request, response); |
| | 184 | |
| | 185 | AdRequestDAO adRequestDAO = (AdRequestDAO) applicationContext.getBean("adRequestDAO"); |
| | 186 | List<AdRequest> list = adRequestDAO.query("SELECT * FROM ad_request WHERE userid = 393648562 AND ip = '192.168.20.92' AND srcid = "+srcid); |
| | 187 | Assert.assertEquals(1,list.size()); |
| | 188 | |
| | 189 | } |
| | 190 | .................. |
| | 191 | |
| | 192 | } |
| | 193 | |
| | 194 | }}} |