Skip to content

Latest commit

ย 

History

26 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 

Repository files navigation

์œ ๋‹› ํ…Œ์ŠคํŠธ์˜ ๊ตฌ์„ฑ์š”์†Œ

์š”๊ตฌ์‚ฌํ•ญ ๊ตฌ์„ฑ์š”์†Œ

Java ํ”„๋กœ๊ทธ๋žจ ์œ ๋‹› ํ…Œ์ŠคํŠธ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ 6๊ฐœ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ฐ๊ฐ ๋…๋ฆฝ์ ์œผ๋กœ ๋งŒ์กฑ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๊ฐ ์š”๊ตฌ์‚ฌํ•ญ ์†์„ฑ์— ๋”ฐ๋ผ(R1-R6)๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • R1: ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์‹œํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์‹คํ–‰๋˜์–ด์•ผ ํ•จ
  • R2: ์ƒ์ดํ•œ ํ™˜๊ฒฝ์—์„œ๋„ ๋™์ผํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋Š” ๋™์ผํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•จ
  • R3: ์œ ๋‹› ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ์‹คํ–‰์€ ์ƒํ˜ธ ๊ฐ„ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์•„์•ผ ํ•จ
  • R4: ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒฝ์šฐ, ํ…Œ์ŠคํŠธ ์‹คํŒจ(test fail)๋กœ ๊ด€์ฐฐ๋˜์–ด์•ผ ํ•จ
  • R5: ํ…Œ์ŠคํŠธ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ฒฝ์šฐ, ํ…Œ์ŠคํŠธ ์„ฑ๊ณต(test pass)๋กœ ๊ด€์ฐฐ๋˜์–ด์•ผ ํ•จ
  • R6: ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ „๋‹ฌ๋˜์–ด์•ผ ํ•จ.

์‹คํ–‰๊ณผ์ • ๊ตฌ์„ฑ์š”์†Œ

์œ ๋‹› ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐ ์‹คํ–‰๊ณผ์ •์€ ๋‹ค์Œ์˜ 7๊ฐ€์ง€ ์ข…๋ฅ˜๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฒ”์ฃผ(S1-S7)๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • S1 (์‚ฌ์šฉ์ž ์ž…๋ ฅ์— ๋”ฐ๋ฅธ ์œ ๋‹› ํ…Œ์ŠคํŠธ ๊ตฌ๋™): ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ†ตํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž์˜ ์ง์ ‘ ์กฐ์ž‘์„ ํ†ตํ•ด ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์„ ํ•ด์„ํ•˜์—ฌ, ์‹คํ–‰ํ•ด์•ผ ํ•  ์œ ๋‹› ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ง€์ •ํ•˜์—ฌ ์‹œํ–‰
  • S2 (ํ…Œ์ŠคํŠธ ์‹คํ–‰ํ™˜๊ฒฝ ์„ค์ •): ์œ ๋‹› ํ…Œ์ŠคํŠธ์˜ ์‹คํ–‰์„ ํ†ต์ œํ•˜๊ธฐ ์œ„ํ•ด ๊ฒ€์ฆ๋Œ€์ƒ ๋ชจ๋“ˆ์˜ ์‹คํ–‰์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜(ํ™˜๊ฒฝ๋ณ€์ˆ˜, ๊ณต์œ  ๋ณ€์ˆ˜ ๋“ฑ)๋ฅผ ํŠน์ • ๊ฐ’์œผ๋กœ ์„ค์ •
  • S3 (ํ…Œ์ŠคํŠธ ์ž…๋ ฅ ์ง€์ •): ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฒ€์ฆ ๋ชฉ์ ์— ๋”ฐ๋ผ ์กฐ์ •ํ•œ ํ…Œ์ŠคํŠธ ์ž…๋ ฅ ๊ฐ’์„ ์ง€์ •
  • S4 (๊ฒ€์ฆ๋Œ€์ƒ ๋ชจ๋“ˆ ์‹คํ–‰): ์ง€์ •๋œ ํ…Œ์ŠคํŠธ ์ž…๋ ฅ ๊ฐ’์œผ๋กœ ๊ฒ€์ฆ๋Œ€์ƒ ๋ชจ๋“ˆ์˜ ๊ธฐ๋Šฅ์„ ์‹คํ–‰
  • S5 (ํ…Œ์ŠคํŠธ ์‹คํ–‰ ๊ฒฐ๊ณผ ๊ฒ€์‚ฌ): ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ํ…Œ์ŠคํŠธ ์ž…๋ ฅ ๊ฐ’์— ๋Œ€ํ•œ ๊ธฐ๋Œ€ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ํ…Œ์ŠคํŠธ ํ†ต๊ณผ ์œ ๋ฌด๋ฅผ ํŒ๋ณ„
  • S6 (ํ…Œ์ŠคํŠธ ์‹คํ–‰ํ™˜๊ฒฝ ์„ค์ • ํ•ด์ œ): S2์—์„œ ์„ค์ •ํ•œ ์‹คํ–‰ํ™˜๊ฒฝ์„ ํ•ด์ œ
  • S7 (์‚ฌ์šฉ์ž์—๊ฒŒ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ ๋ณด๊ณ ): ํ…Œ์ŠคํŠธ ํ†ต๊ณผ ์œ ๋ฌด๋ฅผ ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ†ตํ•˜๊ฑฐ๋‚˜ ํ˜น์€ ์ง์ ‘ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „๋‹ฌ

์ฝ”๋“œ ๊ตฌ์„ฑ์š”์†Œ

