I have 3 tests defined inside a class
#Test
public void test1(){
enter userID
}
#Test
public void test2(){
enter password
}
#Test
public void test3(){
click loginButton
}
But tests starts executing from test3 clicks loginButton first rather than in an order.
In TestNG the ordering of methods in the class file is unpredictable, so you need to either use dependencies[dependsOnMethods or priority] or include your methods explicitly in XML[preserver-order=true in your testng.xml].
#Test //( priority = 1 )
public void test1(){
enter userID
}
#Test(dependsOnMethods="test1") //( priority = 2 )
public void test2(){
enter password
}
#Test(dependsOnMethods="test2") //( priority = 3 )
public void test3(){
click loginButton
}
Related
Test class :
#isTest
public class PriceName_PriceList_test {
#istest static void PriceName_PriceListMethod(){
PriceName_PriceList priceObj = new PriceName_PriceList();
Product_Line_Item__c prl = New Product_Line_Item__c();
prl = [SELECT Product_Name__r.name, List_Price__c FROM Product_Line_Item__c];
prl.Product_Name__r.name = 'testproduct';
prl.List_Price__c = 123;
insert prl;
PriceObj.getdetails();
}
}
You didn't pass the Id to the class. So with a null the query will return 0. But it's interesting, I'd expect you to at least get coverage for line 6...
Change your code to this and run again
insert prl;
PriceObj.drId = prl.Id;
PriceObj.getdetails();
Or make the function accept an Id parameter and call PriceObj.getdetails(prl.Id), no idea what's your use case
Salesforce Test Class - Facing - System.NullPointerException: Attempt to de-reference a null object Error while trying to Run Test Class
APEX class
public class NewAccountCreation {
public Account account{get;set;}
public void save(){
Account acc = new Account();
// User enter values in vf page and we are capturing and creating account
acc.name = account.Name;
//acc.address = account.adress;
Insert acc;
}
}
Test Class
#isTest
public class TestNewAccountCreation {
#isTest static void TestNewAccountCreationMethod(){
NewAccountCreation testAccount = new NewAccountCreation ();
Account acc = new Account(Name='TestAcct');
Insert acc;
system.debug(''+acc);
testAccount.save();
System.assert([Select Id From Account].size()==1);
}
}
Error:
System.NullPointerException: Attempt to de-reference a null object
StackTrace:
Class.NewAccountCreation.save: line 6, column 1
Class.TestNewAccountCreation.TestNewAccountCreationMethod: line 9, column 1
You never set testAccount.account, so it's null. You do need to populate this variable, and you do not need to create and insert an Account in your test code. In fact, doing so will cause your assertion to fail.
I'm relatively new to connection pooling, but from what I've read it seems ideal to leave some connections idle for faster performance.
I'm currently using JDBI, and after idle periods I'll get a
com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
I would assume this is either because my database configuration settings are lacking or that I must be using the framework incorrectly:
config.yml:
database:
# whether or not idle connections should be validated
checkConnectionWhileIdle: false
# the maximum amount of time to wait on an empty pool before throwing an exception
maxWaitForConnection: 10s
# Limits for simultaneous connections to DB
minSize: 10
initialSize: 10
maxSize: 100
DAOs:
public class AccountDAO {
private final Jdbi jdbi;
public AccountDAO(Jdbi jdbi) {
this.jdbi = jdbi;
}
public void addAccount(String id) {
jdbi.useHandle(h ->
h.createUpdate("INSERT INTO Account(id) values (:id)")
.bind("id", id)
.execute());
}
}
public class RestaurantDAO {
private final Jdbi jdbi;
public RestaurantDAO(Jdbi jdbi) {
this.jdbi = jdbi;
}
public Optional<RestaurantDTO> getRestaurantByName(String restName) {
return jdbi.withHandle(h ->
h.createQuery("SELECT * FROM Restaurant WHERE restName =:restName")
.bind("restName", restName)
.mapToBean(RestaurantDTO.class)
.findOne());
}
public void addRestaurant(String restName) {
jdbi.useHandle(h ->
h.createUpdate("INSERT INTO Restaurant(restName) values (:restName)")
.bind("restName", restName)
.execute()
);
}
}
public class ReviewDAO(Jdbi jdbi) {
this.jdbi = jdbi;
}
public Optional<ReviewDTO> getReviewByAuthorAndRestaurant(String author, String restName) {
return jdbi.withHandle(h ->
h.createQuery("SELECT * FROM Review WHERE author=:author AND restName =:restName")
.bind("author", author)
.bind("restName", restName)
.mapToBean(ReviewDTO.class)
.findOne());
}
public List<ReviewDTO> getReviewsByAuthor(String author) {
return jdbi.withHandle(h ->
h.createQuery("SELECT * FROM Review WHERE author =:author ORDER BY created DESC")
.bind("author", author)
.mapToBean(ReviewDTO.class)
.list());
}
public List<ReviewDTO> getReviewsByRestaurant(String restName) {
return jdbi.withHandle(h ->
h.createQuery("SELECT * FROM Review WHERE restName =:restName ORDER BY created DESC")
.bind("restName", restName)
.mapToBean(ReviewDTO.class)
.list());
}
public List<ReviewDTO> getRecentReviews() {
return jdbi.withHandle(h ->
h.createQuery("SELECT top 5 * FROM Review ORDER BY created DESC")
.mapToBean(ReviewDTO.class)
.list());
}
public void addReview(String author, String restName, String title, String bodyText, int rating) {
jdbi.useHandle(h ->
h.createUpdate("INSERT INTO Review(bodyText, rating, restName, author, title) values (:bodyText, :rating, :restName, :author, :title)")
.bind("bodyText", bodyText)
.bind("rating", rating)
.bind("restName", restName)
.bind("author", author)
.bind("title", title)
.execute());
}
public void updateReview(String author, String restName, String title, String bodyText, int rating) {
jdbi.useHandle(h ->
h.createUpdate("UPDATE Review SET bodyText=:bodyText, rating=:rating, title=:title where author=:author AND restName=:restName")
.bind("bodyText", bodyText)
.bind("rating", rating)
.bind("title", title)
.bind("author", author)
.bind("restName", restName)
.execute());
}
public void deleteReview(String author, String restName) {
jdbi.useHandle(h ->
h.createUpdate("DELETE FROM Review WHERE author=:author AND restName=:restName")
.bind("author", author)
.bind("restName", restName)
.execute());
}
}
Using the setting
checkConnectionOnBorrow: true
Might work, but I would assume that the ideal solution would be to prevent my initial connections from being closed in the first place?
Any assistance is appreciated
It turns out my DB host, Azure, automatically closes idle connections after 30 minutes. For the time being, I've added aggressive validation settings to my config to renew the pool accordingly. Probably just gonna switch hosts since it doesn't look like you can configure the timeout on Azure's end.
validationQuery: "/* APIService Health Check */ SELECT 1"
validationQueryTimeout: 3s
checkConnectionWhileIdle: true
minIdleTime: 25m
evictionInterval: 5s
validationInterval: 1m
I know it is not a good thing write a unit test for mapper or get set but, it is what it is, so I am stucked how to do unit test for mappers;
StudentGroupList below;
#Getter
#Setter
public class StudentGroupList {
private String studentId;
}
StudentGroupListRowMapper below;
public class StudentGroupListRowMapper implements RowMapper<StudentGroupList> {
#Override
public StudentGroupList mapRow(Resultset rs, int rowNum) throws SQLException {
StudentGroupList studentGroupList = new StudentGroupList();
studentGroupList.setStudentId(rs.getString("student_id"));
return studentGroupList;
}
}
I have tried below, but jococo coverage test did not coverage anything
public class TaskGroupListRowMapperTest {
private ResultSet resultSet;
private StudentGroupList studentGroupList;
#Before
public void setUp() {
resultSet = mock(ResultSet.class);
studentGroupList = mock(StudentGroupList.class);
}
#Test
public void testStudentGroupListMapper() throws SQLException {
when(resultSet.getString("student_id"))
.thenReturn(studentGroupList.getStudentID());
assertTrue(studentGroupList.getStudentId(), true);
}
}
İt says exception; thenReturn() may be missing.
Take it easy, we were all there in the past and try to understand what a unit test is supposed to do.
You don't unit test everything just for the sake of unit test coverage. When you have a framework callback like RowMapper, it is one of those cases. Your StudentGroupListRowMapper is very simple, so an integration test for your Dao will cover it. Anyway, you want to unit test so just think of it as a simple class and let's go through the steps.
You want to create the instance of the class you want to test and also provide mock dependencies for any services it calls. Luckily your StudentGroupListRowMapper does not call anything. Since the method you want to test is StudentGroupList mapRow(Resultset rs, int rowNum), you have to decide if you can provide a Resultset and a rowNum. Since Resultset is not something you create, your provide a mock for that
Resultset inputRs = mock(Resultset.class);
int rowNum = 1;
Now when your method executes, it is going call inputRs.getString("student_id") to get student id but it is mock and it doesn't know what to return so you have to tell your mock what to do when inputRs.getString("student_id") is called
when(inputRs.getString("student_id")).thenReturn("sutdent-id-1");
Now you know your expected StudentGroupList should be created with "sutdent-id-1" and should be returned from the method.
assertEquals(resultedStudentGroupList.getStudentId(),"sutdent-id-1");
Lets combine all of them together.
public class StudentGroupListRowMapperTest {
StudentGroupListRowMapper mapper = new StudentGroupListRowMapper();
#Test
public void testMapRow() {
Resultset inputRs = mock(Resultset.class);
int rowNum = 1;
when(inputRs.getString("student_id")).thenReturn("sutdent-id-1");
StudentGroupList result = mapper.mapRow(inputRs, rowNum);
assertEquals(result.getStudentId(), "sutdent-id-1");
}
}
I have two .xaml pages LoginPage and child page - Workloads_New . I need to pass LoginID from LoginPage to Workloads_New. But in Workloads_New I keep getting LoginID value 0. Here is my code in LoginPage:
void webService_GetUserIDCompleted(object sender, GetUserIDCompletedEventArgs e)
{
int ID = e.Result; //for example i get ID=2
if (ID > 0)
{
this.Content = new MainPage();
Workloads_New Child = new Workloads_New();
Child.LoginID = ID; //In debug mode i see that ID=2 and LoginID=2
}
}
and in Workloads_New I have:
public int LoginID { get; set; }
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
//to test i just want to see that id in textblock but i keep getting LoginID=0 why?
this.ErrorBlock.Text = this.LoginID.ToString();
}
The UriMapper object also supports URIs that take query-string arguments. For example, consider
the following mapping:
In XAML :
<navigation:UriMapping Uri="Products/{id}"
MappedUri="/Views/ProductPage.xaml?id={id}"></navigation:UriMapping>
In C# you can also see this
consider the following code, which embeds two numbers into a URI as
query-string arguments:
string uriText = String.Format("/Product.xaml?productID={0}&type={1}",productID, productType);
mainFrame.Navigate(new Uri(uriText), UriKind.Relative);
A typical completed URI might look something like this:
/Product.xaml?productID=402&type=12
You can retrieve the product ID information in the destination page with code like this:
int productID, type;
if (this.NavigationContext.QueryString.ContainsKey("productID"))
productID = Int32.Parse(this.NavigationContext.QueryString["productID"]);
if (this.NavigationContext.QueryString.ContainsKey("type"))
type = Int32.Parse(this.NavigationContext.QueryString["type"]);
I found answer.
In App.xaml.cs
public int LoginID { get; set; }
In LoginPage.xaml.cs where I set LoginID value I wrote
((App)App.Current).LoginID = ID;
In Workloads_New.xaml.cs where I use LoginID I wrote
this.ErrorBlock.Text = ((App)App.Current).LoginID.ToString();