How to avoid call to super class constructors/methods using JMockit?

When you work with legacy codebase, sometime you would like to avoid the call to super constructor and super methods so that you can focus on covering sub class.

package com.ourownjava.tdd.jmockit;

/**
 * 
 * @author Sanju Thomas
 *
 */
public class BaseClass {
	
	public BaseClass(final String name){
		throw new IllegalArgumentException(name);
	}
}
package com.ourownjava.tdd.jmockit;

/**
 * 
 * @author Sanju Thomas
 *
 */
public class SubClass extends BaseClass{

	private String name;
	
	public SubClass(final String name) {
		super(name);
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}
}

package com.ourownjava.tdd.jmockit;

import static org.junit.Assert.assertEquals;
import mockit.Mocked;

import org.junit.Test;

/**
 * 
 * @author Sanju Thomas
 *
 */
public class TestSubClass {

	private SubClass subClass;
	
	@Test
	public void shouldSetNameUsingConstructorInject(@Mocked final BaseClass baseClass){
		subClass = new SubClass("name");
		assertEquals("name", subClass.getName());
	}	
}

Leave a comment

Your email address will not be published. Required fields are marked *