Spring boot 1.5.3项目,测试用户注册表在内存数据库中的H2上
这是错误Stacktrace
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'myAppUserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'SMRTUserService': Unsatisfied dependency expressed through field 'userInfoDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SMRTUserDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
有人可以帮我理解这个问题吗?我无法解决这个错误。
测试控制器
public class CustomerControllerTest extends AbstractControllerTest {
@Test
@WithMockUser(roles = "ADMIN")
public void testShow() throws Exception {
mockMvc.perform(get("/customer/list")
.contentType(APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}
}
AbstractControllerTest
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
public abstract class AbstractControllerTest extends AbstractTest {
@Autowired protected MockMvc mockMvc;
@Autowired private FilterChainProxy filterChainProxy;
@Autowired private WebApplicationContext webApplicationContext;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.mockMvc = webAppContextSetup(webApplicationContext)
.dispatchOptions(true)
.addFilters(filterChainProxy).build();
}
}
SecurityConfiguration
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired private SMRTUserService myAppUserDetailsService;
@Autowired private BCryptPasswordEncoder bCryptPasswordEncoder;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myAppUserDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}
}
SMRTUSerService
@Service
@Slf4j
public class SMRTUserService implements UserDetailsService {
@Autowired private ISMRTUserDAO userInfoDAO;
@Autowired private SMRTUserRepository smrtuserRepository;
...
}
由于
答案 0 :(得分:0)
好吧,你的例外已经很好地解释了问题所在:
Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
我认为您为测试导入了一些测试数据? SQL语句中必定存在错误。
答案 1 :(得分:0)
如果您使用 keycloak 进行身份验证。你可能会收到这个错误。 jhipster 默认为您提供 8090 作为身份验证服务器,因此您必须更改它
解决方案:1 - 启动您的 keycloak 服务器
2 - go to your jhipster project main->ressources->config->application.yml
更改 issuer ui 通过运行 keycloak 服务器的端口:例如:issuer-uri: http://localhost:8080/auth/realms/demo
希望对你有帮助