Junit 테스트 케이스를 작성하는 데 매우 복잡한 클래스가 있습니다. 테스트를 수행 할 클래스에는 생성자 초기화가 있으므로 PowerMockito를 사용하기로 결정했습니다.InjectMocks 오류를 사용하는 PowerMockito
이public class MainClass extends BaseClass{
MainClass(SomeClass class){
super(class);
}
public void methodToBeTested(){
some code here
}
..few other methods which I am not going to test.
}
지금은 테스트 케이스는 다음과 같이 썼다 :
내 주요 클래스는 다음과 같이이다
그 이후 테스트 케이스를 실행할 때 나는 널 포인터 예외을 얻고있다@RunWith(PowerMockRunner.class)
public class TestClass{
@Mock
OtherClassUsedInMainClass mock1;
@Mock
OtherClassUsedInMainClass mock2;
@InjectMocks
MainClass mainClass;
@Before
public void setUp() throws Exception{
MockitoAnnotations.initMocks(this);
PowerMockito.whenNew(MainClass.class).withArguments(Mockito.any(SomeClass.class))
.thenReturn(mainClass);)
}
@Test
public void testMethodtobeTested(){
...I am using the other objects to mock data and test if this method works fine
mainClass.methodtobeTested();
\\This method will increment a value. I am just asserting if that value is right.
Assert.assertEquals(mainClass.checkCount(),RequiredCount)
}
}
mainClass을 초기화하려고합니다. 그것은 조롱받지 않습니다. 내가 뭔가 잘못하고있는 것을 안다. 하지만 그게 뭔지 모르겠습니다.
오류 :
org.mockito.exceptions.base.MockitoException:
Cannot instantiate @InjectMocks field named 'mainClass' of type 'class com.main.MainClass'.
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null
Caused by: java.lang.NullPointerException
This null pointer exception is thrown from a the constructor of the BaseClass when it tries to initialize another class.
는 BaseClass로의 생성자를 표시합니다. 오류가 발생했습니다 – Jens
왜 MainClass를 스터 빙하고 있습니까? 당신이 테스트하는 수업이 아닌가요? –
@Jens 기본 클래스 생성자는 mainClass에서 전달 된 someClass를 인수로 취합니다. 또한이 someClass를 사용하고 someClass를 사용하는 생성자를 가진 몇몇 다른 클래스를 초기화하는 데이 클래스를 사용합니다. – v1shnu