方法不能静态
方法如下:
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.w3c.dom.Document;
public class XmlUtils {
private static String filename="exam.xml";
@Test
public static Document getDocument()throws Exception
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
return builder.parse(filename);
}
@Test
public static void write2Xml(Document document)throws Exception
{
TransformerFactory factory=TransformerFactory.newInstance();
Transformer tf=factory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream(filename)));
}
}
这是我完全按照代码打的。我用junit测试。出现了红条,语句如下
求帮忙------解决方案--------------------没记错的话 junit 的@Test 标记的方法 不能 static