Why in NUnit when i write :
Assert.AreEqual(ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString , "Data Source=server511;Initial Catalog=FERTIGUNG;Persist Security Info=True");
it does not run the test and raises an error : Object reference not set to an instance of an object.
But ConfigurationManager is static class. So how can i run this test?
It is running the test - but the test is failing, because ConfigurationManager.ConnectionStrings["FertigungRead"] is returning null.
See this post about app.config files an NUnit, as that's where it'll be getting the configuration from.
However, I don't really see a test for a config file value as a valuable unit test... is this part of a more reasonable test?
Related
I posted this: Maven JUnit 5: Run all unit tests by default or run only a specific test suite from the command line .
I'm simply posting it again with almost exactly the same title because nothing about my base problem has changed, but I will include what I tried to do with Tags, which are not working for me.
We are upgrading our services to a relatively recent SpringBoot version, along with JUnit 5. We normally run our builds with just "mvn package", which runs all of our unit tests by default.
We also have "component tests", which are run from a separate command line, and which are all specified in a single test suite with a specific name. Before JUnit 5, we would run this with something like:
mvn -Dtest=ComponentTestSuite test
This was working fine with JUnit 4.
The class looks like this:
#Suite
#SelectClasses(<testclassname>.class)
public class ComponentTestSuite {
}
With JUnit 5, this ends up saying "No tests found".
We are using v2.3.12 of Spring-Boot, which by default includes a somewhat older version of JUnit 5. I am overriding those defaults and including v1.8.2 of the junit-platform components and v5.8.2 of the jupiter components.
For the service that I'm testing this with, the test suite only has a single component test (which is unusual). I WAS able to simply replace "ComponentTestSuite" with the name of the component test class, and that would run that single component test.
I noticed this thread: Junit5 test suites with #Suite annotation doesn't execute tests with mvn test command .
Unfortunately, what I found was that changing "-Dtest" to "-Dinclude" simply ran all my default unit tests and ignored the test suite.
So, I was told that I'm "not supposed to do it this way", although it's not clear to me exactly which parts I'm not supposed to do, but I am supposed to use "Tags". Ok, so I tried to use tags.
I also noticed this article: https://www.baeldung.com/junit-filtering-tests , although that article appears to use some deprecated mechanisms, like "#RunWith(JUnitPlatform.class)", so I'm not sure how much I can believe from that article.
I've been trying several variations, and my last attempt is this:
#Suite
#Tag("ComponentTest")
#IncludeTags("ComponentTest")
public class ComponentTestSuite {
}
And I added #Tag("ComponentTest") to my one component test class.
This still doesn't work. I try to run mvn -Dtest=ComponentTestSuite test, and it says "No tests found". If I instead try mvn -Dtests=ComponentTest test that just runs all of my unit tests and ignores my component test suite. If I instead run mvn test -Dgroups=ComponentTest, that gives me another variation of "No tests found" in that it doesn't print that, it just literally executes no tests without giving an error.
Looking at that suite class now, it does seem nonsensical, as it doesn't really "contain" anything anymore, which is unfortunate. It was good to have that list of component test classes in one place. Now, it seems like the suite class is pointless, but I can't get anything to work anyway.
Note that I haven't yet added a "groups" element to my surefire config, and I haven't edited all of my unit tests to add a #Tag("UnitTest") annotation. I've only edited the suite class and the one component test.
Update:
To address a comment, I am specifying the following dependencies (among others):
Version 1.8.2 of
junit-platform-suite
junit-platform-suite-api
junit-platform-launcher
junit-platform-commons
junit-platform-engine
junit-platform-runner
junit-platform-suite-engine
Version 5.8.2 of
junit-jupiter
junit-bom (probably unnecessary)
The entire suite class is this:
import org.junit.jupiter.api.Tag;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
#Suite
#Tag("ComponentTest")
#IncludeTags("ComponentTest")
public class ComponentTestSuite {
}
I wrote one test case for the controller. Inside that controller, calling one service class method.
When I Selected RunAsCoverage in the IntelliJ test case got passed. But it shows test coverage only for the controller class. For the service class, it's showing 0 percent.
When I run test coverage with the Jacoco plugin it's showing for both controller and service class
Please help with this. Thankyou
A simple login and logout selenium test, using pagefactory is created with TestNG and it works fine while running it as a "TestNG" test. I'm migrating it to Maven and came across issues in accessing variables between different classes.
src/main/java
---package:myapp.pages
Homepage.java
LoginPage.java
---package:utilities
CommonUtils.java
DatabaseUtils.java
src/test/java
---package:myapp.test
LoginTest.java (has static variable about test name)
public static String sTestName = "Regression test - Login/Logout"
While using the above variable in LoginPage.java, there is a compilation error. I'm using it as
public static String sName=myapp.test.LoginTest.sTestName;
The compilation error is:
[10,22] package myapp.test does not exist
How to handle this error? Any help would be highly appreciated.
src/main is where your production code lives, src/test is where your test code lives. The production code is to be shipped to the production environment, the test code is not. Hence, the production code should not depend on the test code. Maven enforces this for you (and your IDE will do so accordingly). There should never be a case where your production code needs to depend on something that's only known to the testing code. Now, there are valid scenarios in which you want behavior to be different during tests than it is for production code, and there is many strategies to do so. For example you can use a properties file (and have one with the same name in src/test/resources "magically" override the production one during testing), or use setters to override things just during testing.
I have a #SpringBootTest which is used to execute an integration test on the server. Depending on the configuration I want the server to behave differently. The configuration itself is read by beans (scope = singleton) deep inside my app logic and they read the property via #Value annotation.
How could I execute the same test with different configuration settings? I have tried to write different test classes and annotate them with #TestPropertySource(properties = XYZ). But it seems that this affects all other tests as well (due to the singleton scope?). It there a way to reset the properties after the test?
To respecify my problem: I want to configure my bean with a different #Value property during my tests and this value should only be valid throughout this specific test execution.
Thanks ahead for any pointers.
I have a webservice, which is connecting to the client of other webservice by using property from the config. As in any organization, we have different environments. For testing, I wanted to hit testing env instead of local. This is how I override the default property value only for integration test. By doing this, I can hit the test env instead of default local env.
#SpringBootTest(value = {"eureka.client.enabled=false", // Don't start Eureka
"com.somepackage.webservicename.client.serviceUrl = http://nodename.envname:26730"})
Hope this helps!
I am using Emma for code-coverage for my project. In my JUnit test case, I am calling a method but in code coverage report, it shows no coverage for that method which is getting called from testCase.
Check these points:
Set a breakpoint in the method to make sure it is really called.
You can tell emma to omit some classes. Check the config.
Is the class properly instrumented?