小编use*_*855的帖子

带@ EnableJpa*注释的Spring @WebMvcTest

我有以下主类.

@EnableJpaAuditing
@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
public class SampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

以及单元测试类.

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = { AccountController.class }, secure = false)
public class AccountControllerTest {

    @Autowired
    private MockMvc mockMvc;
    // Commented out below
Run Code Online (Sandbox Code Playgroud)

如果我要运行单元测试,它会给我这个错误.

IllegalArgumentException:必须至少存在一个JPA元模型!

所以我研究了一下,发现它是由于@EnableJpa*注释在使用时放在Main类中@WebMvcTest,所以它没有找到@Entity类,因此也就是错误信息.

我遇到了一个页面,并描述了我或多或少相同的问题,并建议使用@SpringBootTest而@AutoConfigureMockMvc不是@WebMvcTest.

这是唯一的方法吗?我希望能够使用,@WebMvcTest因为使用@SpringBootTest加载完整的上下文,并需要更长的时间来运行测试.

根据要求包括测试类.

@WebMvcTest(controllers = { AccountController.class }, secure = false)
public class AccountControllerTest {

@Autowired
private …
Run Code Online (Sandbox Code Playgroud)

java unit-testing spring-data-jpa spring-boot

13
推荐指数
1
解决办法
1578
查看次数

使用Spring @TransactionalEventListener发布事件时的奇怪(循环)行为

我有一个奇怪的问题,涉及@TransactionalEventListener没有正确触发或行为按预期由另一个触发@TransactionalEventListener.

一般流程是:

  • AccountService发布一个事件(到AccountEventListener)
  • AccountEventListener侦听事件
  • 执行一些处理,然后发布另一个事件(到MailEventListener)
  • MailEventListener侦听事件并执行某些处理

所以这是课程(摘录).

public class AccountService {

    @Transactional
    public User createAccount(Form registrationForm) {

        // Some processing

        // Persist the entity
        this.accountRepository.save(userAccount);

        // Publish the Event
        this.applicationEventPublisher.publishEvent(new RegistrationEvent());
    }
}

public class AccountEventListener {

    @TransactionalEventListener
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public MailEvent onAccountCreated(RegistrationEvent registrationEvent) {

        // Some processing

        // Persist the entity
        this.accountRepository.save(userAccount);

        return new MailEvent();
    }
}

public class MailEventListener {

    private final MailService mailService;

    @Async
    @EventListener
    public void onAccountCreated(MailEvent mailEvent) {

        this.mailService.prepareAndSend(mailEvent);
    } …
Run Code Online (Sandbox Code Playgroud)

java spring event-handling spring-boot

10
推荐指数
1
解决办法
262
查看次数

即时解密音频/视频到MediaPlayer

我一直在寻找几天,但无法得到如何做到这一点的明确答案.

我的SD卡上有一些加密的音频/视频文件,我想将它发送到MediaPlayer,并在解密工作的同时开始播放.它最初会创建一个缓冲区,当它有足够的初始数据开始播放时,MediaPlayer将开始播放,并在后台解密并继续发送数据.

我环顾四周,大部分解决方案似乎都指向了localhttpserver,然后将文件发送到服务器,当它返回时,它将开始播放音频/视频,就像直播一样.我不知道的是解密代码在哪里?在服务器端?如果是这样,在哪一部分呢?

我也看过这个http://libeasy.alwaysdata.net/解决方案,它可能能够解决它,但我似乎无法理解它是如何工作的.我知道它创建了一个本地服务器,但在Cipher部分,我自己的解密如何发挥作用?

private void myPlay(String path) {
    mServer = new LocalSingleHttpServer();
    mServer.setCipher(myGetCipher());
    mServer.start();
    path = mServer.getURL(path);
    mVideoView.setVideoPath(path);
    mVideoView.start();
}
public void onCompletion(MediaPlayer mp) { // MediaPlayer.OnCompletionListener interface
    mServer.stop();
} 
Run Code Online (Sandbox Code Playgroud)

由于我对Android开发很陌生,如果我不清楚或任何事情,请耐心等待.

谢谢.

encryption audio video android media-player

6
推荐指数
1
解决办法
2531
查看次数

验证器的 Spring 单元测试问题

我正在尝试为我拥有的验证器类编写单元测试。因此,在我的 UniqueEmailValidator 类中,我注入了一个 @Service 组件来检查它是否存在。

@AllArgsConstructor
public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, String> {
    
    private final AccountService accountService;
    
    @Override
    public void initialize(final UniqueEmail constraintAnnotation) {
    }

    @Override
    public boolean isValid(final String email, final ConstraintValidatorContext context) {
        return !this.accountService.findByEmail(email).isPresent();
    }
}

@Documented
@Target({TYPE, FIELD, ANNOTATION_TYPE}) 
@Retention(RUNTIME)
@Constraint(validatedBy = UniqueEmailValidator.class)
public @interface UniqueEmail {
    
    String message() default "{com.x.x.validator.UniqueEmail.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)

我尝试为这个约束验证器编写单元测试。

@RunWith(SpringRunner.class)
@DataJpaTest
public class AccountValidatorTest {
    
    private static Validator validator;

    @BeforeClass
    public static …
Run Code Online (Sandbox Code Playgroud)

java validation junit spring spring-boot

4
推荐指数
1
解决办法
4996
查看次数

Group,Sum byType然后使用Java流获得diff

我想使用流来分组,按类型获取总和,然后按类型查找不同的结果.

所以这是我的数据集.

Sample(SampleId=1, SampleTypeId=1, SampleQuantity=5, SampleType=ADD), 
Sample(SampleId=2, SampleTypeId=1, SampleQuantity=15, SampleType=ADD), 
Sample(SampleId=3, SampleTypeId=1, SampleQuantity=25, SampleType=ADD), 
Sample(SampleId=4, SampleTypeId=1, SampleQuantity=5, SampleType=SUBTRACT), 
Sample(SampleId=5, SampleTypeId=1, SampleQuantity=25, SampleType=SUBTRACT) 
Sample(SampleId=6, SampleTypeId=2, SampleQuantity=10, SampleType=ADD), 
Sample(SampleId=7, SampleTypeId=2, SampleQuantity=20, SampleType=ADD), 
Sample(SampleId=8, SampleTypeId=2, SampleQuantity=30, SampleType=ADD), 
Sample(SampleId=9, SampleTypeId=2, SampleQuantity=15, SampleType=SUBTRACT), 
Sample(SampleId=10, SampleTypeId=2, SampleQuantity=35, SampleType=SUBTRACT)
Run Code Online (Sandbox Code Playgroud)

我目前的做法是这样的:

Map<Long, Integer> result = sampleList.stream()
.collect(
    Collectors.groupingBy(
        Sample::getSampleTypeId, 
        Collectors.summingInt(Sample::getSampleQuantity)
        )
    );
Run Code Online (Sandbox Code Playgroud)

我的结果是这样的(SampleTypeId,结果):

{1=75, 2=110}
Run Code Online (Sandbox Code Playgroud)

我不知道怎么从这里继续.基本上,有一个ADD或SUBTRACT的SampleType.所以我需要SUM所有TypeId = 1和Type = ADD AND SUM所有typeId = 1和Type = SUBSTRACT然后找到差异.

例

1 >> (5+15+25) - (5+25) = 15
2 >> (10+20+30) - …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream collectors

3
推荐指数
1
解决办法
99
查看次数