Skip to content

hyeoni90/java-application-test-way

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Java Application Testing Way Example

Java, Spring μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ ν…ŒμŠ€νŠΈ ν•˜λŠ” 방법듀에 λŒ€ν•œ 예제

Junit5

ν…ŒμŠ€νŠΈ κΈ°λ³Έ 원칙

λ‹¨μœ„ ν…ŒμŠ€νŠΈ 원칙 - F.I.R.S.T

  • Fast: μœ λ‹›ν…ŒμŠ€νŠΈλŠ” 빨라야 ν•œλ‹€.
  • Isolated: λ‹€λ₯Έ ν…ŒμŠ€νŠΈμ— 쒅속적인 ν…ŒμŠ€νŠΈλŠ” μ ˆλŒ€ μž‘μ„±ν•˜μ§€ μ•ŠλŠ”λ‹€.
  • Repeatable: ν…ŒμŠ€νŠΈλŠ” μ‹€ν–‰ν•  λ•Œ λ§ˆλ‹€ 같은 κ²°κ³Όλ₯Ό λ§Œλ“€μ–΄μ•Όν•œλ‹€.
  • Self-validating: ν…ŒμŠ€νŠΈλŠ” 슀슀둜 결과물에 λŒ€ν•œ 옳고 그름을 νŒλ‹¨ν•  수 μžˆμ–΄μ•Ό ν•œλ‹€.
  • Timely: μœ λ‹› ν…ŒμŠ€νŠΈλŠ” production μ½”λ“œκ°€ ν…ŒμŠ€νŠΈλ₯Ό ν†΅κ³Όν•˜κΈ° 직전에 κ΅¬μ„±λ˜μ–΄μ•Ό ν•œλ‹€. μ΄λŠ” ν…ŒμŠ€νŠΈ 주도 개발(TDD) 방법둠에 μ ν•©ν•œ μ›μΉ™μ΄λ‚˜ μ‹€μ œλ‘œ μ μš©λ˜μ§€ μ•ŠλŠ” κ²½μš°λ„ μžˆλ‹€.

λ‹¨μœ„ ν…ŒμŠ€νŠΈ (unit test), 톡합 ν…ŒμŠ€νŠΈ (integration test), μΈμˆ˜ν…ŒμŠ€νŠΈ (acceptance test) λ“± λ‹€μ–‘ν•œ ν…ŒμŠ€νŠΈκ°€ μ‘΄μž¬ν•œλ‹€. 각 ν…ŒμŠ€νŠΈμ˜ λͺ©μ κ³Ό 상황에 맞게 ν…ŒμŠ€νŠΈλ₯Ό κ΅¬μ„±ν•˜λŠ” 것도 μ€‘μš”ν•˜λ‹€.

  • Assertion ν™œμš©ν•œ ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„± 방법

Extension Model

  • Junit 4: @RunWith(Runner), TestRule, MethodRule
  • Junit 5: Extension

Register Extension

  1. Declarative Registration - @ExtendWith({extensionInstanceName}.class)
@ExtendWith({FindSlowTestExtension}.class)
class ReportTest {
    ...
}
  1. Programmatic Registration - @RegisterExtension
class ReportTest {
    
    @RegisterExtension
    static FindSlowTestExtension findSlowTestExtension = new FindSlowTestExtension({THRESHOLD});
}
  1. Automatic Registration - ServiceLoader
ex) junit-platform.properties

junit.jupiter.extensions.autodetection.enabled = true

References

Mockito

Mockito λž€?

Mock(κ°€μ§œ 객체)을 생성, κ΄€λ¦¬ν•˜κ³  κ²€μ¦ν•˜μ—¬ λ‹¨μœ„ ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•œ ν”„λ ˆμž„μ›Œν¬

μ“°λŠ” 이유?

  • 컨트둀 ν•˜κΈ° μ–΄λ €μš΄ 뢀뢄을 Mock 객체둜 λŒ€μ²΄ν•΄ ν…ŒμŠ€νŠΈν•˜λ €λŠ” 뢀뢄에 집쀑할 수 μžˆλ‹€ (ex. payment, external API, ...)
  • κ°„ν˜Ή λ³΅μž‘ν•  수 μžˆλŠ” μ˜μ‘΄μ„±μ„ κ°„μ†Œν™”μ‹œν‚€κ³ , ν…ŒμŠ€νŠΈ μ‹€ν–‰ 속도λ₯Ό ν–₯μƒμ‹œν‚¨λ‹€.

