AbstractPackageSanity的含义测试Guava testlib中的异常

时间:2015-08-07 13:47:19

标签: java guava guava-testlib

我正在使用Guava Testlib的AbstractPackageSanityTests,而我只是不理解我得到的例外情况。那他们是什么意思?我需要改变什么才能解决这些问题?

这是我的测试类:

package be.fror.racetrack;

import com.google.common.testing.AbstractPackageSanityTests;

public class PackageSanityTests extends AbstractPackageSanityTests {
  public PackageSanityTests() {
    publicApiOnly();
  }
}

这些是我运行测试用例时遇到的错误。

Running be.fror.racetrack.PackageSanityTests
Tests run: 3, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 1.21 sec <<< FAILURE!
testNulls(be.fror.racetrack.PackageSanityTests)  Time elapsed: 0.554 sec  <<< FAILURE!
junit.framework.AssertionFailedError: Error in automated nulls test of class be.fror.racetrack.Racer
If the class is better tested explicitly, you can add testNulls() to be.fror.racetrack.RacerTest
    at com.google.common.testing.AbstractPackageSanityTests.sanityError(AbstractPackageSanityTests.java:288)
    at com.google.common.testing.AbstractPackageSanityTests.testNulls(AbstractPackageSanityTests.java:215)
    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:483)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    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:483)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: junit.framework.AssertionFailedError: Can't find or create a sample instance for type 'be.fror.racetrack.Vector'; please provide one using NullPointerTester.setDefault()
    at junit.framework.Assert.fail(Assert.java:57)
    at junit.framework.Assert.assertTrue(Assert.java:22)
    at com.google.common.testing.NullPointerTester.buildParamList(NullPointerTester.java:365)
    at com.google.common.testing.NullPointerTester.testParameter(NullPointerTester.java:336)
    at com.google.common.testing.NullPointerTester.testMethodParameter(NullPointerTester.java:210)
    at com.google.common.testing.NullPointerTester.testMethod(NullPointerTester.java:178)
    at com.google.common.testing.NullPointerTester.testStaticMethods(NullPointerTester.java:124)
    at com.google.common.testing.ClassSanityTester.doTestNulls(ClassSanityTester.java:227)
    at com.google.common.testing.AbstractPackageSanityTests.testNulls(AbstractPackageSanityTests.java:213)
    ... 25 more

testEquals(be.fror.racetrack.PackageSanityTests)  Time elapsed: 0.127 sec  <<< FAILURE!
junit.framework.AssertionFailedError: Error in automated equals test of class be.fror.racetrack.Racer
If the class is better tested explicitly, you can add testEquals() to be.fror.racetrack.RacerTest
    at com.google.common.testing.AbstractPackageSanityTests.sanityError(AbstractPackageSanityTests.java:288)
    at com.google.common.testing.AbstractPackageSanityTests.testEquals(AbstractPackageSanityTests.java:251)
    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:483)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    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:483)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: com.google.common.testing.ClassSanityTester$ParameterNotInstantiableException: Cannot determine value for parameter be.fror.racetrack.Vector arg1 of be.fror.racetrack.Racer.public static be.fror.racetrack.Racer be.fror.racetrack.Racer.startingAtWithVelocity(be.fror.racetrack.Player,be.fror.racetrack.Vector,be.fror.racetrack.Vector)
    at com.google.common.testing.ClassSanityTester.generateDummyArg(ClassSanityTester.java:672)
    at com.google.common.testing.ClassSanityTester.testEqualsUsing(ClassSanityTester.java:581)
    at com.google.common.testing.ClassSanityTester.doTestEquals(ClassSanityTester.java:323)
    at com.google.common.testing.AbstractPackageSanityTests.testEquals(AbstractPackageSanityTests.java:249)
    ... 25 more

由于这些例外情况提及了RacerVector,因此以下是:

Vector.java(还没有VectorTest.java):

package be.fror.racetrack;

import java.util.Objects;

public final class Vector {
  // Factory methods
  public static Vector zero() { return new Vector(0, 0); }
  public static Vector of(int x, int y) { return new Vector(x, y); }
  public static Vector copyOf(Vector other) {
    if (other == null) { throw new NullPointerException(); }
    // Vector is immutable. Don't make unuseful copies.
    return other;
  }

  // Data
  private final int x;
  private final int y;
  private Vector(int x, int y) { this.x = x; this.y = y; }
  public int getX() { return x; }
  public int getY() { return y; }

  // Business
  public Vector add(Vector other) { return of(x + other.x, y + other.y); }

  // Overriding Object
  @Override public boolean equals(Object obj) {
    if (obj instanceof Vector) {
      Vector other = (Vector) obj;
      return x == other.x && y == other.y;
    }
    return false;
  }
  @Override public int hashCode() { return Objects.hash(x, y); }
  @Override public String toString() { return String.format("(%d,%d)", x, y); }
}

Racer.java(还没有RacerTest.java):

package be.fror.racetrack;

/**
 *
 * @author Olivier Grégoire
 */
public final class Racer {
  // Factory methods
  public static Racer startingAt(Player player, Vector position) { return startingAtWithVelocity(player, position, Vector.zero()); }
  public static Racer startingAtWithVelocity(Player player, Vector position, Vector velocity) {
    if (player == null || position == null || velocity == null) { throw new NullPointerException();}
    return new Racer(player, position, velocity);
  }

  // Data
  private final Player player;
  private Vector position;
  private Vector velocity;
  private Racer(Player player, Vector position, Vector velocity) {
    this.player = player;
    this.position = position;
    this.velocity = velocity;
  }
  public Player getPlayer() { return player; }
  public Vector getPosition() { return position; }
  public Vector getVelocity() { return velocity; }

  public void accelerateAndMove(Direction direction) {
    velocity = velocity.add(direction.asVector());
    position = position.add(velocity);
  }

  // Overriding Object
  @Override public boolean equals(Object obj) {
    if (obj instanceof Racer) { return super.equals(obj); }
    return false;
  }
  @Override public int hashCode() { return super.hashCode(); }
  @Override public String toString() { return String.format("Racer{position=%s, velocity=%s}", position, velocity); }
}

1 个答案:

答案 0 :(得分:2)

对于testEquals问题,“正确”解决方案可能是您的PackageSanityTests()构造函数调用setDistinctValues(Vector.class, Vector.zero(), Vector.of(1, 1))

这里的想法是,为了测试Racer.equals(),测试需要创建Racer的实例。要创建实例,他们需要在Racer上调用其中一个静态工厂方法。要做到这一点,除其他外,他们需要Vector - 两个不同的Vector个对象,不能少。但是虽然测试足够聪明,可以在Racer上查找静态工厂方法,但它们并不够智能,无法在Vector上查找。{/ p> 无论如何,认为就是这样。这里有足够的魔法层,我可能记得错了。