java:junit4
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| java:junit4 [2010/07/30 22:22] – creado rlunaro | java:junit4 [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 52: | Line 52: | ||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||
| + | import static org.junit.Assert.*; | ||
| Line 83: | Line 84: | ||
| System.out.println(" | System.out.println(" | ||
| element.setIdAndContent( -1, new StringBuilder(" | element.setIdAndContent( -1, new StringBuilder(" | ||
| + | assertTrue( true ); | ||
| } | } | ||
| Line 88: | Line 90: | ||
| </ | </ | ||
| + | |||
| + | Por cierto, agradeceré enormemente que alguien me explique porqué los ejemplos los pone la gente sin los " | ||
| + | |||
| + | |||
| + | ==== Explicando un poco el asunto ==== | ||
| + | |||
| + | <code java> | ||
| + | |||
| + | @ContextConfiguration(locations={" | ||
| + | |||
| + | </ | ||
| + | |||
| + | El meollo del maridaje JUnit - Spring. ContextConfiguration consigue proveer un contexto de aplicación para el test. Además de que nos va a hacer el Autowiring --o sea, crear automáticamente los //beans// que necesitemos--, | ||
| + | |||
| + | ==== Enchufando cosas ==== | ||
| + | |||
| + | <code java> | ||
| + | // this instance will be dependency injected by type | ||
| + | @Autowired | ||
| + | private htmlElement element; | ||
| + | |||
| + | </ | ||
| + | |||
| + | La anotacion '' | ||
| + | |||
| + | **¿Qué pasa si hay dos bean del mismo tipo???** pues que entonces se hace un lío. En ese caso '' | ||
| + | |||
| + | Las anotaciones '' | ||
| + | |||
| + | ===== Ejecutando el test ===== | ||
| + | |||
| + | Sobre la clase, boton derecho, Run As -> Junit Test y se lanzará el test, mostrando algo así: | ||
| + | |||
| + | < | ||
| + | 31-jul-2010 0:54:00 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions | ||
| + | INFO: Loading XML bean definitions from class path resource [test/ | ||
| + | 31-jul-2010 0:54:00 org.springframework.context.support.AbstractApplicationContext prepareRefresh | ||
| + | INFO: Refreshing org.springframework.context.support.GenericApplicationContext@351e1e67: | ||
| + | 31-jul-2010 0:54:00 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons | ||
| + | INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4ce66f56: | ||
| + | hello setup method | ||
| + | hello spring | ||
| + | goodbye setup | ||
| + | |||
| + | </ | ||
| + | |||
| + | Si os sale una ventana de configuración de ejecución teneis que seleccionar que eclipse ejecute el test como un test de JUnit4. Dato importante. | ||
| + | |||
| + | Otro problema bastante común es que se produce un error "File not found exception": | ||
| + | |||
| + | ===== Segundo error: Creando una Suite que agrupe a todos los test ===== | ||
| + | |||
| + | Tampoco se os ocurra crear la suite de tests usando el asistente de eclipse. Aquí hay que picarlo todo a mano. Os paso un ejemplo de los muchos que hay por internet: | ||
| + | |||
| + | <code java> | ||
| + | package test; | ||
| + | |||
| + | import org.junit.runner.RunWith; | ||
| + | import org.junit.runners.Suite; | ||
| + | |||
| + | @RunWith(Suite.class) | ||
| + | @Suite.SuiteClasses({HTMLTokenizerTest.class, | ||
| + | HTMLCleanerTest.class, | ||
| + | HTMLElementTest.class}) | ||
| + | public class AllTests | ||
| + | { | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | '' | ||
| + | |||
| + | |||
| + | ===== Documentasao ===== | ||
| + | |||
| + | |||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * JUnit4, gracias por nada!!! | ||
| + | * | ||
java/junit4.1280528579.txt.gz · Last modified: 2022/12/02 21:02 (external edit)
