LoginSignup
0
0

More than 1 year has passed since last update.

junit test dto class

Posted at
private <T> void testDto(Class<T> clazz) throws InstantiationException, IllegalAccessException,
			NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		T obj = clazz.newInstance();
		T obj2 = clazz.newInstance();

		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			// serialVersionUIDは対象外とする。
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}

			if (field.getName().contains("jacoco")) {
				continue;
			}

			String fieldName = field.getName();
			String typeName = field.getType().getName();

			if("boolean".equals(typeName) || "java.lang.Boolean".equals(typeName)) {
				if(fieldName.startsWith("is")) {
					fieldName = fieldName.substring(2);
				}
			}

			StringBuffer sbSet = new StringBuffer();
			sbSet.append("set");
			sbSet.append(fieldName.substring(0, 1).toUpperCase());
			sbSet.append(fieldName.substring(1));

			@SuppressWarnings("rawtypes")
			Class[] parameterTypes = new Class[1];
			parameterTypes[0] = field.getType();
			Method setMethod = clazz.getMethod(sbSet.toString(), parameterTypes);


			StringBuffer sbGet = new StringBuffer();
			if ("boolean".equals(typeName) || "java.lang.Boolean".equals(typeName)) {
				sbGet.append("is");
			} else {
				sbGet.append("get");
			}
			sbGet.append(fieldName.substring(0, 1).toUpperCase());
			sbGet.append(fieldName.substring(1));
			Method getMethod = clazz.getMethod(sbGet.toString());

			switch (typeName) {
			case "java.lang.String":
				setMethod.invoke(obj, "test");
				setMethod.invoke(obj2, "test");
				String val = (String) getMethod.invoke(obj);
				assertThat(val, is("test"));
				break;
			case "int":
			case "java.lang.Integer":
				setMethod.invoke(obj, 1);
				setMethod.invoke(obj2, 1);
				int valInt = (int) getMethod.invoke(obj);
				assertThat(valInt, is(1));
				break;
			case "long":
			case "java.lang.Long":
				setMethod.invoke(obj, 1L);
				setMethod.invoke(obj2, 1L);
				long valLong = (int) getMethod.invoke(obj);
				assertThat(valLong, is(1L));
				break;
			case "boolean":
			case "java.lang.Boolean":
				setMethod.invoke(obj, true);
				setMethod.invoke(obj2, true);
				boolean valBool = (boolean) getMethod.invoke(obj);
				assertThat(valBool, is(true));
				break;
			case "java.sql.Timestamp":
				Timestamp timestamp = new Timestamp(System.currentTimeMillis());
				setMethod.invoke(obj, timestamp);
				setMethod.invoke(obj2, timestamp);
				Timestamp valTime = (Timestamp) getMethod.invoke(obj);
				assertThat(valTime, is(timestamp));
				break;
			case "java.util.List":
		        ParameterizedType listClsType = (ParameterizedType) field.getGenericType();
		        @SuppressWarnings({ "unchecked", "unused" })
		        Class<T> listCls = (Class<T>) listClsType.getActualTypeArguments()[0];
		        ArrayList<T> list = new ArrayList<T>();
				setMethod.invoke(obj, list);
				setMethod.invoke(obj2, list);
				@SuppressWarnings("unchecked")
				ArrayList<T> valList = (ArrayList<T>) getMethod.invoke(obj);
				assertThat(valList, is(list));
				break;
			default:
	            Object newObj = field.getType().newInstance();
				setMethod.invoke(obj, newObj);
				setMethod.invoke(obj2, newObj);
				Object getObj = getMethod.invoke(obj);
				assertThat(getObj, is(newObj));
				break;
			}
		}

		assertThat(obj.equals(obj2), is(true));

		int hasCode1 = obj.hashCode();
		int hasCode2 = obj2.hashCode();
		assertThat(hasCode1, is(hasCode2));

		String str1 = obj.toString();
		String str2 = obj2.toString();
		assertThat(str1.equals(str2), is(true));

	}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0