Próbuję pozbyć się powtórzeń z unit testów.

0

Firma transportowa.
Driver, Shipper i CallCenterConsultantEmployee. Przy testowaniu każdej z tych klas kod jest taki sam, bo póki co te klasy nic nie dodają ponadto to co dziedziczą od Employee.

Spróbowałem to zrobić tak:

public class DriverMapperTest {

	EmployeeMapperTest<Driver, DriverDto, DriverMapper> emplTest;
	
	@Before
	public void setUp() {
		this.emplTest = new EmployeeMapperTest<Driver, DriverDto, DriverMapper> (Driver.class, DriverDto.class, DriverMapper.class);
	}
	
	@Test
	public void testSourceToDto() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException   {
		emplTest.testSourceToDto();
	}
	
}

.

public class ShipperMapperTest {

	EmployeeMapperTest<Shipper, ShipperDto, ShipperMapper> emplTest;
	
	@Before
	public void setUp() {
		this.emplTest = new EmployeeMapperTest<Shipper, ShipperDto, ShipperMapper> (Shipper.class, ShipperDto.class, ShipperMapper.class);
	}
	
	@Test
	public void testSourceToDto() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException   {
		emplTest.testSourceToDto();
	}
	
}

.

public class CallCenterConsultantMapperTest {

	EmployeeMapperTest<CallCenterConsultant, CallCenterConsultantDto, CallCenterConsultantMapper> emplTest;
	
	@Before
	public void setUp() {
		this.emplTest = new EmployeeMapperTest<CallCenterConsultant, CallCenterConsultantDto, CallCenterConsultantMapper> (CallCenterConsultant.class, CallCenterConsultantDto.class, CallCenterConsultantMapper.class);
	}
	
	@Test
	public void testSourceToDto() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException   {
		emplTest.testSourceToDto();
	}
	
}

.

public class EmployeeMapperTest<S extends Employee, D extends EmployeeDto, M extends GenericMapper<S, D>> {

	GenericMapper<S, D> mapper;
	Class<S> sourceClass;
	Class<D> dtoClass;
	Class<M> mapperClass;

	final String DEFAULT_NAME = "testname";
	final String DEFAULT_NAME2 = "testname2";
	final String DEFAULT_LAST_NAME = "testlastname";
	final String DEFAULT_LAST_NAME2 = "testlastname2";
	final String DEFAULT_PESEL = "92071314764";

	public EmployeeMapperTest(Class<S> sourceClassType, Class<D> dtoClassType, Class<M> mapperClass) {
		this.sourceClass = sourceClassType;
		this.dtoClass = dtoClassType;
		this.mapperClass = mapperClass;
        UserMapper userMapper = new UserMapper();
		EmployeeBaseMapper<S, D> baseMapper = new EmployeeBaseMapper<>(userMapper);
		this.mapper = mapperClass.getConstructor(EmployeeBaseMapper.class).newInstance(baseMapper);
	}

	public S getDefaultSource() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		S s = sourceClass.getConstructor(String.class, String.class, String.class, Boolean.class)
				.newInstance(DEFAULT_PESEL, DEFAULT_NAME2, DEFAULT_LAST_NAME2, true);
		return s;
	}

	public void testSourceToDto() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		// given
		S source = getDefaultSource();

		// when
		D dto = mapper.sourceToDto(source);

		// then
		assertEquals(DEFAULT_NAME2, dto.getFirstName());
		assertEquals(DEFAULT_LAST_NAME2, dto.getLastName());
		assertTrue(dto.isActive());
	}
}

na mj gust wsyzstko powinno hulać, ale przy kompilowaniu coś jest nie tak z metodą getDefaultSource() na:
S s = sourceClass.getConstructor(String.class, String.class, String.class, Boolean.class) eclipse rzuca wyjątkiem:

java.lang.NoSuchMethodException: com.julian.bella.domain.Driver.<init>(java.lang.String, java.lang.String, java.lang.String, java.lang.Boolean)
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getConstructor(Class.java:1825)
	at com.julian.bella.api.mapper.EmployeeMapperTest.getDefaultSource(EmployeeMapperTest.java:36)
	at com.julian.bella.api.mapper.EmployeeMapperTest.testSourceToDto(EmployeeMapperTest.java:43)
	at com.julian.bella.api.mapper.DriverMapperTest.testSourceToDto(DriverMapperTest.java:22)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Są przecież takie konstruktory, np. Driver:

