Tagged: testng
Dagger with TestNG
It’s just simple.
public class MyTest { @BeforeClass public void inject() { ObjectGraph.create(new MyModule()).inject(this); } @Inject String injected; }
New Kids On The Block
I wrote a simple library works with an annotation.
@Dagger(modules = MyModule.class) public class MyTest { // @BeforeClass // public void inject() { // ObjectGraph.create(new MyModule()).inject(this); // } @Inject String injected; }
finding the current test method
테스트 메서드 하나가 끝나지 않고 계속 도는 듯 하다. 어떤 클래스의 어떤 메서드인지 다음과 같이 리스너를 하나 만들어서 사용했다.
public class InvokedMethodListener implements IInvokedMethodListener { @Override public void beforeInvocation(final IInvokedMethod method, final ITestResult result) { logger.debug("beforeInvocation({}, {})", method, result); } @Override public void afterInvocation(final IInvokedMethod method, final ITestResult result) { // not interested } }
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <properties> <property> <name>listeners</name> <value>package.to.the.InvokedMethodListener</value> </property> </properties> </configuration> </plugin>
HH.mm.ss.SSS [main] DEBUG package.to.the.InvokedMethodListener - beforeInvocation(...)
Unit-Testing JAX-RS with Jersey Test Framework and Weld
요 근래 JAX-RS에 기반한 라이브러리를 하나 만들던 중 단위 테스트에 문제가 생겨서 SO에 이것 저것 물어봤다.
References
- How can I inject a custom factory using hk2?
- How can I bind a factory to a annotation-qualified injection point?
- @PostConstruct of abstract ancestors are not invoked
- How can I enable CDI with Jersey Test Framework?
JAX-RS
만들고 있는 라이브러리는 JAX-RS와 DI만 사용한다.
<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <scope>provided</scope> </dependency>
Jersey Test Framework
단위 시험을 하기위해 다음과 같이 Jersey Test Framework을 추가하였다.
<dependency> <groupId>org.glassfish.jersey.test-framework.providers</groupId> <artifactId>jersey-test-framework-provider-grizzly2</artifactId> <scope>test</scope> </dependency>
Weld
Jersey가 사용하고 있는 HK2(JSR-330)은 주입(injection)은 잘 수행하는 듯 하나 JSR-250에 해당하는 @PostConstruct
등은 수행하지 못한다. 다음과 같이 dependency를 추가했다.
<dependency> <groupId>org.glassfish.jersey.ext.cdi</groupId> <artifactId>jersey-weld2-se</artifactId> <scope>test</scope> </dependency>
그리고 다음과 같이 Weld를 적용시켰다.
@Override protected Application configure() { final Weld weld = new Weld(); weld.initialize(); Runtime.getRuntime().addShutdownHook(new Thread(() -> weld.shutdown())); // ... }
launching 3rd party servlets using jetty for unit testing
References
Apache Maven
<dependency> <!-- I'm not sure this is the minimum dependency --> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet-tester</artifactId> <scope>test</scope> </dependency>
TestNG
private static final PORT; static { PORT = ThreadLocalRandom.current().nextInt(1024, 65536); } private static Server SERVER; @BeforeClass public static void startServer() throws Exception { SERVER = new Server(PORT); final ServletHandler handler = new ServletHandler(); SERVER.addServeltHandler(handler); handler.addServletWithMapping(SomeServlet.class, "/some"); SERVER.start(); } @AfterClass public static void stopServer() throws Exception { SERVER.stop(); SERVER = null; } @Test public void test() throws MalformedURLException { final URL url = new URL("http://localhost:" + PORT + "/some"); }