Требуются заглушки для юнит тестов. Установил Mockito импортировал в библиотеки в проект. При запуске тестов Выходит простыня с такой ошибкой: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker перерыл все как её исправить без мавена и грайдела не найду. Есть у кого какие идеи?
package validator;
import org.junit.*;
import org.junit.runner.RunWith;
import validator.Validator.NotValidXMLException;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.FileNotFoundException;
import javax.xml.stream.XMLStreamException;
@RunWith(MockitoJUnitRunner.class)
public class ValidatorTest {
private Validator validator;
private String path = "src/validatorTest/";
@InjectMocks
Validator myValidator=new Validator();
@Mock
Validator.Buildfile buildfile;
public void initTest() {
myValidator.setCheckdefault(true);
myValidator.setCheckdepends(true);
myValidator.setChecknames(true);
when(buildfile.getLocation()).thenReturn(path);
}
@Before
public void setUp() {
validator = new Validator();
}
@After
public void tearDown() {
//validator = null;
}
@Test
public void testValidFileException() {
validator.execute();
}
@Test
public void testIsCheckdefault() {
assertEquals(validator.isCheckdefault(), false);
}
@Test
public void testIsCheckdepends() {
assertEquals(validator.isCheckdepends(), false);
}
@Test
public void testIsChecknames() {
assertEquals(validator.isChecknames(), false);
}
@Test
public void testSetIsCheckdefault() {
validator.setCheckdefault(true);
assertEquals(validator.isCheckdefault(), true);
}
@Test
public void testSetIsCheckdepends() {
validator.setCheckdepends(true);
assertEquals(validator.isCheckdepends(), true);
}
@Test
public void testSetIsChecknames() {
validator.setChecknames(true);
assertEquals(validator.isChecknames(), true);
}
@Test(expected = NotValidXMLException.class)
public void testNotValidXMLException() throws NotValidXMLException {
throw validator.new NotValidXMLException("", false);
}
@Test(expected = FileNotFoundException.class)
public void testFileNotFoundException() throws FileNotFoundException, XMLStreamException, NotValidXMLException {
validator.isValid(path + "bild.xml");
}
@Test(expected = XMLStreamException.class)
public void testXMLStreamException() throws FileNotFoundException, XMLStreamException, NotValidXMLException {
validator.isValid(path + "xmlStreamEx.xml");
}
@Test
public void testIsValidFileValidWithAllChek()
throws FileNotFoundException, XMLStreamException, NotValidXMLException {
validator.setCheckdefault(true);
validator.setCheckdepends(true);
validator.setChecknames(true);
validator.isValid(path + "build.xml");
}
@Test(expected = NotValidXMLException.class)
public void testCheckdefaultNotValid() throws FileNotFoundException, XMLStreamException, NotValidXMLException {
validator.setCheckdefault(true);
validator.isValid(path + "notval.xml");
}
@Test(expected = NotValidXMLException.class)
public void testCheckdependsNotValid() throws FileNotFoundException, XMLStreamException, NotValidXMLException {
validator.setCheckdepends(true);
validator.isValid(path + "notval.xml");
}
@Test(expected = NotValidXMLException.class)
public void testChecknamesNotValid() throws FileNotFoundException, XMLStreamException, NotValidXMLException {
validator.setChecknames(true);
validator.isValid(path + "notval.xml");
}
}
тестирую валидатор для XML файла.
package validator;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.io.FileNotFoundException;
import javax.xml.namespace.QName;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import validator.Validator.NotValidXMLException;
public class Validator extends Task {
private final List<Buildfile> buildfiles = new ArrayList<Buildfile>();
private boolean checkdepends;
private boolean checkdefault;
private boolean checknames;
public boolean isCheckdepends() {
return checkdepends;
}
public void setCheckdepends(boolean checkdepends) {
this.checkdepends = checkdepends;
}
public boolean isCheckdefault() {
return checkdefault;
}
public void setCheckdefault(boolean checkdefault) {
this.checkdefault = checkdefault;
}
public boolean isChecknames() {
return checknames;
}
public void setChecknames(boolean checknames) {
this.checknames = checknames;
}
public void execute() throws BuildException {
String filePath = buildfile.getLocation();
System.out.println("filepath = " + filePath);
try {
boolean validFile = isValid(filePath);
} catch (FileNotFoundException | XMLStreamException | NotValidXMLException e) {
e.printStackTrace();
}
}
boolean isValid(String filePath) throws FileNotFoundException, XMLStreamException, NotValidXMLException {
boolean isValid = true;
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(filePath));
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
String mainPoint = null;
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("project") && (checkdefault == true)) {
isValid = true;
Attribute defaultAttr = startElement.getAttributeByName(new QName("default"));
if (defaultAttr == null) {
isValid = false;
throw new NotValidXMLException("checkdefaults - project contains default attribute: "
+ Boolean.toString(isValid).toUpperCase(), isValid);
}
mainPoint = defaultAttr.getValue();
System.out.println("checkdefaults - project contains default attribute: "
+ Boolean.toString(isValid).toUpperCase());
}
if (startElement.getName().getLocalPart().equals("target") && (checkdepends == true)) {
isValid = true;
Attribute nameTarget = startElement.getAttributeByName(new QName("name"));
if (("main".equals(nameTarget.getValue()))
&& (startElement.getAttributeByName(new QName("depends")) == null)) {
isValid = false;
throw new NotValidXMLException("checkdepends - <target name='" + nameTarget.getValue()
+ "'> with depends are used instead of 'main' point: "
+ Boolean.toString(isValid).toUpperCase(), isValid);
}
System.out.println("checkdepends - <target name='" + nameTarget.getValue()
+ "'> with depends are used instead of 'main' point: "
+ Boolean.toString(isValid).toUpperCase());
}
if (!startElement.getName().getLocalPart().equals("include") && (checknames == true)) {
isValid = true;
Attribute nameAttr = startElement.getAttributeByName(new QName("name"));
if (nameAttr != null) {
String name = nameAttr.getValue();
isValid = isValid & name.matches("([a-z]|[A-Z]|-|_)*");
System.out.println("checknames - name: '" + name + "' contains only letters with '-': "
+ Boolean.toString(isValid).toUpperCase());
if (isValid == false) {
throw new NotValidXMLException(
"checknames - name: '" + name + "' contains only letters with '-': "
+ Boolean.toString(isValid).toUpperCase(),
isValid);
}
}
}
}
}
} catch (FileNotFoundException | XMLStreamException e) {
throw e;
/*e.printStackTrace();
System.out.println();
return isValid = false;*/
}
return isValid;
}
public class Buildfile {
private String location;
public void setLocation(String location) {
this.location = location;
System.out.println("setLocation = " + location);
}
public String getLocation() {
System.out.println("getLocation = " + location);
return location;
}
}
Buildfile buildfile = createBuildfile();
public Buildfile createBuildfile() {
buildfile = new Buildfile();
return buildfile;
}
public class NotValidXMLException extends Exception {
private static final long serialVersionUID = 1L;
private boolean valid;
public NotValidXMLException(String messege, boolean val) {
super(messege);
valid = val;
}
}
}