SlideShare a Scribd company logo
public class QuestionTest {
private Question question;
@Before
public void setUp() throws Exception {
question = new Question("How many wheels are there on a car?",
"car.png");
}
@Test
public void shouldAddAnswersToQuestion() {
addAnswerToQuestion(new Answer("It is 3", false));
addAnswerToQuestion(new Answer("It is 4", true));
addAnswerToQuestion(new Answer("It is 5", false));
addAnswerToQuestion(new Answer("It is 6", false));
assertEquals(4, question.getAnswers().size());
}
@Test(expected = MultipleAnswersAreCorrectException.class)
public void shouldFailIfTwoAnswersAreCorrect() {
addAnswerToQuestion(new Answer("It is 3", false));
addAnswerToQuestion(new Answer("It is 4", true));
addAnswerToQuestion(new Answer("It is 5", true));
addAnswerToQuestion(new Answer("It is 6", false));
}
private void addAnswerToQuestion(Answer answer) {
question.addAnswer(answer);
}
}
public void addAnswer(Answer answer) throws MultipleAnswersAreCorrectException {
boolean correctAnswerAdded = false;
for (Answer element : answers) {
if (element.getCorrect()) {
correctAnswerAdded = true;
}
}
if (correctAnswerAdded) {
throw new MultipleAnswersAreCorrectException();
} else {
answers.add(answer);
}
}

More Related Content

Viewers also liked

Organigrama EJE.
Organigrama EJE.Organigrama EJE.
Organigrama EJE.
Luz San Martin Lago
 
Importancia de los valores para una convivencia social
Importancia de los valores para una convivencia socialImportancia de los valores para una convivencia social
Importancia de los valores para una convivencia social
judithmoya
 
Interfaz piratas
Interfaz piratasInterfaz piratas
Interfaz piratas
Horacio Ibarra Almaguer
 
Luis eduardo zarate actividad1- mapa conceptual
Luis eduardo zarate actividad1- mapa conceptualLuis eduardo zarate actividad1- mapa conceptual
Luis eduardo zarate actividad1- mapa conceptualLUIS EDUARDO ZARATE
 
Contra el chikunguya
Contra el chikunguyaContra el chikunguya
Contra el chikunguya
levisalberto
 
El podcast en el aula
El podcast en el aulaEl podcast en el aula
El podcast en el aula
Juan Díaz
 
Mantenimiento
MantenimientoMantenimiento
Mantenimiento
Cesar Torres Anicama
 
Protocolo y etiqueta
Protocolo y etiquetaProtocolo y etiqueta
Protocolo y etiqueta
jhonfaberzuleta
 
Las wikis
Las wikisLas wikis
Las wikis
MaNe Hernández
 
Recicla!
Recicla!Recicla!
Recicla!aleqs
 
Parcial
ParcialParcial
Parcial
guspavo
 
Before and after
Before and afterBefore and after
Before and after
hannah_hughes
 
Objetos tecnologicos niño lilia
Objetos tecnologicos niño liliaObjetos tecnologicos niño lilia
Objetos tecnologicos niño lilia
Lili_Leguizamo
 
publicidad
publicidadpublicidad
publicidad
guest2f55770
 
Presentación1 buscadores
Presentación1 buscadoresPresentación1 buscadores
Presentación1 buscadores
warioti
 

Viewers also liked (19)

Organigrama EJE.
Organigrama EJE.Organigrama EJE.
Organigrama EJE.
 
Importancia de los valores para una convivencia social
Importancia de los valores para una convivencia socialImportancia de los valores para una convivencia social
Importancia de los valores para una convivencia social
 
Interfaz piratas
Interfaz piratasInterfaz piratas
Interfaz piratas
 
J.JUAN, SA (Cicle de presentacions empresarials PEG)
J.JUAN, SA (Cicle de presentacions empresarials PEG)J.JUAN, SA (Cicle de presentacions empresarials PEG)
J.JUAN, SA (Cicle de presentacions empresarials PEG)
 
Luis eduardo zarate actividad1- mapa conceptual
Luis eduardo zarate actividad1- mapa conceptualLuis eduardo zarate actividad1- mapa conceptual
Luis eduardo zarate actividad1- mapa conceptual
 
Contra el chikunguya
Contra el chikunguyaContra el chikunguya
Contra el chikunguya
 
El podcast en el aula
El podcast en el aulaEl podcast en el aula
El podcast en el aula
 
Mantenimiento
MantenimientoMantenimiento
Mantenimiento
 
Protocolo y etiqueta
Protocolo y etiquetaProtocolo y etiqueta
Protocolo y etiqueta
 
Las wikis
Las wikisLas wikis
Las wikis
 
Recicla!
Recicla!Recicla!
Recicla!
 
Warnet
WarnetWarnet
Warnet
 
Parcial
ParcialParcial
Parcial
 
Before and after
Before and afterBefore and after
Before and after
 
Bertsoblogabe
BertsoblogabeBertsoblogabe
Bertsoblogabe
 
Objetos tecnologicos niño lilia
Objetos tecnologicos niño liliaObjetos tecnologicos niño lilia
Objetos tecnologicos niño lilia
 
publicidad
publicidadpublicidad
publicidad
 
L’art romànic
L’art romànicL’art romànic
L’art romànic
 
Presentación1 buscadores
Presentación1 buscadoresPresentación1 buscadores
Presentación1 buscadores
 

Similar to Junit for method_throwing_exception

Methods Of Thread Class
Methods Of Thread ClassMethods Of Thread Class
Methods Of Thread Class
kqibtiya5
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
NicholasflqStewartl
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Vinod Kumar V H
 
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdfRepeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
arracollection
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdf
akshpatil4
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
jeresig
 

Similar to Junit for method_throwing_exception (6)

Methods Of Thread Class
Methods Of Thread ClassMethods Of Thread Class
Methods Of Thread Class
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdfRepeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdf
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 

Junit for method_throwing_exception

  • 1. public class QuestionTest { private Question question; @Before public void setUp() throws Exception { question = new Question("How many wheels are there on a car?", "car.png"); } @Test public void shouldAddAnswersToQuestion() { addAnswerToQuestion(new Answer("It is 3", false)); addAnswerToQuestion(new Answer("It is 4", true)); addAnswerToQuestion(new Answer("It is 5", false)); addAnswerToQuestion(new Answer("It is 6", false)); assertEquals(4, question.getAnswers().size()); } @Test(expected = MultipleAnswersAreCorrectException.class) public void shouldFailIfTwoAnswersAreCorrect() { addAnswerToQuestion(new Answer("It is 3", false)); addAnswerToQuestion(new Answer("It is 4", true)); addAnswerToQuestion(new Answer("It is 5", true)); addAnswerToQuestion(new Answer("It is 6", false)); } private void addAnswerToQuestion(Answer answer) { question.addAnswer(answer); } } public void addAnswer(Answer answer) throws MultipleAnswersAreCorrectException { boolean correctAnswerAdded = false; for (Answer element : answers) { if (element.getCorrect()) { correctAnswerAdded = true; } } if (correctAnswerAdded) { throw new MultipleAnswersAreCorrectException(); } else { answers.add(answer); } }