jUnit의 다중 RunWith 문
단위테스트를 사용하고 요.JUnitParamsRunner
★★★★★★★★★★★★★★★★★」MockitoJUnitRunner
1번입니다.
유감스럽게도 다음 기능은 작동하지 않습니다.
@RunWith(MockitoJUnitRunner.class)
@RunWith(JUnitParamsRunner.class)
public class DatabaseModelTest {
// some tests
}
Mockito와 JUnitParams를 한 번의 테스트 클래스에서 모두 사용할 수 있는 방법이 있습니까?
사양에 따라 동일한 주석 요소에 동일한 주석을 두 번 달 수 없기 때문에 이 작업을 수행할 수 없습니다.
면면면 면면 면면 면면 면? 면? 면?은 하나만 예요.@RunWith()
러너 없이는 다른 것을 다른 것으로 대체할 수 없습니다.의 , ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★MockitoJUnitRunner
프로그램적으로 할 수 있어요
실제로 실행되는 것은 다음과 같습니다.
MockitoAnnotations.initMocks(test);
테스트 케이스의 선두에 있습니다.가장 은 이 를 이 코드 안에 입니다.setUp()
★★★★
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
확실하지 않지만 플래그를 사용하여 이 메서드를 여러 번 호출하지 않도록 해야 합니다.
private boolean mockInitialized = false;
@Before
public void setUp() {
if (!mockInitialized) {
MockitoAnnotations.initMocks(this);
mockInitialized = true;
}
}
그러나 JUNt 규칙을 사용하여 재사용 가능한 솔루션을 구현할 수도 있습니다.
public class MockitoRule extends TestWatcher {
private boolean mockInitialized = false;
@Override
protected void starting(Description d) {
if (!mockInitialized) {
MockitoAnnotations.initMocks(this);
mockInitialized = true;
}
}
}
이제 테스트 클래스에 다음 행을 추가합니다.
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
원하는 주자와 함께 이 테스트 케이스를 실행할 수 있습니다.
JUnit 4.7 및 Mockito 1.10.17에서는 이 기능이 내장되어 있으며 클래스가 있습니다.간단하게 Import하여 라인을 추가할 수 있습니다.
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
테스트 클래스까지.
이 솔루션은 이 모키토 예뿐만 아니라 가능한 모든 주자에게 적용됩니다.예를 들어, 스프링의 경우 런너 클래스를 변경하고 필요한 주석을 추가합니다.
@RunWith(JUnitParamsRunner.class)
public class DatabaseModelTest {
@Test
public void subRunner() throws Exception {
JUnitCore.runClasses(TestMockitoJUnitRunner.class);
}
@RunWith(MockitoJUnitRunner.class)
public static class TestMockitoJUnitRunner {
}
}
DatabaseModelTest
쥬니트 TestMockitoJUnitRunner
(논리에 의해) 그것에 의존하며, 그것은 메인 안에서 실행될 것이다.@Test
,콜 중 " " "JUnitCore.runClasses(TestMockitoJUnitRunner.class)
, 러너가 이 보증됩니다static class TestMockitoJUnitRunner
되어 여러 의 네스트된 "" " " " " " " 가 효과적으로 됩니다.@RunWith
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
https://bekce.github.io/junit-multiple-runwith-dependent-tests에도 게재되어 있습니다.
PowerMock 1.6이 출시되었기 때문에 다음과 같은 작업을 쉽게 수행할 수 있습니다.
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(JUnitParamsRunner.class)
public class DatabaseModelTest {
// some tests
}
여기서 설명하겠습니다.https://blog.jayway.com/2014/11/29/using-another-junit-runner-with-powermock/
저 같은 경우에는 봄콩에 어떤 방법을 모킹하려다가
MockitoAnnotations.initMocks(test);
동작하지 않습니다.대신 다음과 같이 xml 파일 내에서 mock 메서드를 사용하여 구성할 bean을 정의해야 합니다.
...
<bean id="classWantedToBeMocked" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.fullpath.ClassWantedToBeMocked" />
</bean>
...
시험시간에 자동배선장치가 있는 콩을 이렇게 넣어주세요.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="file:springconfig.xml")
public class TestClass {
...
@Autowired
private ClassWantedToBeMocked classWantedToBeMocked;
...
when(classWantedToBeMocked.methodWantedToBeMocked()).thenReturn(...);
...
}
@RunWith(Parameterized.class) - outerrunner - @RunWith(Mockito)를 조합한 접근방식을 사용하여 이 링크를 확인합니다.JUnitRunner.class) - 내부 러너외부 클래스/러너의 멤버 변수를 정적 상태로 만들어 내부/내부 러너/클래스에 액세스할 수 있도록 하는 것이 유일한 수정 사항입니다.행운을 빌고 즐겨요.
SWTBotJunit4ClassRunner와 org.junit.runners를 실행하고 싶었습니다.동시에 파라미터화된 파라미터 테스트가 있어 SWT 테스트 실패 시 스크린샷을 하고 싶습니다(스크린샷 기능은 SWTotJunit4ClassRunner에서 제공).@bekce의 답변은 훌륭했고 처음에는 그 길을 가고 싶었지만, 논쟁을 통과하기 위한 기발한 방법이었다.또는 서브클래스에서 매개 변수를 실행하여 정확한 테스트에 합격/불합격하고 마지막 스크린샷만 있는 정보를 손실합니다(스크린샷 이름이 테스트 자체에서 이름을 얻음).그래서 어느 쪽이든 그것은 좀 지저분했다.
SWTBotJunit4ClassRunner는 매우 심플하기 때문에 클래스의 소스 코드를 복제하고 ParametrizedScreenshotRunner라는 이름을 붙였습니다.원래의 TestRunner가 확장되어 있던 곳에서, 제 클래스는 파라메타라이즈드 클래스를 확장하고 있기 때문에, 기본적으로는 앞의 2개의 러너를 사용할 수 있습니다.내 러너를 줄여서 그 위에 스크린샷 기능을 구현하면서 파라미터화된 러너 위로 확장했습니다.이제 테스트에서는 이 "하이브리드" 러너를 사용하여 모든 테스트가 예상대로 즉시 작동합니다(테스트 내에서는 아무것도 변경할 필요가 없습니다.
다음과 같습니다(간단히 하기 위해 리스트에서 모든 코멘트를 삭제했습니다).
package mySwtTests;
import org.junit.runners.Parameterized;
import org.eclipse.swtbot.swt.finder.junit.ScreenshotCaptureListener;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
public class ParametrizedScreenshotRunner extends TestRu Parameterized {
public ParametrizedScreenshotRunner(Class<?> klass) throws Throwable {
super(klass);
}
public void run(RunNotifier notifier) {
RunListener failureSpy = new ScreenshotCaptureListener();
notifier.removeListener(failureSpy); // remove existing listeners that could be added by suite or class runners
notifier.addListener(failureSpy);
try {
super.run(notifier);
} finally {
notifier.removeListener(failureSpy);
}
}
}
JUnit 4에는 솔루션이 없지만 JUnit 5에는 여러 내선번호를 등록할 수 있습니다.
@ExtendWith(MockitoExtension.class)
@ExtendWith(AnotherExtension.class)
public class MyTest {
// some tests
}
주의:JUnitParams
프레임워크는 JUnit 5에 포함되어 있습니다.
다음의 조작도 실행할 수 있습니다.
@RunWith(JUnitParamsRunner.class)
public class AbstractTestClass {
// some tests
}
@RunWith(MockitoJUnitRunner.class)
public class DatabaseModelTest extends AbstractTestClass {
// some tests
}
언급URL : https://stackoverflow.com/questions/24431427/multiple-runwith-statements-in-junit
'programing' 카테고리의 다른 글
문자열의 문자가 대문자인지 소문자인지 JavaScript를 사용하여 테스트하려면 어떻게 해야 합니까? (0) | 2022.09.19 |
---|---|
Composer create-project와 함께 특정 larabel 버전 설치 (0) | 2022.09.19 |
IP 주소로 ping을 실행할 수 있는데 InetAddress.isReachable이 false를 반환하는 이유는 무엇입니까? (0) | 2022.09.19 |
여러 체크박스에서 $_POST 가져오기 (0) | 2022.09.19 |
GROUP_CONCAT를 사용하여 짝수와 홀수를 분할하는 쿼리 (0) | 2022.09.19 |