选项1:在
选项2:用
我正在尝试编写一个嘲讽测试,不幸的是出现了问题。我的课程如下:
我的测试课:
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
public class TestClass {
@Nested
public class testingPersonClass{
@InjectMocks
public Person personOne = new Person("zakzak", 37);
@Mock
public Hobbie hobbie = new Hobbie("Sport");
@Test
public void tesOne(){
System.out.println("personOne "+personOne.toString());
}
}
}
类Person
:
public class Person {
String name;
int age;
Hobbie hobbie;
Person(String name, int age){
this.age=age;
this.name=name;
}
//getters and setters
@Override
public String toString() {
return this.name+" "+this.age+" "+ this.hobbie.toString();
}
}
类Hobbie
:
public class Hobbie {
String hobbie;
public Hobbie( String hobbie ) {
this.hobbie = hobbie;
}
@Override
public String toString() {
return this.hobbie;
}
}
我得到了
java.lang.nullpointerexception,
因为hobbie
对象尚未注入personOne
对象。
我该如何解决?
我的pom依赖性:
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
您应该使用以下选项之一初始化单元测试中的Mockito模拟:
选项1:在@BeforeEach
(junit 4中的@Before
(中,方法
package stackoverflow;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class TestClass {
@Nested
public class testingPersonClass {
@InjectMocks
public Person personOne = new Person("zakzak", 37);
@Mock
public Hobbie hobbie = new Hobbie("Sport");
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testOne() {
System.out.println("personOne " + personOne.toString());
}
}
}
选项2:用@ExtendWith(MockitoExtension.class)
(JUNIT 4中的@RunWith(MockitoJUnitRunner.class)
(注释您的测试类
对于此方法,您必须将以下依赖关系添加到pom.xml
:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
请注意,我已经评论了init
方法。
package stackoverflow;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class TestClass {
@Nested
public class testingPersonClass {
@InjectMocks
public Person personOne = new Person("zakzak", 37);
@Mock
public Hobbie hobbie = new Hobbie("Sport");
/*
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
}
*/
@Test
public void testOne() {
System.out.println("personOne " + personOne.toString());
}
}
}
有用的链接
您可以找到这些文章有帮助:
- https://www.baeldung.com/mockito-annotations
- https://www.baeldung.com/mockito-junit-5-extension
尝试一下,它对我有用
您必须将业余爱好设置为Personone以将其联系起来,在您的情况下,爱好和Personone没有链接
@RunWith(MockitoJUnitRunner.class)
public class TestClass {
@InjectMocks
public Person personOne = new Person("zakzak", 37);
@Autowired
public Hobbie hobbie = new Hobbie("Sport");
@Test
public void tesOne(){
personOne.setHobbie(hobbie);
System.out.println(personOne.toString());
Assert.assertEquals("zakzak 37 Sport",personOne.toString() );
}
}