我的测试有问题。我正在使用java spring并尝试运行junit test来检查我的服务器是否还活着。
测试我正在尝试:
# cryptsetup 1.6.6 processing "cryptsetup --debug luksOpen /dev/sdb1 mongo_data"
# Running command open.
# Locking memory.
# Installing SIGINT/SIGTERM handler.
# Unblocking interruption on signal.
# Allocating crypt device /dev/sdb1 context.
# Trying to open and read device /dev/sdb1.
# Initialising device-mapper backend library.
# Trying to load LUKS1 crypt type from device /dev/sdb1.
# Crypto backend (gcrypt 1.6.5) initialized.
# Detected kernel Linux 4.4.0-81-generic x86_64.
# Reading LUKS header of size 1024 from device /dev/sdb1
# Key length 32, device size 20969472 sectors, header size 2050 sectors.
# Timeout set to 0 miliseconds.
# Password retry count set to 3.
# Password verification disabled.
# Iteration time set to 1000 miliseconds.
# Activating volume mongo_data [keyslot -1] using [none] passphrase.
# dm version OF [16384] (*1)
# dm versions OF [16384] (*1)
# Detected dm-crypt version 1.14.1, dm-ioctl version 4.34.0.
# Device-mapper backend running with UDEV support enabled.
# dm status mongo_data OF [16384] (*1)
# STDIN descriptor passphrase entry requested.
# Trying to open key slot 0 [ACTIVE_LAST].
# Reading key slot 0 area.
# Using userspace crypto wrapper to access keyslot area.
# Trying to open key slot 1 [INACTIVE].mke2fs 1.42.13 (17-May-2015)
# Trying to open key slot 2 [INACTIVE].The file /dev/mapper/mongo_data does not exist and no size was specified.
# Trying to open key slot 3 [INACTIVE].
# Trying to open key slot 4 [INACTIVE].
# Trying to open key slot 5 [INACTIVE].
# Trying to open key slot 6 [INACTIVE].
# Trying to open key slot 7 [INACTIVE].
# STDIN descriptor passphrase entry requested.
# Nothing read on input.
# Releasing crypt device /dev/sdb1 context.
# Releasing device-mapper backend.
# Unlocking memory.
这是我的newtest类正在扩展的 AbstractTestController类:
@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration(classes = arrayOf(ServiceContext::class,DatabaseContext::class))
@Transactional
open class newtest : AbstractTestController(){
@Test
fun echoTest() {
mockMvc.perform(get("/echo").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk)
}
}
我的配置:
@WebAppConfiguration
abstract class AbstractTestController : DatabaseInitializedTest() {
@Autowired
lateinit var webAppContext: WebApplicationContext
lateinit var mockMvc: MockMvc
@Autowired
lateinit var authHelper: OAuthHelper
@Autowired
private lateinit var userService: UserService
fun adminToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.adminAuthorities.toTypedArray())
fun clientItToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.clientItAuthorities.toTypedArray())
fun clientManagerToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.clientManagerAuthorities.toTypedArray())
fun endUserToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.endUserAuthorities.toTypedArray())
@Autowired
lateinit var mapper: ObjectMapper
fun <T> ResultActions.and200ReturnClass(clazz: TypeReference<T>): ResultActions {
if (this.andReturn().response.status == HttpServletResponse.SC_OK
&& this.andReturn().response.contentAsString.isNotEmpty()) {
mapper.readValue<T>(this.andReturn().response.contentAsString, clazz)!!
}
return this
}
@Before
fun initialize() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webAppContext)
.apply<DefaultMockMvcBuilder>(SecurityMockMvcConfigurers.springSecurity())
.alwaysDo<DefaultMockMvcBuilder>(MockMvcResultHandlers.print())
.build()
}
}
这是我完整的错误:
@Configuration
@Import(value = *arrayOf(
OAuth2ServerConfiguration::class,
OAuth2ServerConfiguration.AuthorizationServerConfiguration::class,
OAuth2ServerConfiguration.ResourceServerConfiguration::class,
SecurityConfig::class,
OAuthHelper::class,
WebConfig::class,
DatabaseInitializator::class,
LowerCaser::class))
@ComponentScan("com.hyg","com.hyg.service", "com.hyg.web", "com.hyg.utils")
@EnableAutoConfiguration
@EnableAspectJAutoProxy(proxyTargetClass = true)
open class ServiceContext
@Configuration
@EnableJpaRepositories("com.hyg")
@EnableTransactionManagement
@EnableAutoConfiguration
@PropertySource("classpath:application.properties")
open class DatabaseContext {
val DATA_PACKAGE = "com.hyg.data"
val INTERCEPTOR_KEY = "hibernate.ejb.interceptor"
@Value("\${spring.datasource.username:${Const.NONE}}")
lateinit var username: String
@Value("\${spring.datasource.url}")
lateinit var url: String
@Value("\${spring.datasource.driverClassName}")
lateinit var driverClass: String
@Value("\${spring.datasource.password}")
lateinit var password: String
@Bean
@Primary
open fun objectMapper(): ObjectMapper {
val mapper = ObjectMapper()
mapper.registerModule(KotlinModule())
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
mapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true)
mapper.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false)
mapper.registerModule(JavaTimeModule())
mapper.registerModule(localDateTimeSerializer())
return mapper
}
@Bean
open fun localDateTimeSerializer(): SimpleModule {
val localDateTimeSerializer = SimpleModule()
val serializer = LocalDateTimeSerializer(
DateTimeFormatter.ofPattern(Const.DATE_TIME_PATTERN))
val deserializer = LocalDateTimeDeserializer(
DateTimeFormatter.ofPattern(Const.DATE_TIME_PATTERN))
localDateTimeSerializer.addSerializer(LocalDateTime::class.java, serializer)
localDateTimeSerializer.addDeserializer(LocalDateTime::class.java, deserializer)
return localDateTimeSerializer
}
@Bean
open fun dataSource(): DataSource {
val ds = DataSourceBuilder.create().driverClassName(driverClass).username(username).password(password).url(url).build()
val proxy = ProxyDataSource()
proxy.setDataSource(ds)
proxy.setListener(DataSourceQueryCountListener())
return proxy
}
@Bean
open fun entityManagerFactory(
factory: EntityManagerFactoryBuilder, dataSource: DataSource,
properties: JpaProperties): LocalContainerEntityManagerFactoryBean {
val jpaProperties = HashMap<String, Any>()
jpaProperties.putAll(properties.getHibernateProperties(dataSource))
jpaProperties.put(INTERCEPTOR_KEY, hibernateInterceptor())
return factory.dataSource(dataSource).packages(DATA_PACKAGE)
.properties(jpaProperties as MutableMap<String, *>)
.build()
}
@Bean
open fun hibernateInterceptor(): HibernateStatisticsInterceptor {
return HibernateStatisticsInterceptor()
}
@Bean
open fun requestStatisticsInterceptor(): RequestStatisticsInterceptor {
return RequestStatisticsInterceptor()
}
}
希望它有助于解决我的问题。 到目前为止,我尝试将@Primary放在构成ObjectMapper的Bean上,或者添加那些注释@Order @ConditionalOnMissingBean但它没有工作...... 谢谢
答案 0 :(得分:1)
Spring Context中有几个类型为ObjectMapper
的bean。尝试为@Qualifier("objectMapper")
添加注释mapper
(在AbstractTestController
中)或将其重命名为objectMapper
:
@WebAppConfiguration
abstract class AbstractTestController : DatabaseInitializedTest() {
...
@Qualifier("objectMapper")
@Autowired
lateinit var mapper: ObjectMapper
...
}
或
@WebAppConfiguration
abstract class AbstractTestController : DatabaseInitializedTest() {
...
@Autowired
lateinit var objectMapper: ObjectMapper
...
}