Should I check the listing?

If you have a simple ENUM with values. Getters available.

  • Should unit tests be written for this ENUM?
  • Should the test cover all type names?

Can I advise?

// ENUM with constructor and methods.
public enum Planet {
    MERCURY(3.303e+23, 2.4397e6),
    VENUS(4.869e+24, 6.0518e6),
    EARTH(5.976e+24, 6.37814e6),
    MARS(6.421e+23, 3.3972e6),
    JUPITER(1.9e+27, 7.1492e7),
    SATURN(5.688e+26, 6.0268e7),
    URANUS(8.686e+25, 2.5559e7),
    NEPTUNE(1.024e+26, 2.4746e7);

    // Members
    private final double mass; // in kilograms
    private final double radius; // in meters

    // Constructor
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }

    // Accessors
    public double getMass() {
        return mass;
    }

    public double getRadius() {
        return radius;
    }
}
+4
source share
1 answer

This is not a simple yes or no question, but it depends a lot on the context.

If this listing is an important part of a huge project with a lot of programmers and weak communication structures, and you want to make sure that no one accidentally changed this critical part, a sensible junit test might look like this:

public class PlanetTest {
    private final static int NUM_PLANETS = 8;

    @Test
    public void testIntegrity() {
        assertEquals(NUM_PLANETS, Planet.values().length);

        for (Planet planet : Planet.values()) {
            assertTrue("Wierd: Mass in kg is less than radius in m", 
                planet.getMass() > planet.getRadius());
            }
        }
    }

( IDE) , , , ( ).

( ) , , / . . , , :

  • , ?
  • , ?
  • - ?
  • ....
+4

Source: https://habr.com/ru/post/1690835/


All Articles