import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
@PrepareForTest({MapMetadataDAO.class, Transform.class})
public class TransformTest {
@Mock
private MapMetadataDAO mapMetadataDAO;
@Mock
private Map<String,String> srcTargetMap;
@InjectMocks
private Transform Transform = new Transform();
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testConstructor() throws Exception {
List<MapMetadata> mapRecords = new ArrayList<MapMetadata>();
mapRecords.add(new MapMetadata());
PowerMockito.whenNew(MapMetadataDAO.class).withNoArguments().thenReturn(mapMetadataDAO);
PowerMockito.when(mapMetadataDAO.findMapMetadataByTransformationID(Mockito.anyString())).thenReturn(mapRecords);
Transform.getTransform("transformationId");
}
}
class Transform{
private MapMetadataDAO mapMetadataDAO;
private Map<String,String> srcTargetMap;
private List<MapMetadata> mapRecords;
public Transform() {}
public void getTransform(String transformationId){
try {
mapMetadataDAO=new MapMetadataDAO();
mapRecords = mapMetadataDAO.findMapMetadataByTransformationID(transformationId);
System.out.println(mapRecords.size());
if(mapRecords.isEmpty()){
throw new GenericFlowException("Map meta data is not defined for the transformationId "+transformationId);
}
map();
} catch (DAOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GenericFlowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void map() {}
}
MockUp Runtime.availableProcessors可以,但是当MockUp System.currentTimeMillis出问题时,错误是“ java.lang.UnsatisfiedLinkError”。
@Test
public void mockNativeMethod() {
new MockUp<Runtime>() {
@Mock
@SuppressWarnings("unused")
int availableProcessors() {
return 999;
}
};
assertEquals(999, Runtime.getRuntime().availableProcessors());
}
答案 0 :(得分:0)
不幸的是,这是一个JVM问题。 (在1.43版之前,JMockit使用了丑陋的方法来避免失败。)