@Entity
public class Driver extends Employee {

	// ...
	
	public Driver(String pesel, String firstName, String secondName, boolean isActive) {
		super(pesel, firstName, secondName, isActive);
	}
}

.

@Entity
public abstract class Employee {

        // ...

	public Employee(String pesel, String firstName, String secondName, boolean isActive) {
		this.pesel = pesel;
		this.firstName = firstName;
		this.lastName = secondName;
		this.peselEncrypted = HashMD5.getHashed(pesel);
		this.isActive = isActive;
	}
    // ...
1

Wygląda jak use case pod testy parametryczne, gdzie pracownik jest parametrem.
https://github.com/Pragmatists/JUnitParams
https://github.com/junit-team/junit4/wiki/parameterized-tests

2

Straszne.
Ale wex po prostu zmień:

 S s = sourceClass.getConstructor(String.class, String.class, String.class, Boolean.class)

Na

 S s = sourceClass.getConstructor(String.class, String.class, String.class, Boolean.TYPE)

A testy parametryczne (j.w.) to prawdopodobnie lepsze rozwiązanie.

0

nie czaję tego w ogóle

@RunWith(Parameterized.class)
public class EmployeeMapperTest2 {

	final String DEFAULT_NAME = "testname";
	final String DEFAULT_NAME2 = "testname2";
	final String DEFAULT_LAST_NAME = "testlastname";
	final String DEFAULT_LAST_NAME2 = "testlastname2";
	final String DEFAULT_PESEL = "92071314764";
	
	Class<Employee> sourceClass;
	Class<EmployeeDto> dtoClass;
	Class<GenericMapper<Employee, EmployeeDto>> mapperClass;
	GenericMapper<Employee, EmployeeDto> mapper;
	
    @Parameters
    public static Collection<Object> data() {
        return Arrays.asList(new Object[] {     
        		 Driver.class, Shipper.class, CallCenterConsultant.class
           });
    }
	
    public EmployeeMapperTest2(Class<Employee> sourceClass, Class<EmployeeDto> dtoClass, Class<GenericMapper<Employee,EmployeeDto>> mapperClass) {
    	this.sourceClass = sourceClass;
    	this.dtoClass = dtoClass;
    	this.mapperClass = mapperClass;
    }

	@Before
	public void setUp() {
		UserMapper userMapper = new UserMapper();
		EmployeeBaseMapper< Employee, EmployeeDto> baseMapper = new EmployeeBaseMapper<>(userMapper);
		baseMapper.setSourceClassType(sourceClass);
		baseMapper.setDtoClassType(dtoClass);
		this.mapper = baseMapper;
		
		//UserMapper userMapper = new UserMapper();
	  //  EmployeeBaseMapper<S, D> baseMapper = new EmployeeBaseMapper<>(userMapper);
	    //this.mapper = mapperClass.getConstructor(EmployeeBaseMapper.class).newInstance(baseMapper);
	}
	

	public EmployeeDto getDefaultDto(Class<EmployeeDto> dtoClass) throws InstantiationException, IllegalAccessException {
		EmployeeDto dto = dtoClass.newInstance();
		dto.setFirstName(DEFAULT_NAME).setLastName(DEFAULT_LAST_NAME).setPesel(DEFAULT_PESEL).setActive(true);
		return dto;
	}
	
	public Employee getDefaultSource(Class<Employee> sourceClass) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		Employee e = sourceClass.getConstructor(String.class, String.class, String.class, Boolean.TYPE)
				.newInstance(DEFAULT_PESEL, DEFAULT_NAME2, DEFAULT_LAST_NAME2, true);
		return e;
	}
	

	@Test
	public void testSourceToDto() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		// given
		Employee source = getDefaultSource(sourceClass);

		// when
		EmployeeDto dto = mapper.sourceToDto(source);

		// then
		assertEquals(DEFAULT_NAME2, dto.getFirstName());
		assertEquals(DEFAULT_LAST_NAME2, dto.getLastName());
		assertTrue(dto.isActive());
	}
}

java.lang.IllegalArgumentException: wrong number of arguments

title

zostawię tak jak zrobilem i mam nadzieję, że nikt tam nie kliknie jak będzie przeglądał githuba :D

0

com.julian.bella xD

1 użytkowników online, w tym zalogowanych: 0, gości: 1