1

Consider the following test class:

public class MyTest extends extends EasyMockSupport {

    private static TestobjectFactory testobjectFactory = new TestobjectFactory();
    private static final Date DATE = testobjectFactory.createDate(2013, 0, 13, 01, 02, 03);
    private static final String CSV = "einCsv";

    @TestSubject
    private ClassToBeTested classUnderTest = new ClassToBeTested();

    @Mock
    private ClassToBeMocked myMock;

    @After
    public void tearDown() {
        verifyAll();
    }

    @Test
    public void myTest() throws Exception {
        replayAll();

        String outcome = classUnderTest.someMethod("", DATE);

        assertThat(outcome, is(""));
    }

    @Test
    public void anotherTest() throws Exception {
        expect(myMock.mockedMethod((String[]) anyObject())).andReturn(testobjectFactory.foo());

        replayAll();

        String outcome = classUnderTest.someMethod(CSV, DATE);

        assertTrue(outcome.contains("abc"));
    }

}

Questions:

  • Why would you define CSV (or DATE) static final? Defining it final I agree as it's constant. But why static?

  • Which fields are written in uppercase? private static final MY_FIELD; private final MY_FIELD; private static MY_FIELD;

  • Wouldn't it be better to use a static TestobjectFactory?

Edit:

I already saw that post from the possible duplicate before. But the questions I wrote don't have an answer in there.

Chris311
  • 3,794
  • 9
  • 46
  • 80
  • 2
    Magic string constants can always be declared as static final. That way they exist only once and don't have to be created/loaded everytime an instance of the class is produced – ParkerHalo Jan 25 '16 at 11:25
  • "Wouldn't it be better to use a static TestobjectFactory?" You are already: `private static TestobjectFactory testobjectFactory = ...`. What do you mean? – Andy Turner Jan 25 '16 at 11:28
  • Ok, this answers my first question. Nice one. With the second question I mean a call like TestobjectFactory.createDate(2013, 0, 13, 01, 02, 03); instead of instantiating it. – Chris311 Jan 25 '16 at 11:28
  • "Which fields are written in uppercase?" What is your project's coding standard? e.g. [Google's Java style guide](https://google.github.io/styleguide/javaguide.html#s5-naming) specifies it for some identifiers, but that's just the way Google chooses to do it; [Oracle specifies different standards](http://www.oracle.com/technetwork/java/codeconventions-135099.html). – Andy Turner Jan 25 '16 at 11:29
  • I am asking in the sense of java standards. – Chris311 Jan 25 '16 at 11:30
  • Possible duplicate of [What is the best way to implement constants in Java?](http://stackoverflow.com/questions/66066/what-is-the-best-way-to-implement-constants-in-java) – Andy Turner Jan 25 '16 at 11:39

0 Answers0