Mockito μ„€μ •

Spring Boot 2.2.x 이상 버전 spring-boot-starter-test μ—μ„œ μžλ™μœΌλ‘œ μΆ”κ°€ ν•΄μ€€λ‹€.

spring-boot-starter-test > mockito-core, mockito-junit-jupiter

λ§Œμ•½ μ—†λ‹€λ©΄ (Spring Bootλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ±°λ‚˜), maven repo μ—μ„œ μ•„λž˜ dependencyλ₯Ό μΆ”κ°€ν•œλ‹€.

  • gradle > build.gradle
testImplementation 'org.mockito:mockito-core:3.7.0'
testImplementation 'org.mockito:mockito-junit-jupiter:3.7.0'
  • maven > pom.xml
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>3.7.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>3.7.0</version>
    <scope>test</scope>
</dependency>

References

JMeter

μ„±λŠ₯ μΈ‘μ • 및 λΆ€ν•˜ ν…ŒμŠ€νŠΈ κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” μ˜€ν”ˆ μ†ŒμŠ€ μžλ°” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μœΌλ‘œ, CLI λ₯Ό μ§€μ›ν•˜μ—¬ CI or CD 툴과 μ—°λ™ν• λ•Œ νŽΈλ¦¬ν•˜λ©° UI μ‚¬μš©ν•˜λŠ” 것보닀 λ©”λͺ¨λ¦¬ λ“± μ‹œμŠ€ν…œ λ¦¬μ†ŒμŠ€λ₯Ό 적게 μ‚¬μš©ν•œλ‹€.

** JMeter 도 μ‹œμŠ€ν…œ λ¦¬μ†ŒμŠ€λ₯Ό μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ—, ν…ŒμŠ€νŠΈ ν•˜κ³ μž ν•˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜κ³Ό μ„œλ²„κ°€ λΆ„λ¦¬λœ ν™˜κ²½μ—μ„œ ν…ŒμŠ€νŠΈ ν•΄μ•Όν•œλ‹€.

μ• ν”Œλ¦¬μΌ€μ΄μ…˜ ν…ŒμŠ€νŠΈ 지원 ν˜•νƒœ )

  • μ›Ή http, https
  • SOAP / REST web service
  • FTP
  • DataBase (JDBC μ‚¬μš©)
  • Mail (SMTP, POP3, IMAP)
  • etc ...

μ£Όμš” κ°œλ…

  • Thread Group: ν•œ μŠ€λ ˆλ“œ λ‹Ή μœ μ € ν•œλͺ… (μœ μ €μ˜ 그룹이라고 이해 ν•˜κ³ , μœ μ €μ˜ 수)
  • Sampler: μ–΄λ–€ μœ μ €κ°€ ν•΄μ•Όν•˜λŠ” μ•‘μ…˜ (HTTP μš”μ²­ 같은 것듀을 ν•˜λ‚˜μ˜ sampler 라고 λ³Έλ‹€.)
  • Listener: 응닡을 λ°›μ•˜μ„ ν•  일 (λ¦¬ν¬νŒ…, 검증, κ·Έλž˜ν”„ 그리기 λ“±)
  • Configuration: Sampler κ°€ μ‚¬μš©ν•  μ„€μ • κ°’ (HTTP header, Cookie, JDBC connection ...)
  • Assertion: 응닡이 성곡적인지 ν™•μΈν•˜λŠ” 방법

JMeter μ„€μΉ˜ 및 μ‹€ν–‰

  1. Binary μ••μΆ• 파일 λ‹€μš΄λ‘œλ“œ [http://jmeter.apache.org/download_jmeter.cgi ] Binarires > Download apache-jmeter-5.4.zip > Unzip download file
$ cd Download/apache-jmeter-5.4
$ cd bin ./jmeter
  1. Homebrew 둜 μ„€μΉ˜ν•˜κΈ° [https://formulae.brew.sh/formula/jmeter]
$ brew install jmeter
$ brew upgrade jmeter 
$ open /usr/local/bin/jmeter 

Reference