Skip to content

Instantly share code, notes, and snippets.

@sue445
Last active December 11, 2015 18:29
Show Gist options
  • Save sue445/4641958 to your computer and use it in GitHub Desktop.
Save sue445/4641958 to your computer and use it in GitHub Desktop.
ExternalResourceでテストコードのリファクタリングをしてみるサンプル
import org.junit.After;
import org.junit.Before;
public abstract class BaseTest {
@Before
public void setUp() throws Exception {
// setup
}
@After
public void tearDown() throws Exception {
// teardown
}
}
import org.junit.rules.ExternalResource;
public class BaseTestResource extends ExternalResource{
@Override
protected void before() throws Throwable {
// setup
}
@Override
protected void after() {
// teardown
}
}
public class BaseTestResource extends ExternalResource{
@Override
protected void before() throws Throwable {
// setup
}
@Override
protected void after() {
// teardown
}
public String getPath(){
return "";
}
public HttpServletRequest request;
}
import org.junit.Test;
public class SampleTest {
@Test
public void someTest() throws Exception {
// setup
// exercise
// verify
// teardown
}
}
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SampleTest {
@Before
public void setUp() throws Exception {
// setup
}
@After
public void tearDown() throws Exception {
// teardown
}
@Test
public void someTest() throws Exception {
// exercise
// verify
}
}
import org.junit.Test;
public class SampleTest extends BaseTest{
@Test
public void someTest() throws Exception {
// exercise
// verify
}
}
import org.junit.Rule;
import org.junit.Test;
public class SampleTest{
@Rule
public BaseTestResource resource = new BaseTestResource();
@Test
public void someTest() throws Exception {
// exercise
// verify
}
}
import org.junit.Test;
public class SampleTest extends BaseTest{
@Test
public void someTest() throws Exception {
// exercise
String path = super.getPath();
HttpServletRequest request = super.request;
// verify
}
}
public class SampleTest{
@Rule
public BaseTestResource resource = new BaseTestResource();
@Test
public void someTest() throws Exception {
// exercise
String path = resource.getPath();
HttpServletRequest request = resource.request;
// verify
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment