如何在内部类中对方法进行单元测试?

时间:2016-05-25 14:28:34

标签: java unit-testing singleton inner-classes

我用内部类制作了这个单例。我想为方法做一个单元测试:运行。现在我发现我应该用反射来做,但我不知道该怎么做。是否可以为该方法进行单元测试:运行?怎么样?

package monitor;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.util.Hashtable;
import java.util.List;
import java.util.Map; 
import java.util.Timer;
import java.util.TimerTask;
import com.gesmallworld.magik.commons.runtime.annotations.MagikProc;
import com.gesmallworld.magik.commons.runtime.objects.Char16Vector;

public class UsedMemory{
    private static UsedMemory theInstance;

    private static synchronized UsedMemory getInstance() {
        if( theInstance == null) {
            theInstance = new UsedMemory();
        }
        return theInstance;
    }

    private Map<String, MemorySampler> MEMORY_SAMPLERS = new Hashtable<String, MemorySampler>();

    private MemorySampler getMemorySampler(String memoryPool) {
        MemorySampler sampler = MEMORY_SAMPLERS.get(memoryPool);
        if (sampler == null) {
            sampler = new MemorySampler(memoryPool);
            MEMORY_SAMPLERS.put(memoryPool, sampler);
        }
       return sampler;
    }

    @MagikProc(name="rw_diag_setupusedmemory")
    public static void setupUsedMemory(Object proc, Char16Vector memoryPool, int timer){
        new Timer().schedule(getInstance().getMemorySampler(memoryPool.toString()), 0, timer);
    }

    @MagikProc(name="rw_diag_usedmemory")
    public static Char16Vector getUsedMemory(Object proc, Char16Vector memoryPool){
        return getInstance().getMemorySampler(memoryPool.toString()).getUsedMemoryString();
    }

    private class MemorySampler extends TimerTask {
        private MemoryPoolMXBean mxbean;
        private int byteToMb = 1048576;
        private long min = 0;
        private long avg = 0;
        private long count = 0;
        private long max = 0;

        public MemorySampler(String memoryPool) {
            mxbean = getMemoryPoolMXBean(memoryPool);
        }

        private Char16Vector getUsedMemoryString(){
            long minTemp = min;
            long avgTemp = avg;
            long maxTemp = max;
            min = 0;
            avg = 0;
            max = 0;
            count = 0;
            return new Char16Vector(minTemp/byteToMb+"__"+avgTemp/byteToMb+"__"+maxTemp/byteToMb);
        }

        @Override
        public void run() {
            long used = mxbean.getUsage().getUsed();
            if(min==0 || used < min){
                min = used;
            }
            if(max==0 || used > max){
                max = used;
            }
            if(count==0){
                avg = used;
            } else {
                avg=((avg*count)+used)/(count+1);
            }
            count +=1;
        }

        private MemoryPoolMXBean getMemoryPoolMXBean(String pool){
            List<MemoryPoolMXBean> mxbean = ManagementFactory.getMemoryPoolMXBeans();
            for(MemoryPoolMXBean mPool: mxbean){
                if(mPool.getName().equals(pool)){
                    return mPool;
                }
            }
            return null;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

在分析了主类和私有类之后,不需要将该私有类作为内部类,因为该类与包含它的类没有任何关系:内部类不引用任何类封闭类的变量或方法。

最好的解决方案是更改设计,将内部类提取到非内部公共类中,然后像往常一样进行测试。

如果您不想更改设计,或者由于某种原因无法更改设计(即代码可能以某种方式发展或增长,您需要保持原样),则必须使用更高级的机制喜欢反思。在这种情况下,您可以查看此问题:

How do I test a class that has private methods, fields or inner classes?

答案 1 :(得分:0)

我看起来MemorySampler不依赖于UsedMemory的实例,因此您可以将其设为静态。然后,如果您将访问级别更改为默认而非私有,您将能够从同一包中的单元测试中访问它。

static class MemorySampler extends TimerTask {
    ...
}

在您的单元测试课程中:

@Test
public void test() {
    UsedMemory.MemorySampler sampler = new UsedMemory.MemorySampler("memoryPool");
    ...
}