Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Stack Overflow Model Unit Tests

AAA pattern Unit Test

Hello, I'm trying to follow the AAA pattern in Unit Test, but I would like to know if it's ok to add some Act code in the @Before annotation. In order to keep my code DRY, I set the method alice.askQuestion, for example, inside the @Before annotation because it will be used repeatedly through the tests. I put an example of my code in order to know if this structure is ok? Should I add as comments when is Arrange, Act and Assert? Thanks.

@Before public void setUp() throws Exception {

    Board board = new Board("Java");
    alice = board.createUser("alice");
    bob = board.createUser("bob");
    charlie = board.createUser("charlie");
    question = alice.askQuestion("What is a String?");
    bobAnswer=bob.answerQuestion(question,"A sequence of characters");
    bobPost=bob.answerQuestion(question,"A sequence of characters");

}


@Test
public void reputationGoesUpIfQuestionUpvoted() throws Exception {

    bob.upVote(question);

    assertEquals("Reputation doesn't goes up by 5",5,alice.getReputation());
}

2 Answers

I think putting those in setUp() is fine as long as you are aware that it will get executed prior to each test method.

Perfect. Thanks!