Bug 111811 - [JUnit] refactoring: Replace Query with Fixture
Summary: [JUnit] refactoring: Replace Query with Fixture
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 enhancement with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: David Saff CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 39904
Blocks:
  Show dependency tree
 
Reported: 2005-10-06 13:41 EDT by David Saff CLA
Modified: 2005-10-17 11:37 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Saff CLA 2005-10-06 13:41:51 EDT
Triggered by a post to the JUnit mailing list:

http://groups.yahoo.com/group/junit/message/15085

Quoting:

The refactoring I've wanted most is Replace Query with
Fixture.  For example, from:

public void testA() {
    Component comp = new Component(5);
    assertEquals(6, comp.successor());
}

public void testB() {
    Component comp = new Component(5);
    assertEquals(4, comp.predecessor());
}

I can automatically Extract Method to get:


public void testA() {
    Component comp = theComponent();
    assertEquals(6, comp.successor());
}

public void testB() {
    Component comp = theComponent();
    assertEquals(4, comp.predecessor());
}

private Component theComponent() {
    return new Component(5);
}

But I want the next step automatically, as well:

public void setUp() {
    Component fComp = new Component(5);
}

public void testA() {
    assertEquals(6, fComp.successor());
}

public void testB() {
    assertEquals(4, fComp.predecessor());
}

This would have to be implemented for JUnit 3 or 4.
Comment 1 Ilja Preuss CLA 2005-10-07 03:22:49 EDT
I think there is an easier solution:

The "Convert Local Variable to Field" refactoring could offer an 
additional "initialize in setUp" option.
Comment 2 Ilja Preuss CLA 2005-10-07 03:24:00 EDT
See bug 39904 
Comment 3 David Saff CLA 2005-10-07 11:22:14 EDT
bug 39904 goes a long way.  But, especially when using JMock, I often find
myself wanting to move not just the first line of initialization, but a set of
lines, like:

fLaunchConfiguration = mockFor(ILaunchConfiguration.class);
fLaunchConfiguration.stubs();

Come to think of it, given a fix for 39904, I can think of a workaround for this
problem, so I'll let this depend on 39904.
Comment 4 Martin Aeschlimann CLA 2005-10-08 13:11:41 EDT
A quick assist might be better here. 
Comment 5 David Saff CLA 2005-10-14 16:57:28 EDT
Actually, with JUnit 4, it gets a lot easier:

@Before
public void theComponent() {
    fComp = new Component(5);
}
Comment 6 Ilja Preuss CLA 2005-10-17 02:44:43 EDT
(In reply to comment #5)
> Actually, with JUnit 4, it gets a lot easier:
> @Before
> public void theComponent() {
>     fComp = new Component(5);
> }

Easier in which way?
Comment 7 David Saff CLA 2005-10-17 11:14:29 EDT
Easier in this way: for JUnit 4 tests, it is only necessary to mark the method
as "@Before", rather than try to move the logic into the setUp() method.
Comment 8 Ilja Preuss CLA 2005-10-17 11:30:18 EDT
I see - you don't need to move code into a possibly already existing method.

So what needs to be done for JUnit 4 is
- extract method
- mark the method with the @Before annotation
- remove the method call from the test(s)
Comment 9 David Saff CLA 2005-10-17 11:37:53 EDT
Actually, I'm assuming the method is already extracted, so just the two
remaining points.