简体   繁体   English

Junit 测试用例

[英]Junit test cases

I am writing test cases for this code.我正在为此代码编写测试用例。

But I am not able to write junit test cases for this.但是我无法为此编写junit测试用例。 I am having problem to write junit test cases for this.我在为此编写 junit 测试用例时遇到问题。

Please help to write mockito or junit test case for this.请帮助为此编写 mockito 或 junit 测试用例。

public class BasicCacheManager implements CacheManager {
private ConcurrentHashMap<String, BasicCache> cacheMap;
private String defaultCache;

public BasicCacheManager() {
    this.cacheMap = new ConcurrentHashMap<String, BasicCache>();
}

public BasicCacheManager(List<String> cacheNames) {
    this.cacheMap = new ConcurrentHashMap<String, BasicCache>();
    addCache(cacheNames);
}

@Override 
public BasicCache getCache(){
    return getCache(defaultCache);
}

@Override 
public BasicCache getCache(String name) {
    return cacheMap.get(name);
};

@Override 
public void addCache(BasicCache cache) {
    cacheMap.put(cache.getName(), cache);
};

@Override 
public void removeCache(String name) {
    cacheMap.remove(name);
};

@Override 
public void removeAll() {
    cacheMap = new ConcurrentHashMap<String, BasicCache>();
};

@Override 
public boolean cacheExists(String name) {
    return cacheMap.containsKey(name);
};

@Override 
public void removeElement(String cacheName, String element){
    BasicCache cache = getCache(cacheName);
    if(cache != null){
        cache.remove(element);
    }
}

// added final to avoid any child classes from overriding this method
private final void addCache(List<String> cacheNames) {
    for (String cacheName : cacheNames) {
        BasicCache cache = new BasicCache(cacheName);
        addCache(cache);
    }
}

} }

You don't need mocking here.你不需要在这里嘲笑。

Your CUT (class under test) has very clear semantics;您的 CUT(被测类)具有非常清晰的语义; and interfaces that basically guide you into writing testcases.以及基本上指导您编写测试用例的接口。 What you have to do: create objects of your cache class;你需要做的是:创建你的缓存类的对象; and then use its methods to modify it;然后用它的方法修改它; and asserts to verify.并断言验证。

Example:例子:

@Test
public void testNewCacheIsEmpty() {
  BasicCache underTest = new BasicCache();
  assertThat(underTest.isEmpty(), is(true));

} }

(in case you dont have an isEmpty() yet, you could add that one as package-protected method; just to allow for such tests). (如果您还没有 isEmpty() ,您可以将其添加为包保护方法;只是为了允许此类测试)。

Then you write other tests;然后你编写其他测试; for example that make sure that adding a certain cache-key ... actually does what you expect that to do.例如,确保添加某个缓存键......实际上是你期望的那样。

So, the primary thing to understand here: you should design your whole cache so that you can test it ... without knowing what exactly it is doing.所以,这里要理解的主要事情是:你应该设计你的整个缓存,以便你可以测试它......而不知道它到底在做什么。

Of course, you could create a mocked ConcurrentHashMap object and pass that into your class;当然,您可以创建一个模拟的ConcurrentHashMap 对象并将其传递给您的类; to verify that exactly those calls that you expect for some operation to take place really happen.以验证您期望进行某些操作的那些调用确实发生了。 But that means: testing implementation details.但这意味着:测试实现细节。 And you want to avoid that if possible.如果可能的话,你想避免这种情况。

And just for the record: your field defaultCache doesn't make any sense in the code you are showing.只是为了记录:您的字段defaultCache在您显示的代码中没有任何意义。 It is null initially, there is no setter for it;它最初为空,没有设置器; so it has absolutely no purpose.所以它绝对没有目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM