这是我的Spring MVC应用程序中session.execute('UPDATE CourseAssignment SET jsonObject["displayable"] = fromJson("true") WHERE "rowId" = \'%s\'' % jsonObject["SYSTEM"] )
代码的一部分:
@Component
当我运行应用程序时,方法@Component
public class CacheServiceImpl implements CacheService, ApplicationListener<ContextRefreshedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger( CacheServiceImpl.class );
private Jedis JEDIS;
@Value("${redis.host.url}")
String redishost;
@Value("${redis.host.port}")
int redisport;
@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
LOGGER.info("Starting application...");
JEDIS = new Jedis(this.redishost,this.redisport);
}
// Other methods
}
在开始时执行(所以,它没问题)。但是,当我运行测试时,似乎方法onApplicationEvent
未执行,因为onApplicationEvent
等于JEDIS
。
这是测试代码:
null
更新:
@RunWith(SpringJUnit4ClassRunner.class)
public class CacheServiceImplTest {
private CacheServiceImpl cacheService;
private List<UD> listUD;
@Test
public void updateCache() throws Exception
{
cacheService.updateCache(listUD);
}
@Before
public void setUp() throws Exception {
cacheService = new CacheServiceImpl();
List<String> stringData = readStreamOfLinesUsingFiles();
listUD = new ArrayList<>();
for (String rs : stringData)
{
UD ud = new UD(rs);
listUD.add(ud);
}
Thread.sleep(1000);
}
private List<String> readStreamOfLinesUsingFiles() throws IOException
{
Stream<String> lines = Files.lines(Paths.get("MYFILE.txt"));
List<String> result = lines.collect(Collectors.toList());
lines.close();
return result;
}
}