jjzjj

WebMvcTest

全部标签

java - 在 Spring Boot 中为 @WebMvcTest 禁用 Spring Security 配置类

最近我使用以下类将SpringSecurity添加到我的SpringBoot项目中:@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled=true)publicclassMySecurityConfig{}因此,默认情况下,我的所有URL现在都受到身份验证和自行生成的密码的保护。问题是我用于对Controller进行单元测试的@WebMvcTest类中的所有测试:@RunWith(SpringRunner.class)@WebMvcTest(SomeController.class)publicclassSomeCon

java - Controller 的 Spring Boot 测试 @WebMvcTest 似乎在上下文中加载其他 Controller

这是我的SpringController测试用例@RunWith(SpringRunner.class)@WebMvcTest(value=MyController.class)publicclassMyControllerTest{@MockBeanprivateMyServicemyService;}所以这是专门针对MyController中方法的单元测试。但是当我运行测试时,Spring似乎开始实例化OtherController及其所有依赖项。我已经尝试将上面的内容更新为@RunWith(SpringRunner.class)@WebMvcTest(value=MyContro

java - Spring Boot 1.4.2 @WebMvcTest 返回状态 404

我正在使用SpringBoot1.4.2和Jersey(jax-rs)创建一个RESTController。我遵循了有关如何测试RESTController的文档(TestingtheSpringMVCslice)。但是我的测试返回404,我似乎无法找出原因。这里的Controller被简化了,但问题依然存在。我的问题是如何在运行测试时获取200状态?HealthController.java@Controller@Path("/health")publicclassHealthController{@GET@Produces(MediaType.APPLICATION_JSON)pu

java - 如何将@WebMvcTest 用于使用 Autowiring 的 ConversionService 的 Controller ?

在SpringBoot应用程序中,我有两个POJO,Foo和Bar,以及一个BarToFooConverter,它看起来像:@ComponentpublicclassBarToFooConverterimplementsConverter{@OverridepublicFooconvert(Barbar){returnnewFoo(bar.getBar());}}我还有一个使用转换器的Controller:@RestController("test")publicclassTestController{@AutowiredprivateConversionServiceconversi

java - 我如何使用@WebMvcTest 并添加我自己的自定义过滤器?

SpringBoot1.4添加了@WebMvcTest,它连接了测试我的应用程序的Web切片所需的部分。这太棒了,但我也想确保我的自定义过滤器和安全代码已连接,这样我也可以验证它们是否正常工作。如何在使用@WebMvcTest时添加自定义过滤器? 最佳答案 @AutoConfigureWebMvc当前导入了以下自动配置类(参见spring-boot-test-autoconfigurejar中的spring.factories):#AutoConfigureMockMvcauto-configurationimportsorg.sp

mongodb - 在 Spring JUnit @WebMvcTest 中使用嵌入式 MongoDB

我目前在我的Spring应用程序中使用MongoDB。由于我添加了Mongo,我的端点测试由于以下错误而不再工作:Noqualifyingbeanoftype'xxx'available:expectedatleast1beanwhichqualifiesasautowirecandidate.Dependencyannotations:{}我在Controller中Autowiring的存储库如下:privatefinalRuleRepositoryruleRepository;@AutowiredpublicTestController(RuleRepositoryruleRepo

java - SpringBoot @WebMvcTest, Autowiring RestTemplateBuilder

我在测试SpringController时遇到了问题。我在我的测试类中使用注释@WebMvcTest。当我运行测试时,我得到这个错误:没有可用的“org.springframework.boot.web.client.RestTemplateBuilder”类型的合格bean我将RestTemplate用于项目中的其他类,因此我在主类中定义了一个bean:@BeanpublicRestTemplaterestTemplate(RestTemplateBuilderbuilder){returnbuilder.build();}为了让它工作,我必须这样定义我的restTemplatebe

java - SpringBoot @WebMvcTest, Autowiring RestTemplateBuilder

我在测试SpringController时遇到了问题。我在我的测试类中使用注释@WebMvcTest。当我运行测试时,我得到这个错误:没有可用的“org.springframework.boot.web.client.RestTemplateBuilder”类型的合格bean我将RestTemplate用于项目中的其他类,因此我在主类中定义了一个bean:@BeanpublicRestTemplaterestTemplate(RestTemplateBuilderbuilder){returnbuilder.build();}为了让它工作,我必须这样定义我的restTemplatebe

java - 在 Spring Boot 测试类上使用 @WebMvcTest 注释时出错

我已经按照描述的“测试SpringMVC切片”部分为SpringMVCController编写了一个测试类here.该类看起来像这样:@RunWith(SpringRunner.class)@WebMvcTest(controllers=OurController.class)publicclassOurControllerTest{@AutowiredprivateMockMvcmvc;@MockBeanprivateOurRepositoryourRepository;@TestpublicvoidtestGetStuff(){//coderemoved}}当我运行它时,出现以下错

java - 带有@EnableJpa*注解的Spring @WebMvcTest

我有以下主类。@EnableJpaAuditing@SpringBootApplication@EnableJpaRepositories(repositoryFactoryBeanClass=EnversRevisionRepositoryFactoryBean.class)publicclassSampleApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SampleApplication.class,args);}}和下面的单元测试类。@RunWith(SpringRunner.class)@
12