uniquejava / blog

My notes regarding the vibrating frontend :boom and the plain old java :rofl.
Creative Commons Zero v1.0 Universal
11 stars 5 forks source link

SpringBoot Test #300

Open uniquejava opened 4 years ago

uniquejava commented 4 years ago

Integration Tests with @SpringBootTest: https://reflectoring.io/spring-boot-test/

junit4测试 mockmvc + mockito junit5测试 mockmvc + mockito https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing

junit4测试spring security oauth2 https://docs.spring.io/spring-security/site/docs/current/reference/html5/#test

junit5测试spring security oauth2

我碰到的问题 MockBean始终为null.

解决方案在这。 两种
https://github.com/spring-projects/spring-boot/issues/7689#issuecomment-615574514

原因在这: https://github.com/spring-projects/spring-boot/issues/9609

我都加了comment :joy:

是继承了 AbstractTransactionalJUnit4SpringContextTests 的锅

加一个Listener就好了

GlobalExceptionTranslator (DB Column长度不够时 e.getMessage暴露的信息太多?)

uniquejava commented 4 years ago

mockito 连续相同方法调用返回不同值

    when(mockFoo.someMethod(anyString(), anyInt()))
            .thenReturn(obj1)
            .thenReturn(obj2)

见: How to tell a Mockito mock object to return something different the next time it is called?