English 中文(简体)
Applying one test to two separate classes
原标题:

I have two different classes that share a common interface. Although the functionality is the same they work very differently internally. So naturally I want to test them both.

The best example I can come up with; I serialize something to a file, one class serialize it to plaintext, the other to xml. The data (should) look the same before and after the serialization regardless of method used.

What is the best approach to test both classes the same way? The tests only differs in the way that they instantiate different classes. I dont want to copy the entire test, rename it and change one line.

The tests are currently in JUnit, but Im going to port them to NUnit anyway so code doesnt really matter. Im more looking for a design pattern to apply to this test suite.

最佳回答

Create a common abstract base test class for the test.

abstract class BaseTest{

 @Test
 public void featureX(){
    Type t = createInstance();
    // do something with t
 }

 abstract Type createInstance();
}

ConcreteTest extends BaseTest{

    Type createInstace(){
        return //instantiate concrete type here.
    }
}
问题回答

I d reuse the code either with inheritance or aggregation.

To have the shortest code, I d move a tested instance creation to a factory method in, say, XmlImplementationTest class, and inherit a TextImplementationTest from it:

XmlImplementationTest extends TestCase
{
  Interface tested = null
  Interface createTested() { return new XmlImplementation() }
  ...
  void setUp() { tested = createTested(); }
}

TextImplementationTest extends XmlImplementationTest
{
  override Interface createTested() { return new TextImplementation() }
}

This is not completely correct OO design, as it s TextImplementationTest is NOT a XmlImplementationTest. But usually you don t need to care about it.

Or readdress the test method calls to some common utility class. This would involve more code and not show proper test class in test reports, but might be easier to debug.

I tend to avoid any relations between test classes. I like to keep testcases (or classes) as atomic as possible. The benefit of using inheritance here doesn t outweight the strong coupling you get by it.

I guess it would be helpful, if you could share the validation of the result of the two classes (Assuming blackbox tests). If both classes are able to let you set an outputstream, you might validate that, while the classes itself write to PrintWriter or FileWriter (or whatever you need in your cases).

Furthermore I would avoid to create files during unit-tests, because it might take too much time (+ it might not work on the build machine) and therefore delay your build.

In C#, I d use a generic helper method to test both cases, something like:

internal static void SerializationTestHelper<T>() where T : IMySerialize
{
    T serialize = new T();
    // do some testing
}

[TestMethod]
public void XmlTest()
{
    SerializationTestHelper<XmlSerialize>();
}

[TestMethod]
public void PlainTextTest()
{
    SerializationTestHelper<PlainTextSerialize>();
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签