gpt4 book ai didi

java - @WithUserDetails 注释不适用于 Spring 单元测试

转载 作者:行者123 更新时间:2023-11-30 06:43:18 33 4
gpt4 key购买 nike

我编写了一些测试,尝试使用 @WithUserDetails 注释运行。但是,当我运行它时,它告诉我找不到我的自定义 UserDetailsS​​ervice 实现。我怎样才能帮助测试类找到这个类?

这是错误的堆栈跟踪:

java.lang.IllegalStateException: Unable to create SecurityContext using @org.springframework.security.test.context.support.WithUserDetails(value=user1, userDetailsServiceBeanName=)

at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:99)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:81)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.beforeTestMethod(WithSecurityContextTestExecutionListener.java:60)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:269)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.core.userdetails.UserDetailsService' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:53)
at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:39)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:96)
... 31 more

这是我的测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@WithUserDetails("user1")
public class RecipeControllerTest {
@Mock
private RecipeService recipeService;

@Mock
private CategoryService categoryService;

@Mock
private IngredientService ingredientService;

@Mock
private InstructionService instructionService;

@Mock
private UserService userService;

@InjectMocks
private RecipeController recipeController;
private MockMvc mockMvc;

@Before
public void setup() throws Exception {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("classpath:/templates/");
viewResolver.setSuffix(".html");
mockMvc = MockMvcBuilders.standaloneSetup(recipeController)
.setViewResolvers(viewResolver).build();
MockitoAnnotations.initMocks(this);
}

@Test
public void getIndex() throws Exception {
List<Category> categories = new ArrayList<>();
User user = new User();

Recipe recipe = recipeBuilder();
Recipe recipe2 = recipeBuilder();

List<Recipe> recipes = new ArrayList<>();
recipes.add(recipe);
recipes.add(recipe2);

when(userService.findByUsername("user1")).thenReturn(user);
when(recipeService.findAll()).thenReturn(recipes);
when(categoryService.findAll()).thenReturn(categories);

mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(status().isOk())
.andExpect(view().name("login"));
}
}

这是我的自定义UserDetailsS​​ervice:

@Component
public class DetailsService implements UserDetailsService {
@Autowired
private UserService userService;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// Load user from database (throw exception if not found)
User user = userService.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}

// Return user object
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
AuthorityUtils.createAuthorityList(user.getRoles())
);
}
}

我应该提到,当我正常运行应用程序时,应用程序在查找此类时没有问题,只是测试类在查找此类时出现了问题。

提前致谢

最佳答案

鉴于您使用的是standaloneSetup,您没有加载ApplicationContext,因此不存在UserDetailsS​​ervice bean。如果您还没有看过 https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#test-mockmvc-securitycontextholder-rpp 。您可能还有兴趣切换到 MockMvcBuilders.webAppContextSetup

关于java - @WithUserDetails 注释不适用于 Spring 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44036206/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com