我的缓存配置如下;
public void read()
{
//port is already open
port.Write("AT" + System.Environment.NewLine);
//Thread.Sleep(1000);
port.Write("AT+CMGF=1" + System.Environment.NewLine);
//Thread.Sleep(1000);
port.Write("AT+CMGL=\"ALL\"\r" + System.Environment.NewLine);
//Thread.Sleep(3000);
//MessageBox.Show(port.ReadExisting());
Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
Match m = r.Match(port.ReadExisting());
while (m.Success)
{
string a = m.Groups[1].Value;
string b = m.Groups[2].Value;
string c = m.Groups[3].Value;
string d = m.Groups[4].Value;
string ee = m.Groups[5].Value;
string f = m.Groups[6].Value;
ListViewItem item = new ListViewItem(new string[] { a, b, c, d, ee, f });
lvwMessages.Items.Add(item);
m = m.NextMatch();
}
lvwMessages.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
lvwMessages.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
sd.Width = 0;
}
我要测试的服务:
@Configuration
public class CacheConfiguration {
@Bean
public CacheManager cacheManager(Ticker ticker) {
CaffeineCache bookCache = buildCache("books", ticker, 30);
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Collections.singletonList(bookCache));
return cacheManager;
}
private CaffeineCache buildCache(String name, Ticker ticker, int minutesToExpire) {
return new CaffeineCache(name, Caffeine.newBuilder()
.expireAfterWrite(minutesToExpire, TimeUnit.MINUTES)
.maximumSize(100)
.ticker(ticker)
.build());
}
@Bean
public Ticker ticker() {
return Ticker.systemTicker();
}
}
存储库中的必需方法使用@Service
public class TestServiceImpl implements TestService {
private final BookRepository bookRepository; // interface
@Autowired
public TestServiceImpl(final BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Override
public Book getByIsbn(String isbn) {
return bookRepository.getByIsbn(isbn);
}
}
进行注释。
@Cacheable("books")
我需要编写一个显示缓存工作的测试。所以我在test中创建了另一个@Override
@Cacheable("books")
public Book getByIsbn(String isbn) {
LOGGER.info("Fetching Book...");
simulateSlowService(); // Wait for 5 secs
return new Book(isbn, "Some book");
}
bean来覆盖ticker
中存在的bean。代码;
CacheConfiguration
但它在标有@RunWith(SpringRunner.class)
@SpringBootTest
public class TestServiceTests {
private static final String BOOK_ISBN = "isbn-8442";
@SpyBean
private BookRepository bookRepository;
@Autowired
private TestService testService;
@Configuration
@Import(SpringBootCacheApplication.class)
public static class TestConfiguration {
//testCompile('com.google.guava:guava-testlib:23.6-jre')
static FakeTicker fakeTicker = new FakeTicker();
@Bean
public Ticker ticker() {
return fakeTicker::read;
}
}
@Before
public void setUp() {
Book book = fakeBook();
doReturn(book)
.when(bookRepository)
.getByIsbn(BOOK_ISBN);
}
private Book fakeBook() {
return new Book(BOOK_ISBN, "Mock Book");
}
@Test
public void shouldUseCache() {
// Start At 0 Minutes
testService.getByIsbn(BOOK_ISBN);
verify(bookRepository, times(1)).getByIsbn(BOOK_ISBN);
// After 5 minutes from start, it should use cached object
TestConfiguration.fakeTicker.advance(5, TimeUnit.MINUTES);
testService.getByIsbn(BOOK_ISBN);
verify(bookRepository, times(1)).getByIsbn(BOOK_ISBN); // FAILS HERE
// After 35 Minutes from start, it should call the method again
TestConfiguration.fakeTicker.advance(30, TimeUnit.MINUTES);
testService.getByIsbn(BOOK_ISBN);
verify(bookRepository, times(2)).getByIsbn(BOOK_ISBN);
}
}
的行上显示消息;
//FAILS HERE
为什么会失败?它不应该使用缓存吗?或者我的测试错了?
非常感谢任何帮助或指示! :)
答案 0 :(得分:0)
Could not load driverClass org.postgresql.Driver
当然,这里失败了。因为在您已经调用过此方法一次之前大约有4行。在此检查中,您应该放置FROM vertx/vertx3
。在下一次检查时,调用次数应为verify(bookRepository, times(1)).getByIsbn(BOOK_ISBN); // FAILS HERE