์œ ๋‹› ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ๋˜๋ฏ€๋กœ, ๊ฐ ์ฝ”๋“œ ์š”์†Œ๋Š” ์—ฐ๊ด€๋œ ์ฝ”๋“œ ๋ถ€์œ„์— ๋”ฐ๋ผ 4๊ฐ€์ง€ ์ฝ”๋“œ ๊ตฌ์„ฑ์š”์†Œ(P1-P4)๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • P1: ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๋ณ„๋กœ ๊ณ ์œ  ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ •์˜ํ•˜๋Š” ๋ถ€์œ„
  • P2: ์—ฐ๊ด€๋œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์•„์šธ๋Ÿฌ ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ถ€์œ„
  • P3: ๊ฒ€์ฆ๋Œ€์ƒ ๋ชจ๋“ˆ์ด ํ˜ธ์ถœํ•˜๋Š” ํ…Œ์ŠคํŠธ ๋ชจํ˜•์„ ์ •์˜ํ•˜๊ฑฐ๋‚˜, ํ…Œ์ŠคํŠธ ๋ชจํ˜• ํ”„๋ ˆ์ž„์›Œํฌ์™€์˜ ์ธํ„ฐํŽ˜์ด์Šค ๋ถ€์œ„
  • P4: ํ…Œ์ŠคํŒ… ํ”„๋ ˆ์ž„์›Œํฌ ์ธํ„ฐํŽ˜์ด์Šค ๋ถ€์œ„๋กœ, ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๊ธฐ๋Šฅ์„ ํ˜ธ์ถœ/์ฐธ์กฐํ•˜๋Š” ๋ถ€์œ„

Test Code Bug Patterns

1. JUnit3 ์—์„œ TestCase ํด๋ž˜์Šค์— test method ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S1, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
import junit.framework.TestCase;

public class AssertMethodInvokedFromRunMethod extends TestCase {
public void testComplex() {
    new Thread() {
        @Override
        public void run() {
            assertTrue(true); // Appears to pass test
            assertTrue(false); // AssertMethodFailedError occurs not test failure
        }
    }.start();
}
}
  • ์„ค๋ช… : JUnit TestCase๋ฅผ ์ƒ์†ํ•˜์—ฌ Test Class์ด๋‚˜, ์–ด๋– ํ•œ test method๋„ ๊ตฌํ˜„๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค.
  • ์ถœ์ฒ˜

2. JUnit3 ์—์„œ suite() ์„ ์–ธ์ด ์ž˜๋ชป๋œ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S1, P2
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class BadSuiteMethod extends TestCase {
    static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new MyTests("testEquals"));
        suite.addTest(new MyTests("testNotEquals"));
        return suite;
    }
}

3. JUnit4 ์—์„œ suite()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•œ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S1, P2
  • ์˜ˆ์ œ ์ฝ”๋“œ
import junit.framework.TestCase;

public class BadStaticSuite extends TestCase{
    public void suite() {}         // oops, should be static
}
  • ์„ค๋ช… : JUnit3 ํ”„๋ ˆ์ž„์›Œํฌ๋Š” suite() method๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ test suite๋ฅผ ๊ตฌ์„ฑํ•˜๋Š”๋ฐ, JUnit4์—์„œ๋Š” @RunWith(Suite.class) annotation์„ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

  • ์ถœ์ฒ˜

4. JMock ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋™์‹œ์—, ํ•ด๋‹น Test Class ์—์„œ JMock Runner๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S1, P4
  • ์˜ˆ์ œ ์ฝ”๋“œ
