I have been working on a Java Application [That uses Servlets, etc]. And I was hoping to run JUnit Testing for my Controller methods. However, after adding the libraries of JUnit 4.12 and Hamcrest 1.3 & generating the test cases using NetBeans on my controllers. I have faced this problem when I ran the test.
Testsuite: controller.AccountControllerTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
Testcase: controller.AccountControllerTest:BeforeFirstTest: Caused an ERROR
null
junit.framework.AssertionFailedError
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
Test controller.AccountControllerTest FAILED (crashed)
I have not been able to find anything online that is related to this issue. Could anybody help me out here? Thank you!
EDIT: Apologies as this is my first time asking a question. Thanks for taking a look!
CODE:
public class AccountControllerTest {
#BeforeClass
public static void setUpClass() {
}
#AfterClass
public static void tearDownClass() {
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
/**
* Test of register method, of class AccountController.
*/
#Test
public void testRegister() {
System.out.println("register");
String username = "";
String password = "";
String cfmPassword = "";
String email = "";
String name = "";
ArrayList<String> errorList = null;
AccountController instance = new AccountController();
instance.register(username, password, cfmPassword, email, name, errorList);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
}
Error Stacktrace:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
junit.framework.TestResult.addListener(Ljunit/framework/TestListener;)V
at junit.framework.TestResult.addListener(Native Method) at
org.apache.tools.ant.taskdefs.optional.junit.IgnoredTestResult.addListener(IgnoredTestResult.java:49)
at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:357)
at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1179)
at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1030)
Related
I am trying to add ExtentReport in my test automation project, but IntelliJ is throwing this error at line "report = ..." inside "BeforeMethod"
Expected 0 arguments but found 1
I am trying to insert the location of the report file. Help much appreciated. Thanks.
public class Methods {
public static WebDriver driver;
public static ExtentReports report;
public static ExtentTest test;
#BeforeTest
public void startReport(){
report = new ExtentReports(System.getProperty("C:\\Program Files (J)\\VCWebMaine\\Report.html"));
}
#BeforeMethod
public void setDriver() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (J)\\VCWebMaine\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://xxxxxxxxxxxxxxxx");
Thread.sleep(2000);
}
I tried looking for answers in Google. It looks like the same lines work in Eclipse. Is it a problem in the IDE?
Other part of the script as below:
public class TestCases extends Methods {
ExtentTest test = Methods.test;
#Test
public void scenario01() throws InterruptedException, IOException {
I'm new with all of that, so the answer should be obvious but I can't get it by myself :op
I'm working on a simple Spring Boot application and I'm "trying" to set up some JUnit test.
In my controller I've this code :
#Controller
public class UserController {
#Autowired
private OrderInfoService orderInfoService;
#RequestMapping(value = "/single", method = RequestMethod.GET)
public ResponseEntity<List<OrderInfo>> orderinfo() {
List<OrderInfo> orderInfo = orderInfoService.getOrderInfo("ca1121a");
System.out.println("Created output string :" + orderInfo.toString());
return new ResponseEntity<List<OrderInfo>>(orderInfo, HttpStatus.OK);
}
}
This is only displaying a test page at "/single". The content of "orderInfo" appear both in the command line and on my web page. Good!
Now I'm trying to setup a JUnit test like this :
public class OrderInfoServiceImplTest {
// Call class under test
private OrderInfoService orderInfoService;
#Test
public void testGetOrderInfo() {
System.out.println("Test - getOrderInfo");
String res = "[OrderInfo :typeNameca1121a - retroFit ]" ;
List<OrderInfo> orderInfo = orderInfoService.getOrderInfo("ca1121a");
System.out.println("Created output orderInfo");
System.out.println("\t" + orderInfo.size());
Assert.assertEquals(res, orderInfo.get(0).toString());
}
}
This give me a null pointer exception :
[INFO] Running OrderInfoServiceImplTest Test - getOrderInfo [ERROR]
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.178
s <<< FAILURE! - in OrderInfoServiceImplTest
[ERROR]
testGetOrderInfo(OrderInfoServiceImplTest)
Time elapsed: 0.115 s <<< ERROR! java.lang.NullPointerException
at com.devglan.service.impl.OrderInfoServiceImplTest.testGetOrderInfo(OrderInfoServiceImplTest.java:23)
I don't understand why my object is well defined in the controller but not in the test, I use exactly the same command. Does it related to the #autowired in the controller (which I have to say I don't understand yet)?
Any help would be appreciate.
Thank you
Some annotations, which make your test case class be run under the control of spring test framework and your object under test is auto-wired to your test case class,are missed.
As for your example , the following code will work.
#RunWith(SpringRunner.class)
#SpringBootTest
public class OrderInfoServiceImplTest {
// autowire class under test
#Autowired
private OrderInfoService orderInfoService;
#Test
public void testGetOrderInfo() {
System.out.println("Test - getOrderInfo");
String res = "[OrderInfo :typeNameca1121a - retroFit ]" ;
List<OrderInfo> orderInfo = orderInfoService.getOrderInfo("ca1121a");
System.out.println("Created output orderInfo");
System.out.println("\t" + orderInfo.size());
Assert.assertEquals(res, orderInfo.get(0).toString());
}
}
I developed a JAVA (JDK1.6) application to manage PDF file with iText (v5.5.0).
After I wrote test application using groovy, but when i create a PdfReader object, in my test case,
PdfReader pdfReader = new PdfReader("/my/path/project/test.pdf")
I obtain the following error:
java.lang.NoClassDefFoundError: org/bouncycastle/cms/RecipientId
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2484)
...
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.cms.RecipientId
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
The first asserts in groovy test class works fine.
I created the same test class with JUnit4, and all works fine!
How can I fix the error in groovy test class?
I don't use bouncycastle class, why have I that ClassNotFound Exception?
EDIT:
GroovyTestCase
class PdfMergeItextTest extends GroovyTestCase {
PdfMergeItext pdfMerge
void setUp() {
super.setUp();
println "Test class [PdfMergeItext] avviato..."
pdfMerge = new PdfMergeItext()
pdfMerge.openOutputPdf("/my/path/project/output.pdf")
}
void tearDown() {
println "Test class [PdfMergeItext] END."
}
#Test
void testMergeSinglePdfFile() {
println "Test one Pdf.."
File outputPdf = new File("/my/path/project/output.pdf")
assertTrue outputPdf.exists()
PdfReader pdfReader = new PdfReader("/my/path/project/test.pdf")
pdfMerge.addPdf(pdfReader)
pdfMerge.flush()
pdfMerge.close()
assert outputPdf.size() > 0
println "File size: ${outputPdf.size()}"
println "End test one Pdf"
}
}
JUnit4 TestCase
public class PdfMergeItextUnitTest {
PdfMergeItext pdfMergeItext = null;
#Before
public void setUp() throws Exception {
System.out.println("Start..");
this.pdfMergeItext = new PdfMergeItext();
this.pdfMergeItext.openOutputPdf("/my/path/project/output.pdf");
}
#After
public void tearDown() throws Exception {
System.out.println("END!");
}
#Test
public void testMergePdfFile() throws IOException, BadPdfFormatException {
File outputPdf = new File("/my/path/project/output.pdf");
Assert.assertTrue(outputPdf.exists());
PdfReader pdfReader = new PdfReader("/my/path/project/test.pdf");
this.pdfMergeItext.addPdf(pdfReader);
this.pdfMergeItext.flush();
this.pdfMergeItext.close();
Assert.assertTrue(outputPdf.size() > 0);
}
}
Thanks
The BounceCastle JARs are optional, see the pom.xml (search for dependency). So, if you didn't include them in your pom.xml or build.gradle , you won't get them (anyway, what build tool are you using)?
So if you are using dependency management I understand why it is not there. The real question is: why is it needed in the first test and not in the second test?
Please make the tests totally equalent (you are calling addPdf once vs. twice and you are calling length vs. size
Since BounceCastle is used for decryption, are you testing with the same PDF file?
Does Jmeter supports Junit testsuite?
This question trouble me for several days, the test cases all working well no matter a style of junit 3 or 4. But the testsuite is anyway dumb.
Any suggestions?
My code below:
public class LoginLogout extends TestCase {
private static Logger log = Logger.getLogger(LoginLogout.class);
public static Test suite() {
try{
log.info("test suite start!");
TestSuite suite = new TestSuite(LoginLogout.class.getName());
//$JUnit-BEGIN$
suite.addTestSuite(Login.class);
suite.addTestSuite(Logout.class);
return new TestSetup(suite) {
protected void setUp(){
log.info("test suite setup!");
}
protected void tearDown(){
log.info("test suite finished!");
}
};
}catch(Exception e){
log.error(e.getMessage());
}
return null;
}
}
public class Login extends TestCase {
private static Logger log = Logger.getLogger(Login.class);
#Test
public void testLogin() throws Exception {
log.info("login start!");
log.info("login end!");
}
}
public class Logout extends TestCase {
private static Logger log = Logger.getLogger(Logout.class);
#Test
public void testLogout() throws Exception {
log.info("logout start!");
log.info("logout end!");
}
}
You can go to "Download Apache JMeter" page on http://jmeter.apache.org/ , and download the "apache-jmeter-2.8_src.zip" (or whatever the current version is).
After unzipping it, under apache-jmeter-2.8_src\apache-jmeter-2.8\src\junit\test directory, you can find the following java files (as for jmeter version 2.8):
For JUnit4:
AfterAnnotatedTest.java
BeforeAnnotatedTest.java
DummyAnnotatedTest.java
Junit4AnnotationsTest.java
For JUnit3:
RerunTest.java
SetupTestError.java
SetupTestFail.java
TearDownTestFail.java
You can see them shown up at the Classname dropdown menu on JUnit Request of JMeter (Test Plan --> Thread Group --> JUnit Request).
Those JUnit test cases are provided by JMeter by default, so I assume that a simple copy-and-paste of their code and work from there should work; however, so far, I am not able to see my test cases shown up at the Classname dropdown menu.
Here are other useful links I have found; however, none of them solves the current problem I am encountering:
Running Selenium scripts from Jmeter
http://jmeter.apache.org/usermanual/junitsampler_tutorial.pdf
JUnit test classes not showing up in JMeter
For JUnit4 the Suite would be:
#RunWith(Suite.class)
#SuiteClasses({Login.class, Logout.class})
public class LoginLogout {
private static Logger log = Logger.getLogger(LoginLogout.class.getName());
}
And the TestClass is:
public class Login {
private static Logger log = Logger.getLogger(Login.class.getName());
#Test
public void testLogin() throws Exception {
log.info("login start!");
log.info("login end!");
}
}
Worked fine for me
I'm trying to run Selenium RC 1.0.3 using Java 6, JUnit 4, and Eclipse on Snow Leopard.
Here is my test class, from the Selenium docs:
public class TestCase extends SeleneseTestCase {
#Before
public void before() throws Exception {
setUp("http://www.google.com/", "*firefox");
}
#Test
public void test() {
selenium.open("/");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Advanced search"));
}
}
I receive the following error, which occurs at the time that selenium.open() is called:
11:16:59.916 INFO - Got result:
XHR ERROR: URL = http://localhost:4444/ Response_Code = 403
Error_Message = Forbidden+for+Proxy on session a8cf1e0bd5ed42c5a4df0c25ec5f5286
I've tried (finding various suggestions on the web) replacing *firefox with *chrome or *firefox, replacing http with https and adding selenium.start(), but none have helped, or even changed the behavior.
Any ideas?
EDIT: The selenium-server is running, and the local firewall is disabled.
OK, here's a solution, without any understanding: If the #Before method is removed, and the call to setUp() is moved into the #Test method, then it works:
#Test
public void test() throws Exception {
setUp("http://www.google.com/", "*chrome");
selenium.open("/");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Advanced search"));
}
But here is a better solution, based on understanding:
import com.thoughtworks.selenium.SeleneseTestCase;
public class TestCase extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com/", "*firefox");
}
public void testAuto() throws Exception {
selenium.open("/");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Advanced search"));
}
}
It turns out that SeleneseTestCase extends TestCase from JUnit 3. I had upgraded the documentation example to JUnit 4 without thinking about what problems may be caused.