I am using Eclipse Juno. I installed TestNG plugins properly. But when I want to run test I can't find a TestNG option in Run As. What's the matter?
My Class is
package com.oasisdigital.rental.client;
import static javax.ws.rs.core.Response.Status.CREATED;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import javax.ws.rs.core.Response;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.oasisdigital.rental.test.AbstractIT;
#Test
public class ClientResourceIT extends AbstractIT {
private ClientApi clientApi;
#Override
#BeforeMethod
public void setUp() {
super.setUp();
this.clientApi = new ClientApi(api);
}
#Test
public void shouldReturnEmptyListWhenNoProviders() {
assertThat(clientApi.getClients(), is(empty()));
}
#Test
public void shouldReturnClientAfterCreation() {
Response resp = clientApi.postClient("Jimmy");
assertThat(resp.getStatus(), is(CREATED.getStatusCode()));
ClientDto client = resp.readEntity(ClientDto.class);
assertThat(client.getId(), is(notNullValue()));
assertThat(client.getName(), is("Jimmy"));
assertThat(clientApi.getClients(), contains(client));
}
}
Related
package com.example.stubrunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.contract.stubrunner.server.EnableStubRunnerServer;
import org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
#SpringBootApplication
#EnableStubRunnerServer
public class StubRunnerApplication {
public static void main(String[] args) {
SpringApplication.run(StubRunnerApplication.class, args);
}
#Bean
public WireMockConfigurationCustomizer optionsCustomizer() {
WireMockConfigurationCustomizer customizer = new WireMockConfigurationCustomizer() {
#Override
public void customize(com.github.tomakehurst.wiremock.core.WireMockConfiguration config) {
config.jettyHeaderBufferSize(16384);
}
};
return customizer;
}
}
Above customizer bean does not seem to have any effect. This feature has not much documentation. With security token headers Wiremock's (jettty) default value is just too little.
I used start.spring.io with (current) defaults: spring boot 2.5.5. and spring cloud Hoxton.SR3.
java -jar wiremock-standalone-2.26.3.jar --jetty-header-buffer-size 16384
works just fine.
EDIT :
package com.example.wiremockrunnerlatest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.contract.stubrunner.server.EnableStubRunnerServer;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
#SpringBootApplication
#EnableStubRunnerServer
#AutoConfigureStubRunner(httpServerStubConfigurer = HeaderSizeConfigurer.class)
public class WiremockRunnerLatestApplication {
public static void main(String[] args) {
SpringApplication.run(WiremockRunnerLatestApplication.class, args);
}
}
... and then :
public class HeaderSizeConfigurer extends WireMockHttpServerStubConfigurer {
#Override
public WireMockConfiguration configure(WireMockConfiguration httpStubConfiguration, HttpServerStubConfiguration httpServerStubConfiguration) {
return httpStubConfiguration.jettyHeaderBufferSize(16384);
}
}
Have you tried using #AutoConfigureStubRunner annotation?
Just add below annotation in your tests:
#AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.CLASSPATH,
ids = "com.org:servicename:+:stubs")
Here stubsmode is classpath which means that stubs would be available in classpath.
to do that add:
testCompile("com.org:servicename:+:stubs") { transitive = false }
in your build if your using gradle or add equivalient from maven.
This will download the application automatically from remote and configures the wiremock server for stubs to be available.
complete configuration to run a spring boot test is like:
#RunWith(SpringRunner.class)
#SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.MOCK,
classes = HiltiIntegrationApplication.class)
#AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.CLASSPATH,
ids = "com.ict:organization-management:+:stubs")
#DirtiesContext
Hope this helps!
I got the HTML in console using getpagesource in selenium java .
Now i need only the tag 'Forms' in the console result.
How do i do that?
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://''/tandem/login/?");
String pagesource = driver.getPageSource();
System.out.println(pagesource);
}
You can use following code snippet for this :
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//Sample Java class to fetch list of all tag with tagname forms
public class Test {
public static void main(String[] args) {
String geckoDriverPath=Class1.class.getResource("/firefox/geckodriver.exe").getPath();
System.setProperty("webdriver.gecko.driver",geckoDriverPath);
WebDriver driver = new FirefoxDriver();
driver.get("http://www.URL.com");
List<WebElement>list =driver.findElements(By.tagName("form"));
for(WebElement ele:list) {
System.out.println(ele.getText());
}
}
}
Once you get the lists you can fetch individual form element and perform action like getting text.Hope, this helps
I am new in Junit. While testing some basic annotations in Junit4, I am getting this weird response. My #BeforeClass is executing after all the test cases. Can anyone please explain what could be the possible reason?
My test class:
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class NewTest {
#BeforeClass
public static void beforeClass() {
System.out.println("#BeforeClass");
}
#Before
public void before() {
System.out.println("#Before");
}
#Test
public void test() {
System.out.println("#Test");
}
#After
public void after() {
System.out.println("#After");
}
#AfterClass
public static void afterClass() {
System.out.println("#AfterClass");
}
}
Output:
#Before
#Test
#After
#BeforeClass
#AfterClass
Process finished with exit code 0
I wanted to setup mongodb junit environment with flapdoodle and I got a java.io.IOException when it tries to download the mongodb archive.
I am using:
spring-data-mongodb: 1.6.1.RELEASE
de.flapdoodle.embed.mongo 1.47.3
org.springframework: 4.0.3.RELEASE
I am getting following error:
de.flapdoodle.embed.process.exceptions.DistributionException: java.io.IOException: Could not open inputStream for http://downloads.mongodb.org/win32/mongodb-win32-i386-3.0.2.zip
Caused by: java.net.UnknownHostException: downloads.mongodb.org
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
and also its pointing to mongodb-win32-i386-3.0.2.zip but I am using windows 64 bit.
here is my code
package com.bosch.test;
import junit.framework.TestCase;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.bosch.in.model.Device;
import com.bosch.in.service.imp.DeviceServiceImp;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
#ContextConfiguration(classes=ApplicationConfig.class,loader=AnnotationConfigContextLoader.class)
#RunWith(SpringJUnit4ClassRunner.class)
public class DeviceServiceTest2 {
private static final MongodStarter starter = MongodStarter
.getDefaultInstance();
private static MongodExecutable mongodExe;
private static MongodProcess mongod;
private static MongoClient mongo;
private DeviceServiceImp deviceServiceImp;
private MongoTemplate template;
#BeforeClass
public static void setUp() throws Exception {
//System.out.println("1");
mongodExe = starter.prepare(new MongodConfigBuilder()
.version(Version.Main.V3_0)
.net(new Net(12345, Network.localhostIsIPv6())).build());
System.out.println("2");
mongod = mongodExe.start();
System.out.println("3");
System.out.println("4");
mongo = new MongoClient("12345", 12345);
System.out.println("5");
}
#AfterClass
public static void tearDown() throws Exception {
mongod.stop();
mongodExe.stop();
}
public Mongo getMongo() {
return mongo;
}
#Test
public void save(){
System.out.println("1");
}
}
I think the problem is that you are using a default MongodStarter that is not aware of your proxy configuration (it was my case).
You just need to configure the mongodStarter.
Instead of using
private static final MongodStarter starter = MongodStarter
.getDefaultInstance();
you should have somthing like this in setUp()
Command command = Command.MongoD;
IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaults(command)
.artifactStore(new ArtifactStoreBuilder()
.defaults(command)
.download(new DownloadConfigBuilder()
.defaultsForCommand(command)
.proxyFactory(new HttpProxyFactory("proxy_host", 8080))))
.build();
MongodStarter starter = MongodStarter.getInstance(runtimeConfig);
This configuration is well explained on flapdoodle doc.
I use Eclipse with eCobertura
I have a little project with a Controller (SpringMVC).
I created a test (JUnit).
When I run the test from JUnit (in Eclipse IDE) all is right but when I run the command (from menu) I receive an error
My controller :
package ec.europa.eu.nwi.web.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* #author LEBRUJA
*/
#Controller
public class AvailibilityController {
/**
* #param request
* #return mav
*/
#RequestMapping(value = "/available")
public final ModelAndView available(final HttpServletRequest request) {
final ModelAndView mav = new ModelAndView("available", "sample",
new String("availability on 0.0.1"));
return mav;
}
}
My Test :
package ec.europa.eu.nwi.web.controller.test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import ec.europa.eu.nwi.web.controller.AvailibilityController;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:spring-servlet.xml"})
public final class AvalibilityControllerTest {
private transient MockHttpServletRequest request;
private transient MockHttpServletResponse response;
#Autowired
private RequestMappingHandlerAdapter handlerAdapter;
#Autowired
private RequestMappingHandlerMapping handlerMapping;
private static final Logger LOGGER = LoggerFactory.getLogger(AvalibilityControllerTest.class);
#Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
#After
public void tearDown() throws Exception {
LOGGER.debug("TearDown");
}
#Test
public void testAvailable() {
LOGGER.debug("Start testAvailable1");
LOGGER.debug("Test only availibility of the apps");
final AvailibilityController avc = new AvailibilityController();
final Object mav = avc.available(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(mav instanceof ModelAndView);
ModelAndViewAssert.assertAndReturnModelAttributeOfType((ModelAndView)mav, "sample", String.class);
ModelAndViewAssert.assertModelAttributeAvailable((ModelAndView)mav, "sample");
ModelAndViewAssert.assertModelAttributeValue((ModelAndView)mav, "sample", "availability on 0.0.1");
ModelAndViewAssert.assertViewName((ModelAndView)mav, "available");
final BindingResult result = mock(BindingResult.class);
when(result.hasErrors()).thenReturn(true);
LOGGER.debug("End testAvailable1");
}
#Test
public void testAvailable1() throws Exception {
LOGGER.debug("Start testAvailable");
LOGGER.debug("Test only availibility of the apps");
request.setMethod("GET");
request.setRequestURI("/available.html");
Object handler = handlerMapping.getHandler(request).getHandler();
LOGGER.debug("Get the Model and View");
ModelAndView modelAndView = handlerAdapter.handle(request, response,handler);
Assert.assertEquals("availability on 0.0.1", modelAndView.getModel().get("sample"));
Assert.assertTrue(modelAndView.getModel().containsKey("sample"));
LOGGER.debug("End testAvailable");
}
}
If I run with JUnit (Run As Junit), all is right but when I run Cover As .. JUnit I receive the error.
The error :
I filtered the class (from exclude configuration in Eclipse Coverage Configuration).
If I removed the filter, the junit code is marked in red
I don't understand the error.
Thanks a lot
I had
<aop:aspectj-autoproxy proxy-target-class="true" />
In my applicationContext.xml and it is running fine now
Thanks a lot