17 // @RunWith(JMock.class) code using Mockery Class without JMock.class
18 public class PricingServiceTests {
19
20     private static final String SKU = "3283947";
21     private static final String BAD_SKU = "-9999993434";
22
23     private PricingService systemUnderTest;
24     private DataAccess mockedDependency;
25     private Mockery mockingContext;
26
27     @Before
28     public void doBeforeEachTestCase() {
29         mockingContext = new JUnit4Mockery();
30         systemUnderTest = new PricingServiceImpl();
31         mockedDependency = mockingContext.mock(DataAccess.class);
32         systemUnderTest.setDataAccess(mockedDependency);
33     }
  • ์„ค๋ช… : jMock ํ…Œ์ŠคํŠธ๋Š” ๋ฐ˜๋“œ์‹œ JMock Runner๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ํ•ญ์ƒ ํ…Œ์ŠคํŠธ๊ฐ€ ์„ฑ๊ณตํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ถœ์ฒ˜

5. JUnit3 ์—์„œ setUp()์ด ์˜ค๋ฒ„๋ผ์ด๋“œ๋˜์–ด ์žˆ์œผ๋‚˜, super.setUp() ์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S2, P3
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class BadSetUp extends TestCase {

  protected void setUp() throws Exception {
    // somePreparations();
  }
  ...
}
public class CorrectSetUp extends TestCase {
  protected void setUp() throws Exception {
    super.setUp();
    // somePreparations();
  }
  ...
}

6. JUnit4 ์—์„œ setUp()์„ ์ •์˜ํ•˜๊ณ  @Before Annotation์ด ์—†๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S2, P4
  • ์„ค๋ช… : JUnit4 ์—์„œ๋Š” @Before Annotation์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค.
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class BadTest {
    public void setUp() {
        bad();    
    }
}
public class GoodTest {
    @Before
    public void setUp() {
        good();
    }
}

7. JUnit4 ์˜ @AfterClass๋‚˜ @BeforeClass annotation์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ method๊ฐ€ static์ด ์•„๋‹Œ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S2, P4
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class JUnit4ClassAnnotationNonStaticPositiveCases {
  @BeforeClass
  public void beforeClassSetUp() {
      setUpSomething();
  }

  @AfterClass
  public void afterClassTearDown() {
      releaseSomething();
  }
}
  • ์„ค๋ช… : Beforeclass๋‚˜ AfterClass annotation์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ static ๋ฉ”์†Œ๋“œ๋กœ ์„ ์–ธ๋˜์–ด์•ผ ํ•œ๋‹ค.

  • ์ถœ์ฒ˜

8. JUnit3 ์—์„œ ์•ฝ์†๋œ ๋ฉ”์†Œ๋“œ ๋ช…์ด ์ž˜๋ชป๋œ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S2, S6, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
import junit.framework.TestCase;

public class JUnit3TestNotRunPositiveCases extends TestCase {
  //  TestCase๋ฅผ extendํ•˜๋‚˜, @Test annotation์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด,
  //  JUnit3์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ธ์ง€, JUnit4 ์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ธ์ง€ ๋ถˆ๋ถ„๋ช…ํ•˜๋‹ค.
  @Test
  public void testName() {}
}
  • ์„ค๋ช… : JUnit3 ์—์„œ๋Š” ๋ฐ˜๋“œ์‹œ test* ํ˜•์‹์œผ๋กœ ๋ฉ”์†Œ๋“œ ๋ช…์ด ์ •์˜๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฅผ ์ง€ํ‚ค์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ถœ์ฒ˜

9. JDK9 ๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ–ˆ์„ ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” Mockito code pattern ์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S3, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
class Foo {
  <T> T getFirst(Iterable<T> xs) {
    return xs.iterator().next();
  }
}
class Test {
  @Mock Foo f;

  @Test
  public void test() {
    Iterable<Boolean> it = Arrays.asList(false);
    when(f.getFirst(it)).thenReturn(false);
  }
}
  • ์„ค๋ช… : Jdk9์œผ๋กœ ํ•ด๋‹น ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์„ฑ๊ณตํ•ด์•ผํ•  test๊ฐ€ ClassCastException์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜์–ด failํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ถœ์ฒ˜

10. JUnit3 ์—์„œ tearDown()์ด ์˜ค๋ฒ„๋ผ์ด๋“œ๋˜์–ด ์žˆ์œผ๋‚˜, super.tearDown() ์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S6, P3
  • ์„ค๋ช… : JUnit3 ์—์„œ setUp() method๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ, super.tearDown()์„ ํ˜ธ์ถœํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class BadTearDown extends TestCase {
    protected void tearDown() throws Exception {
        // no super.tearDown();
    }
}
public class CorrectTearDown extends TestCase {
    protected void tearDown() throws Exception {
        super.teraDown();
    }
}

11. JUnit4 ์—์„œ tearDown()์„ ์ •์˜ํ•˜๊ณ  @After Annotation ์ด ์—†๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S6, P4
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class MyTest {
    public void tearDown() {
        bad();
    }
}
public class MyTest2 {
    @After 
    public void tearDown() {
        good();
    }
}

12. assertThrows์— ๋„˜๊ฒจ์ง„ ์ฝ”๋“œ์—์„œ Exception์„ throwํ•œ ์ดํ›„ statement๊ฐ€ ๋‚จ์•„์žˆ์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R2, S4, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
ImmutableList xs;
assertThrows( UnsupportedOperationException.class, () -> 
    { xs = ImmutableList.of(); // the test passes if this throws 
      xs.add(0); // this is never executed 
});
assertThat(xs).isEmpty();
  • ์„ค๋ช… : assertThrows์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ง„ lambda์— ํ•œ๊ฐœ ์ด์ƒ์˜ statement๊ฐ€ ์žˆ๊ณ , ๋‚ด๋ถ€์—์„œ exception์„ throwํ•œ๋‹ค๋ฉด ๊ทธ ๋’ค์˜ statement๋“ค์€ ๋ฌด์‹œ๋œ๋‹ค.
  • ์ถœ์ฒ˜

13. Test Method ๋‚ด๋ถ€์—์„œ Assertion ๋ฉ”์†Œ๋“œ์˜ ํ˜ธ์ถœ์ด ์—†๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class Foo extends TestCase {
   public void testSomething() {
   Bar b = findBar();
   // This is better than having a NullPointerException
   // assertNotNull("bar not found", b);
   b.work();
   }
}

14. assertNull() ์•ˆ์— boxed primitive ๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ง€๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S5, P1
  • ์ฝ”๋“œ ์˜ˆ์ œ
public class ImpossibleNullTest {
    @Test
    public void impossibleNullTest() {
        Integer autoboxedInt = 30;
        assertNotNull(autoboxedInt);
    }
}

15. Test Framework๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋™์‹œ์— assert()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class AssertUsedTest {
    @Test
    public void assertUsedTest() {
        Integer actual = methodReturnsNonZeroNumber();
        assert(actual != 0); // This is BAD
        assertNotEquals(actual, 0); // This is GOOD
    }
}

16. Assertion method ์—์„œ ๋™์ผํ•œ ๊ฐ์ฒด์˜ reference ๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ์ง€ ํ™•์ธํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class JUnitAssertSameCheckPositiveCase {

  public void test(Object obj) {
    // BUG: Diagnostic contains: An object is tested for reference equality to itself using JUnit
    org.junit.Assert.assertSame(obj, obj);

    // BUG: Diagnostic contains: An object is tested for reference equality to itself using JUnit
    org.junit.Assert.assertSame("message", obj, obj);

    // BUG: Diagnostic contains: An object is tested for reference equality to itself using JUnit
    junit.framework.Assert.assertSame(obj, obj);

    // BUG: Diagnostic contains: An object is tested for reference equality to itself using JUnit
    junit.framework.Assert.assertSame("message", obj, obj);
  }
}
  • ์„ค๋ช… : ๊ฐ™์€ ๊ฐ์ฒด์—๋Œ€ํ•ด reference๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋Š” ๊ฒฝ์šฐ assertion์€ ๋ฐ˜๋“œ์‹œ passํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ถœ์ฒ˜

17. Mockito ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ…Œ์ŠคํŠธ์—์„œ verify ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
// Calls to Mockito.when should always be accompanied by a call to a method like thenReturn.
when(mock.get()).thenReturn(answer); // correct
when(mock.get())                     // oops!
  • ์„ค๋ช… : Mockito๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ…Œ์ŠคํŠธ์—์„œ verify ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„, ํ…Œ์ŠคํŠธ๊ฐ€ ํ•ญ์ƒ ์„ฑ๊ณตํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ถœ์ฒ˜ :

18. Assertion ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ๊ฐ์ฒด์— ๋Œ€ํ•ด equality ๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class TruthSelfEqualsPositiveCases {
  public void testAssertThatEq() {
    String test = Boolean.TRUE.toString();
    // BUG: Diagnostic contains: new EqualsTester().addEqualityGroup(test).testEquals()
    assertThat(test).isEqualTo(test);
  }
  ...
}
  • ์„ค๋ช… : ๊ฐ™์€ ๊ฐ์ฒด์—๋Œ€ํ•ด equality๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ๊ฒฝ์šฐ ํ•ญ์ƒ passํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ถœ์ฒ˜

19. Thread.run() ๋‚ด๋ถ€์— assertion ์ด ์žˆ๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S7, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class AssertMehtodInvodedFromeRunMethod extends TestCase {
  public void testComplex() {
    new Thread() {
      @Override
      public void run() {
        assertTrue(true); // Appears to pass test
        assertTrue(false); // AssertMethodFailedError occurs not test failure
      }
    }.start();
  }
}

20. Assertion ๋ฉ”์†Œ๋“œ์—์„œ test ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์ˆ˜์ธ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, R5, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
import static com.google.common.truth.Truth.assertThat;
public class TruthConstantAssertsPositiveCases {
  public void testAssertThat() {
    // BUG: Diagnostic contains: assertThat(new TruthConstantAssertsPositiveCases()).isEqualTo(1);
    assertThat(1).isEqualTo(new TruthConstantAssertsPositiveCases());

    // BUG: Diagnostic contains: assertThat(someStaticMethod()).isEqualTo("my string");
    assertThat("my string").isEqualTo(someStaticMethod());
    ...
}

์„ค๋ช… : JUnit์˜ assertEquals๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ฐ’(์ƒ์ˆ˜)์„ ๋จผ์ €, ์‹ค์ œ ํ…Œ์ŠคํŠธ ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์„ ๋‹ค์Œ์— ๋ฐ›์•„์•ผ ํ•œ๋‹ค.

21. assert ๊ตฌ๋ฌธ์—์„œ ํƒ€์ž…์ด ๋‹ค๋ฅธ ๋‘ ๊ฐ์ฒด์˜ equality ๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, R6, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class TruthIncompatibleTypePositiveCase  {
  assertThat(someMethodThatReturnsString()).isEqualTo(10);
}
  • ์„ค๋ช… : Truth framework์—์„œ ์„œ๋กœ ํƒ€์ž…์ด ๋‹ค๋ฅธ ๋‘ ๊ฐ์ฒด์˜ equality ๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค.
  • ์ถœ์ฒ˜

22. assertionEquals()์—์„œ ๋ถ€๋™ ์†Œ์ˆ˜์  ๋น„๊ต์—์„œ ์˜ค์ฐจ ํ—ˆ์šฉ ๋ฒ”์œ„๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R5, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
import junit.framework.TestCase;

public class JUnit3FloatingPointComparisonWithoutDeltaCases extends TestCase {
  public void testFloat() {
      float actual = 0.5 + 0.5;
      assertEquals(1.0f, actual); // this is not good
  }
}

23. Try ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ fail()์ด ์žˆ์œผ๋‚˜, catch ๋ธ”๋ก์—์„œ AssertionError ๋ฅผ catch ํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ์š”์†Œ : R5, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
try {
  doSomething();
  fail("expected doSomething to throw AssertionError");
} catch (AssertionError expected) {
  // expected exception
}
  • ์„ค๋ช… : Exception throw ๋˜๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ•˜๋Š” try block ์•ˆ์—์„œ ์‹ค์ œ๋กœ Exception์ด throw ๋˜์ง€ ์•Š์•„ ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํŒจ๋˜์•ผ ํ•˜๋ฏ€๋กœ fail()์„ ํ˜ธ์ถœ๋˜์—ˆ์œผ๋‚˜, catch block์—์„œ AssertionError catchํ•˜๋Š” ๊ฒฝ์šฐ, ํ…Œ์ŠคํŠธ๊ฐ€ failํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.
  • ์ถœ์ฒ˜

24. assertTrue() ๋‚ด๋ถ€์˜ ํŒ๋ณ„์‹์—์„œ equals ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋‘ ๊ฐ์ฒด๊ฐ€ ๊ฐ™์Œ์„ ํ™•์ธํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R6, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestHelloWorld {
    @Test()
    public void testUseAssertEquals() {
        Object actual = getActual();
        Object expected = getExpected;
        Assert.assertTrue(expected.equals(actual)); // This is Bad
        Assert.assertEquals(expected,actual); // This is Good
    }
}
  • ์„ค๋ช… : AssertTrue() eqauls()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐ์ฒด๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๊ณ  ์žˆ๋‹ค. equals()๋Š” ํ˜ธ์ถœํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ null์ผ ๋•Œ NullPointerException์ด ๋ฐœ์ƒ๋˜๋ฏ€๋กœ, null-safeํ•œ AssertSame()์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋œ๋‹ค. ๋˜ํ•œ Test์˜ ์‹คํŒจ์‹œ AssertSame()์„ ์‚ฌ์šฉํ•˜๋ฉด Framework๊ฐ€ ๋”์šฑ ์ƒ์„ธํ•œ ์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์ถœ์ฒ˜

25. assertTrue() ๋‚ด๋ถ€์˜ ํŒ๋ณ„์‹์—์„œ test ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ null ์ธ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R6, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestHelloWorld {
    @Test()
    public void testUseAssertNull() {
        Object actual = getActual();
        Assert.assertEquals(actual == null ); // This is Bad
        Assert.assertNull(actual) // This is Good
    }
}

26. JUnit4 ์—์„œ test method ์— @Test Annotation์ด ์—†๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S1, P4
  • ์˜ˆ์ œ ์ฝ”๋“œ
public class MyTest {
  public void testBad() {
    doSomething(); // no execution of test
  }

  @Test
  public void testGood() {
    doSomething();
  }
}

27. assertionTrue() ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์•ผํ•  expression์„ ์ž˜๋ชป ์ž…๋ ฅํ•จ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
   public class SystemPrivilegesPermissionTest extends BaseTestCase {
     private void assertEquivalentPermissions(Permission p1, Permission p2) {
       ...
       assertEquals(p1.hashCode(), p2.hashCode());
       assertTrue(p1.implies(p2));
-      assertTrue(p1.implies(p2));
+      assertTrue(p2.implies(p1));
    }
  }
  • ์„ค๋ช… : Check both "p1 implies p2" and "p2 implies p1" in assertEquivalentPermissions(), instead of checking "p1 implies p2" twice.
  • ์ถœ์ฒ˜

28. xml query๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ž˜๋ชป ์ž…๋ ฅํ•จ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S2, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
  pupblic class PredicatesTests extends AbstractQueryTest {
    ...
    public void testEquality() throws RepositoryException {
-   String stmt = "/" + jcrRoot + "/*[@" + jcrPrimaryType + "='" + nodeTypeName + "']";
+   String stmt = "/" + jcrRoot + "/" + testPath + "/*[@" + jcrPrimaryType + "='" + nodeTypeName + "']";
    try {
      qm.createQuery(stmt, Query.XPATH);
    ...
  }
  
  public abstract class Abstract JCRTest extends JUnitTest {
    ...
    /***
      *  Absolute path to the test root node.
      */
    protected String testRoot;
    ...
    protected void setUp throws Exception {
      super.setUp();
      testRoot = getProperty(RepositoryStub.PROP_TESTROOT);
      if (testRoot == null) {
        fail("Property '" + RepositoryStub.PROP_TESTROOT + "' is not defined.");
      }
      ...
      
      // cut off '/' to build testPath
      testPath = testRoot.substring(1);
      ...
    }
  }  
  • ์„ค๋ช… : xml Query์— ์‚ฌ์šฉ๋˜๋Š” stmt ๋ณ€์ˆ˜์— testPath๊ฐ€ concatenate ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, PredicateTest class์—์„œ ์ด๋ฅผ ๋ˆ„๋ฝํ•˜์˜€๋‹ค. (PredicatesTest does not respect testroot configuration property)
  • ์ถœ์ฒ˜

28. FileInputStream ์„ ์ƒ์„ฑํ•˜์˜€์œผ๋‚˜ close()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, S6, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
@Test
   public void testBZip2Codec() throws IOException {
     ...
     // check the output to make sure it is what we expected.
     // read the gzip file and verify the contents
     BZip2Codec bz2Codec = new BZip2Codec();
     InputStream bz2in = bz2Codec.createInputStream(new FileInputStream(f
-        .getPath()
-        + "/sub-foo.bz2"));
+        .getPath() + "/sub-foo.bz2"));
     byte[] buf = new byte[1];
     StringBuilder output = new StringBuilder();
 
     while ((bz2in.read(buf)) > 0) {
       output.append(new String(buf));
     }
+    bz2in.close(); // Must close for windows to delete
     assertEquals(expected, output.toString());
 
     assertTrue("temp folder successfully deleted", FileUtil.rmr(f));
   }
  • ์„ค๋ช… : InputStream์„ ์ƒ์„ฑํ•œ ๋’ค ํ…Œ์ŠคํŠธ ์ข…๋ฃŒ ์ „ close๋ฅผ ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์ด๋‹ค. (windows์—์„œ๋Š” close ๋˜์ง€ ์•Š์Œ)
  • ์ถœ์ฒ˜

29. Thread๋ฅผ ์ƒ์„ฑํ•˜์˜€์œผ๋‚˜, join์ด ํ˜ธ์ถœ๋˜์ง€ ์•Š๊ณ  ํ…Œ์ŠคํŠธ๊ฐ€ ์ข…๋ฃŒ๋จ\

  • ๊ตฌ์„ฑ ์š”์†Œ :R4, S2, S3, S6, P1
  • ์ฝ”๋“œ ์˜ˆ์ œ
 public simpleThread(String argv[]) throws Exception {

    ij.getPropertyArg(argv);
    _connection = ij.startJBMS();
    Connection conn = GetConnection();
    Statement stmt = conn.createStatement();
    ...
+   Thread[] threads = {
+     new simpleThread(query,0),
+     new simpleThread(query,10000),
+     new simpleThread(query,10100),
+     new simpleThread(query,20000),
+   };
+
+   for (int i = 0; i < threads.length; i++) {
+     threads[i].join();
    }
+
+   _connection.close();
+   _connection = null;
  }
  • ์„ค๋ช… : ํ…Œ์Šคํ„ฐ๋Š” 5-8 ๋ผ์ธ์—์„œ Exception์ด ๋ฐœ์ƒ๋˜๋Š” ๊ฒƒ์„ ๊ธฐ๋Œ€ํ•˜๊ณ  ์žˆ๊ณ , ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ test fail๋กœ ์ •์˜ํ•˜์˜€๋‹ค. 10-13 ๋ผ์ธ์€ ์ƒˆ๋กœ์ด ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„์ธ๋ฐ, ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‚˜์„œ join()์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ธฐ๋„ ์ „์— ํ…Œ์ŠคํŠธ๊ฐ€ ์ข…๋ฃŒ๋˜์–ด ๋ฒ„๋ฆฌ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

  • ์ถœ์ฒ˜

30. org.junit.Test ๋ฅผ import ํ•˜๋Š” java ํŒŒ์ผ์˜ public class ๋ช…์ด Test ๋กœ ์‹œ์ž‘๋˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, R3, S1, P4
- public class SliderUtilsTest {
+ public class TestSliderUtils {
    ...
  }
  • ์„ค๋ช… : Maven์—์„œ ํ…Œ์ŠคํŠธ๋ช…์ด Test๋กœ ์‹œ์ž‘ํ•ด์•ผ Test Class๋กœ ์ธ์‹ํ•œ๋‹ค (Default).
  • ์ถœ์ฒ˜

31. Exception์„ ๊ธฐ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์—์„œ try๋ธ”๋ฝ ์•ˆ์— fail()์˜ ํ˜ธ์ถœ์ด ์—†์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R1, R4, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
/* DERBY-6088 */
    try {
      insert.setObject( 1, null, ILLEGAL_JDBC_TYPES[ i ] );
+     fail( "setObject() should have failed." );
    }
      catch (SQLException se) { assertUnimplemented( se ); }
    }
/* DERBY-3852*/
JDBCDataSource.setBeanProperty(ds, "shutdownDatabase", "shutdown");
    try {
      ds.getConnection();
+     fail("shutdown should raise exception");
    } catch (SQLException sqle) {
      assertSQLState("XJ015", sqle);
    }
  • ์„ค๋ช… : Exception์„ ์˜๋„ํ•œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์—์„œ ์˜๋„ ํ•˜์ง€ ์•Š์€ Exception์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ fail()์ด ์—†์–ด์„œ ํ…Œ์ŠคํŠธ๊ฐ€ ํ•ด๋‹น ๋ถ€๋ถ„์„ ๋„˜์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

  • ์ถœ์ฒ˜

32. SSLContext ์˜ getInstance ๋ฉ”์†Œ๋“œ์— "SSL"์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธด ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R2, S2, S3, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
- SSLContext sc = SSLContext.getInstance("SSL");
+ SSLContext sc = null;
+ if (System.getProperty("java.vendor").contains("IBM")) {
+     sc = SSLContext.getInstance("SSL_TLS");
+ } else {
+     sc = SSLContext.getInstance("SSL");
+ }
  • ์„ค๋ช… : IBM JDK ๋ฒ„์ „์— ๋”ฐ๋ผ์„œ "SSL_TLS"๋ฅผ getInstance()์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์•ผ ํ•œ๋‹ค.
  • ์ถœ์ฒ˜

33. file path ์˜ ๊ฒฝ๋กœ๋ฅผ '/'์œผ๋กœ๋งŒ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R2, S3, P1
  • **์ฝ”๋“œ **
   private static void delete(File dir) throws IOException {
-    Path p = new Path("file://"+dir.getAbsolutePath());
     Configuration conf = new Configuration();
-    FileSystem fs = p.getFileSystem(conf);
+    FileSystem fs = FileSystem.getLocal(conf);
+    Path p = fs.makeQualified(new Path(dir.getAbsolutePath()));
     fs.delete(p, true);
   }
  • ์„ค๋ช… : The prior code for path construction fails on Windows due to the drive spec and backslashes. Using FileSystem#makeQualified against the local file system works cross-platform.

  • ์ถœ์ฒ˜

34. for ๋ฌธ์˜ ์กฐ๊ฑด๋ฌธ์„ ์ž˜๋ชป ์ž…๋ ฅํ•˜์—ฌ test ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R2, S4, P1
  • ์ฝ”๋“œ ์˜ˆ์ œ
   public void testDirectoryScanner() throws Exception {
     // Run the test with and without parallel scanning
-    for (int parallelism = 1; parallelism < 2; parallelism++) {
+    for (int parallelism = 1; parallelism < 3; parallelism++) {
       runTest(parallelism);
     }
   }
  • ์„ค๋ช… : for๋ฌธ์•ˆ์˜ ์กฐ๊ฑด๋ฌธ์„ ์ž˜๋ชป ์ž…๋ ฅํ•˜์—ฌ test๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์•˜๋‹ค.
  • ์ถœ์ฒ˜

35. ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๋’ค try-catch ๋ธ”๋ก ์ดํ›„ finally ๋ธ”๋ก์— close()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R2, S6, P1

  • ์ฝ”๋“œ ์˜ˆ์ œ

  public void testSessionGetcontentHandler() throws RepositoryException, IOException, SAXException {
    FileInputSstrean in = new FileInputStream(file);
-   exportRepository(SAVEBINARY, RECURSE);
-   doImportNoSave(treeComparator.targetFoler, in, CONTENTHANDLER);
+   try {
+     exportRepository(SAVEBINARY, RECURSE);
+     doImportNoSave(treeComparator.targetFoler, in, CONTENTHANDLER);    
+   } finally {
+     in.close();
+ }
  • ์„ค๋ช… : FileInputStream์„ ์ƒ์„ฑํ•œ ๋’ค ํ…Œ์ŠคํŠธ๊ฐ€ ๋๋‚˜๊ธฐ ์ „ try๋ธ”๋ฝ ์•ˆ์— exception์ด throw๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ดํ›„, finally ๋ธ”๋ก ์•ˆ์— FileInputStream์˜ close()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์ด๋‹ค. ์ด๊ฒฝ์šฐ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๋’ค์— temporary files created by some jUnit test are not automatically removed

  • ์ถœ์ฒ˜

36. Test Suite ์—์„œ ํ•˜๋‚˜์˜ DB ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , Sequence ๋ฅผ ์ƒ์„ฑํ•œ ๋’ค Drop ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R3, S2, S5, P2

  • ์„ค๋ช… : โ€˜alpha_seqโ€™ ๋ผ๋Š” sequence๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  testCase๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์ „ drop ๋˜์ง€ ์•Š์•„, ๋‹ค์Œ ํ…Œ์ŠคํŠธ์˜ ์‹คํ–‰์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค. lang.SequenceTest fails w/ "Seqeunce 'ALPHA_SEQ' already exists." on phoneME/cvm. commit message : Do cleanup in testcases to make the testcases independent of running order.

  • ์ฝ”๋“œ ์„ค๋ช…

  public class SequenceTest extends BaseJDBCTestCase {
    public static Test suite() {
      public void testCreateOtherSchemeSequence() throws SQLExecption {
        ..
        assertStatementError("42507", stmtBeta, "CREATE SEQUENCE alpha.alpha_seq3");
 
+       // Cleanup:
+       stmtAlpha.executeUpdate("DROP SEQUENCE alpha_seq");
+        
        stmtAlpha.close();

37. Socket ์„ ์ƒ์„ฑํ•˜๊ณ  ํ…Œ์ŠคํŠธ๊ฐ€ ๋๋‚˜๋Š” ์‹œ์ ์— close() ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R3, S6, P1
  • ์ฝ”๋“œ ์„ค๋ช…
  @Test
  public void testShortCircuitReadFromServerWithoutShm() throws Exception {
    TemporarySocketDirectory sockDir = new TemporarySocketDirectory();
    //...
        
+   sockDir.close();
  }
  • ์„ค๋ช… : socket์„ ์ƒ์„ฑํ•˜๊ณ  close๋ฅผ ํ•˜์ง€ ์•Š์•„ ๋‹ค์Œ ํ…Œ์ŠคํŠธ ์‹คํ–‰ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ๋‹ค.

  • ์ถœ์ฒ˜

38. ํ…Œ์ŠคํŠธ ์‹คํ–‰ ๋„์ค‘ ๊ณต์œ  ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ž„์˜๋กœ ๋ณ€๊ฒฝํ•œ ํ›„ ์ด๋ฅผ ์ดˆ๊ธฐ ๊ฐ’์œผ๋กœ ๋˜๋Œ๋ฆฌ์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R3, S6, P1, P2
  • ์˜ˆ์ œ ์ฝ”๋“œ
@Test
public void testGzipOutputFormat() throws IOException, InterruptedException {
    // set the output format.
    FlumeConfiguration conf = FlumeConfiguration.get();
    conf.set(FlumeConfiguration.COLLECTOR_OUTPUT_FORMAT, "syslog");
    conf.set(FlumeConfiguration.COLLECTOR_DFS_COMPRESS_GZIP, "true");
    ...
}

@Test
public void testDefaultCodec() throws IOException, InterruptedException {
    // set the output format.
    FlumeConfiguration conf = FlumeConfiguration.get();
    conf.set(FlumeConfiguration.COLLECTOR_OUTPUT_FORMAT, "syslog");
    conf.set(FlumeConfiguration.COLLECTOR_DFS_COMPRESS_CODEC, "DefaultCodec");
+   conf.set(FlumeConfiguration.COLLECTOR_DFS_COMPRESS_GZIP, "false");
    ...
}    
  • ์„ค๋ช… : ์ด์ „ ํ…Œ์ŠคํŠธ์—์„œ์˜ ์„ค์ •์ด ๋‹ค์Œ ํ…Œ์ŠคํŠธ์—์„œ๋„ ์ ์šฉ๋˜์–ด ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

  • ์ถœ์ฒ˜

39. Ant 1.9.3 ์—์„œ ant ๊ฐ€ test ๊ฒฐ๊ณผ๋ฅผ file ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ์„ค์ •ํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R3, S7, P4
  • ์˜ˆ์ œ ์ฝ”๋“œ
  // java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy
  ...
+ // Starting with Ant 1.9.3, write permission has to be granted to ant.jar
+ // as well so that Ant's JUnit runner can write test results to a file.
+ // Only needed when running the tests under Ant. See DERBY-6685.
+ grant codeBase "${derbyTesting.ant}" {
+    permission java.io.FilePermission "${user.dir}${/}*", "write";
+ };
+
  • ์„ค๋ช… : Ant 1.9.3 ๋ฒ„์ „๋ถ€ํ„ฐ Test result๋ฅผ file์— ์“ฐ๊ธฐ ์œ„ํ•ด ant.jar์— write permission์ด ํ•„์š”ํ•ด์กŒ๋Š”๋ฐ ๊ด€๋ จ ๊ถŒํ•œ ์„ค์ •์ด ์‹คํ–‰๋˜์ง€ ์•Š์•˜๋‹ค.
  • ์ถœ์ฒ˜

40. ๋ชจ๋“  test ๊ฐ€ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S1, P4-
  • ์˜ˆ์ œ ์ฝ”๋“œ
public static void runTests( final SecurityEvaluator securityEvaluator, final PrefixMapping prefixMapping ) throws Exception
{
    ...
    Method lockTest = null;
    for (final Method m : pmTest.getClass().getMethods())
    {
        if (m.isAnnotationPresent(Test.class))
        {
        // lock test must come last
            if (m.getName().equals("testLock")) { lockTest = m; }
            else { pmTest.setup(); m.invoke(pmTest); }
        }
    }
+   Assert.assertNotNull( "Did not find 'testLock' method", lockTest );     
    pmTest.setup();
    lockTest.invoke(pmTest);
}
  • ์„ค๋ช… : ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๋ถ€๋ถ„์—์„œ ์‹ค์ œ ์˜๋„ํ•˜๋Š”๋Œ€๋กœ ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์ง€ ์•Š์•˜๋‹ค.

  • ์ถœ์ฒ˜

41. test ๊ฐ€ ์ถ”๊ฐ€๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•  ๋ฉ”์†Œ๋“œ leasechecker.interruptAndJoin()์ด ๋ˆ„๋ฝ๋จ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S2, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
Index: src/test/hdfs/org/apache/hadoop/hdfs/TestReadWhileWriting.java
===================================================================
--- src/test/hdfs/org/apache/hadoop/hdfs/TestReadWhileWriting.java  (revision 889032)
+++ src/test/hdfs/org/apache/hadoop/hdfs/TestReadWhileWriting.java  (working copy)
@@ -83,6 +83,8 @@ 
       //b. On another machine M2, open file and verify that the half-block
       //   of data can be read successfully.
  01   @Test
  02   public void pipeline_02_03() throws Exception {
  03    ...
  04   checkFile(p, half, conf);
+ 05   AppendTestUtil.LOG.info("leasechecker.interruptAndJoin()");
+ 06   ((DistributedFileSystem)fs).dfs.leasechecker.interruptAndJoin();
  07   //c. On M1, append another half block of data.  Close file on M1.
  08   {
  09     //sleep to let the lease is expired.
  10     Thread.sleep(2*LEASE_LIMIT);
  11 
  12     final DistributedFileSystem dfs = (DistributedFileSystem)FileSystem.newInstance(conf);
  13     final FSDataOutputStream out = append(dfs, p);
  14     write(out, 0, half);
  15     out.close();
  16   }
  • ์„ค๋ช… : Test๊ฐ€ ์ถ”๊ฐ€๋˜๊ธฐ์ „์— ๋ฐ˜๋“œ์‹œ ํ˜ธ์ถœ๋˜์–ด์•ผํ•  ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ˆ„๋ฝ๋˜์—ˆ๋‹ค.

  • ์ถœ์ฒ˜

42. ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๋Š” ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฐ์†ํ•ด ์ค‘๋ณต ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S2, P1
  • ์„ค๋ช… : ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฐ์†ํ•ด์„œ ์ค‘๋ณต ํ˜ธ์ถœํ•˜์˜€๋‹ค.
  • ์˜ˆ์ œ ์ฝ”๋“œ
   c.setInt("hbase.ipc.client.connect.max.retries", 1);
-  c.setInt("hbase.ipc.client.connect.max.retries", 1);
   c.setInt("dfs.client.block.recovery.retries", 1);

43. setup() ์ด๋‚˜ teardown()์—์„œ Tree์˜ child node๋“ค์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š์Œ (์„ค์ • ์ดˆ๊ธฐํ™” ์˜ค๋ฅ˜)

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S2, S6, P2
  • ์˜ˆ์ œ ์ฝ”๋“œ
    protected Node cleanUpTestRoot(Session s) throws RepositoryException
    ...
    // clean test root
    testRootNode = root.getNode(testPath);
    for (NodeIterator children = testRootNode.getNodes(); children.hasNext();) {
-     children.nextNode().remove();
+     Node child = children.nextNode();
+     NodeDefinition nodeDef = child.getDefinition();
+     if (!nodeDef.isMandatory() && !nodeDef.isProtected()) {
+       child.remove();
+     }
    }
  • ์„ค๋ช… : ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ์—์„œ ์ ์ ˆํ•œ ํ™˜๊ฒฝ ์„ค์ •(children node์˜ ์ œ๊ฑฐ)์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์•„์„œ, ์‹ค์ œ ์œ ๋‹›์˜ ์‹คํ–‰๊ณผ๋Š” ์ƒ๊ด€์—†์ด ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํŒจํ•œ๋‹ค. JCR-505: TCK: AbstractJCRTest fails if setUp/tearDown cannot remove children of test node
  • ์ถœ์ฒ˜

44. Exception๊ฐ€ Throw๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋‚˜ ๊ทธ๊ฒƒ์„ ํ…Œ์ŠคํŠธํ•˜์ง€ ์•Š์Œ

  • ๊ตฌ์„ฑ ์š”์†Œ : R4, S5, P1
  • ์˜ˆ์ œ ์ฝ”๋“œ
   public void testGetInvalidJob() throws Exception {
-    RunningJob runJob = new JobClient(getJobConf()).getJob(JobID.forName("job_0_0"));
-    assertNull(runJob);
+    try {
+      RunningJob runJob = new JobClient(getJobConf()).getJob(JobID.forName("job_0_0"));
+      fail("Exception is expected to thrown ahead!");
+    } catch (Exception e) {
+      assertTrue(e instanceof IOException);
+      assertTrue(e.getMessage().contains("ApplicationNotFoundException"));
+    }
   }
  • ์„ค๋ช… : ํ…Œ์ŠคํŠธ ๋Œ€์ƒ ์ฝ”๋“œ๊ฐ€ null์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์—์„œ IOException์„ throwํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณ€๊ฒฝ๋˜์–ด Test code๋„ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•„ Test๊ฐ€ ์‹คํŒจํ•œ๋‹ค.
  • ์ถœ์ฒ˜

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors