Junit 中的自动装配
Autowiring in Junit
我正在尝试从上下文中自动装配两个对象,但似乎在 Configuraiton
和 JavaMailSender
上都出现 Nullpointer 异常,我可能会遗漏什么。如果我只是自动装配 EmailService
,它可以正常工作,但是我需要将模拟对象传递给构造函数,这就是为什么我要按照现在的方式进行。如果我用 @Autowire
注释我的 EmailService
对象,它会起作用。
@SpringBootTest
public class EmailServiceTest {
@Autowired
private Configuration configuration;
@Autowired
private JavaMailSender javaMailSender;
private AuthUser authUser = Mockito.mock(AuthUser.class);
private EmailService emailService = new EmailService(javaMailSender,configuration,authUser);
@Test
public void testSendAttachment() throws IOException {
List<Attachment> attachments = new ArrayList<>();
attachments.add(new Attachment(new Archive(TestData.getDocs(), "test").getZipArchive(), "documents.zip"));
attachments.add(new Attachment(new Archive(TestData.getDocs(), "test").getZipArchive(), "documents2.zip"));
Email mail = new Email("a",
"b",
"Requested archive attached",
"Thank you for using our services, please refer to the attached archive", attachments);
emailService.sendMailWithAttachment(mail);
}
@Test
public void shouldSendConfirmationEmail(){
Mockito.when(authUser.getUsername()).thenReturn("email@email.com");
emailService.sendConfirmationToCooperateEmail();
}
Spring 创建 EmailServiceTest.java
属性 Configuration 和 EmailService 默认为 null
当我们在 属性 初始化 EmailService 时
电子邮件服务立即获取空值,然后才 Spring 将值注入配置和 EmailSender。
@Autowired
private Configuration configuration;
@Autowired
private JavaMailSender javaMailSender;
private AuthUser authUser = Mockito.mock(AuthUser.class);
@Test
public void testSendAttachment() throws IOException {
EmailService emailService = new EmailService(javaMailSender, configuration, authUser);
我正在尝试从上下文中自动装配两个对象,但似乎在 Configuraiton
和 JavaMailSender
上都出现 Nullpointer 异常,我可能会遗漏什么。如果我只是自动装配 EmailService
,它可以正常工作,但是我需要将模拟对象传递给构造函数,这就是为什么我要按照现在的方式进行。如果我用 @Autowire
注释我的 EmailService
对象,它会起作用。
@SpringBootTest
public class EmailServiceTest {
@Autowired
private Configuration configuration;
@Autowired
private JavaMailSender javaMailSender;
private AuthUser authUser = Mockito.mock(AuthUser.class);
private EmailService emailService = new EmailService(javaMailSender,configuration,authUser);
@Test
public void testSendAttachment() throws IOException {
List<Attachment> attachments = new ArrayList<>();
attachments.add(new Attachment(new Archive(TestData.getDocs(), "test").getZipArchive(), "documents.zip"));
attachments.add(new Attachment(new Archive(TestData.getDocs(), "test").getZipArchive(), "documents2.zip"));
Email mail = new Email("a",
"b",
"Requested archive attached",
"Thank you for using our services, please refer to the attached archive", attachments);
emailService.sendMailWithAttachment(mail);
}
@Test
public void shouldSendConfirmationEmail(){
Mockito.when(authUser.getUsername()).thenReturn("email@email.com");
emailService.sendConfirmationToCooperateEmail();
}
Spring 创建 EmailServiceTest.java 属性 Configuration 和 EmailService 默认为 null 当我们在 属性 初始化 EmailService 时 电子邮件服务立即获取空值,然后才 Spring 将值注入配置和 EmailSender。
@Autowired
private Configuration configuration;
@Autowired
private JavaMailSender javaMailSender;
private AuthUser authUser = Mockito.mock(AuthUser.class);
@Test
public void testSendAttachment() throws IOException {
EmailService emailService = new EmailService(javaMailSender, configuration, authUser);