TRUNK-5655: How to write Unit tests

I am working on a certain ticket that requires me to add a method that gets a Concept by name, map or id. I have written the method but i dont know how to write unit tests for the method that i have created. Anybody with links to the sources where i can get more of this information should help me.

could you share the ticket and may be your commit…

this is the ticket

@jnsereko you can also have a look at https://wiki.openmrs.org/display/docs/Unit+Testing+Conventions

1 Like

thanks @gcliff

@samuel34 please keep an eye on this one.

3 Likes
public Concept getConceptByRef(String conceptRef) throws APIException {
	if(conceptRef == null) {
		throw new APIException("null isn`t allowed as acceptable input");
	}
	conceptRef = conceptRef.trim();
	if (conceptRef.contains(".")) {
		return getConcept(evaluateStaticConstant(conceptRef));
	}
	Concept cpt = null;
	int index = conceptRef.indexOf(":");
	if (index != -1) {
		String conceptSource, conceptCode;
		conceptCode = conceptRef.substring(index+1);
		conceptSource = conceptRef.substring(0, index);
		cpt = Context.getConceptService().getConceptByMapping(conceptCode, conceptSource);
		if (cpt != null)
			return cpt;
	}
	if ( !(conceptRef.length() < 36 || conceptRef.length() > 38 || conceptRef.contains(" ") || conceptRef.contains("."))){
		cpt = Context.getConceptService().getConceptByUuid(conceptRef);
		if (cpt != null)
			return cpt;
	}
	cpt = getConcept(conceptRef);
	return cpt;
}
protected static String evaluateStaticConstant(String fqn) {
	int lastPeriod = fqn.lastIndexOf(".");
	String clazzName = fqn.substring(0, lastPeriod);
	String constantName = fqn.substring(lastPeriod + 1);

	try {
		Class<?> clazz = Context.loadClass(clazzName);
		java.lang.reflect.Field constantField = clazz.getField(constantName);
		Object val = constantField.get(null);
		return val != null ? String.valueOf(val) : null;
	}
	catch (Exception ex) {
		throw new IllegalArgumentException("Unable to evaluate " + fqn, ex);
	}
}

this is the method i have added. it is the one i want to know whether it works or its has faults.

public Concept getConceptByRef(String conceptRef) throws APIException {
	if(conceptRef == null) {
		throw new APIException("null isn`t allowed as acceptable input");
	}
	conceptRef = conceptRef.trim();
	if (conceptRef.contains(".")) {
		return getConcept(evaluateStaticConstant(conceptRef));
	}
	Concept cpt = null;
	int index = conceptRef.indexOf(":");
	if (index != -1) {
		String conceptSource, conceptCode;
		conceptCode = conceptRef.substring(index+1);
		conceptSource = conceptRef.substring(0, index);
		cpt = Context.getConceptService().getConceptByMapping(conceptCode, conceptSource);
		if (cpt != null)
			return cpt;
	}
	if ( !(conceptRef.length() < 36 || conceptRef.length() > 38 || conceptRef.contains(" ") || conceptRef.contains("."))){
		cpt = Context.getConceptService().getConceptByUuid(conceptRef);
		if (cpt != null)
			return cpt;
	}
	cpt = getConcept(conceptRef);
	return cpt;
}
protected static String evaluateStaticConstant(String fqn) {
	int lastPeriod = fqn.lastIndexOf(".");
	String clazzName = fqn.substring(0, lastPeriod);
	String constantName = fqn.substring(lastPeriod + 1);

	try {
		Class<?> clazz = Context.loadClass(clazzName);
		java.lang.reflect.Field constantField = clazz.getField(constantName);
		Object val = constantField.get(null);
		return val != null ? String.valueOf(val) : null;
	}
	catch (Exception ex) {
		throw new IllegalArgumentException("Unable to evaluate " + fqn, ex);
	}
}

this is the method i have added. it is the one i want to know whether it works or its has faults.

@jnsereko So if you’ve added a method to the ConceptServiceImpl class, the correct place to add the relevant tests is in the ConceptServiceTest class.

2 Likes

Yes :+1: that is the best place.

1 Like