Easymock mock 异常处理的模拟代码


@Test(expected = ReferenceRetryException.class)
	public void testTryErrorsButHasReturn() throws Exception {
		
		String schema = "BPOS";
		Map<String, Object> parameters = new HashMap<String, Object>();

		parameters.put("REGION_FUND", "SRS1");
		parameters.put("CLIENT_ACCT", "SLX1");
		parameters.put("CUSIP", "007942006");

		ReferenceMeta rm = new ReferenceMeta();
		rm.setMetaKey("metaKey");
		rm.setSchema(schema);
		rm.setRequestNumber("500011");
		rm.setContext("osw");
		rm.setOutputFields("Fieldname1,Fieldname2,Fieldname3");
		rm.setRegion("UA1NA");

		parameters.put(Constants.PARAM_META, rm);
		parameters.put(Constants.PARAM_COLUMNS_CACHED, true);

		String user = "user";
		String password = "pass";

		final Client client2 = mockControl.createMock(Client.class);

		client2.signOn(user, password);
		expectLastCall().times(3);
		
		ResultSet resultSet = null;
		try {
			resultSet = mockControl.createMock(ResultSet.class);
			Column[] columns = new Column[2];
			columns[0] = createMockBuilder(Column.class).withConstructor("column1", "string").createMock();
			columns[1] = createMockBuilder(Column.class).withConstructor("column2", "string").createMock();
			expect(resultSet.getColumns()).andReturn(columns);

			String status = "INTERNAL ERROR [B061 ERROR:  BRIHRC_INVALID_FACILITYTOKEN - Tran=ATNU, Compcode=001]";
			expect(resultSet.getString("__status")).andReturn(status);
			expectLastCall().times(1);
			
			expect(resultSet.getString("__errorMsg")).andReturn("");
			expectLastCall().times(1);

			expect(resultSet.getString("column1")).andReturn("value1");
			expectLastCall().times(1);

			expect(resultSet.getString("column2")).andReturn("value2");
			expectLastCall().times(1);

			expect(resultSet.next()).andReturn(true);
			expectLastCall().times(1);
			expect(resultSet.next()).andReturn(false);
			expectLastCall().times(1);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		expect(client2.process((String) anyObject())).andReturn(resultSet).times(2);
		
		custodyChannel = new CustodyReferenceChannel() {
			@Override
			protected Client createClient(String signUrl, String metaKey) {
				return client2;
			}
		};

		custodyChannel.setUser(user);
		custodyChannel.setPassword(password);


		setReferenceBuilder();
		mockControl.replay();

		ReferenceSet rs = custodyChannel.getReference(schema, parameters);


		Assert.assertNotNull(rs);
		Assert.assertEquals(schema, rs.getSchema());
		Assert.assertEquals(1, rs.getDataList().size());
		Assert.assertEquals("value1", rs.getDataList().get(0).get("column1"));
		Assert.assertEquals("value2", rs.getDataList().get(0).get("column2"));

	}
	


你可能感兴趣的:(easymock)