Arquillian test not failing - maven

When dependency injection in Arquillian test class fails, maven still claims that all tests are run and are passed. This is major issue if we have hundreds of tests and some tests show as Green in Jenkins, but in reality they are not driven at all.
Is there a way to make the above test fail in Jenkins/Maven? One can use any Interface long as there is no implementation added to deployment.
#ExtendWith(ArquillianExtension.class)
class FailingArquillianTestIssueIT {
#Inject
DummyInterface dummyInterface;
#Deployment(testable = true)
public static JavaArchive create() throws Exception {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class).addAsManifestResource(EmptyAsset.INSTANCE,
"beans.xml");
return archive;
}
#Test
void test1() {
assertTrue(false);
}
}

Related

Jacoco missing coverage when test is created

I have a project with spring-boot 2, JUnit 4, jacoco 0.8.6 and I use maven to run unit test, I have the following class:
#Component
public class AccountVOValidator implements FormValidator<AccountVO> {
#Override
public void validate(AccountVO object, Object... params) throws GenericException {
//logic
}
}
This implements FormValidator interface, it looks like this:
public interface FormValidator<T> {
public void validate(T object, Object... params) throws GenericException;
}
I already created the test class for that, it looks like this:
#RunWith(MockitoJUnitRunner.class)
public class AccountVOValidatorTest {
private TestUtils testUtils = new TestUtils();
#InjectMocks
private AccountVOValidator accountVOValidator;
#Test
public void shouldValidateOk() throws GenericException {
accountVOValidator.validate(accountVO, transaction);
}
}
I have several unit test for different packages and jacoco report looks like this:
enter image description here
When I run "mvn clean test", AccountVOValidatorTest ran without problems and all tests passed successfully.
All classes that implements FormValidator interface have a missing coverage in jacoco report, I implements interfaces in my services classes and they got coverage in report, do you have an idea what it happens?
I am not sure why all classes that implements FormValidator interface don't get coverage in jacoco report.

Launch application before tests on condition

I have rest-assured tests marked with #SpringBootTest (which allows to start application before executing tests). But I have two use cases:
Run tests locally (current setup is ok for that).
Run same tests on already running host (not ok, as application is started before executing tests).
Is there any way to sometimes ignore #SpringBootTest or somehow turn off application initialization?
Adding minimal example:
#RunWith(SpringRunner.class)
//In case service.host provided: no need to run application
#SpringBootTest
public class RestTest {
#Value("service.host")
private String host;
#Before
public void setUp() {
if (StringUtils.isNotEmpty(host)) {
RestAssured.baseURI = host;
}
}
#Test
public void test() {
RestAssured.get("/test").then().assertThat().statusCode(HttpStatus.OK.value());
}
}

How to call a TESTNG class from a Springboot based selenium project

Running Testng class from Springboot application gives an error
I have a Springboot based Selenium Testng project.
When I do a Maven configuration like mvn clean package -Dtests= testng is file name of xml file, it works seemlessly.
Now I have a scenario where I need to get the testsuite name which user likes to test on commandline and use that and run the test suite.
I have the following in the #SpringBootApplication main method. Assuming I get the test suite thru commandline, I have this code, but the springboot servers keeps running in infinite without running the tests.
How could I fix it.
#SpringBootApplication
public class MyAutomationApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(MyAutomationApplication.class, args);
}
public void run(String... args) throws Exception {
System.out.println("Welcome!!");
//assuming i get the InitialLoginTest from commandline, i am passing the CLASS NAME to classes.add(new XmlClass("InitialLoginTest"));
TestNG tng = new TestNG();
XmlSuite suite = new XmlSuite();
suite.setName("Appium Test suite");
XmlTest test = new XmlTest(suite);
test.setName("Sample Test");
List<XmlClass> classes = new ArrayList();
classes.add(new XmlClass("InitialLoginTest"));
test.setXmlClasses(classes);
List<XmlSuite> suites = new ArrayList();
suites.add(suite);
tng.setXmlSuites(suites);
tng.run();
}
}
#SpringBootTest
public class InitialLoginTest {
#Test
void loading() {
System.out.println("In InitialLoginTest!!");
}
}

Is it possible to fail fast Spring tests if the application cannot start?

I have this base test class in one Spring Boot module:
#ActiveProfiles("test")
#SpringBootTest(classes = {WebServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class BaseWebServiceTest {
//...
}
If for some reason the application cannot start (in my case, if localstack docker image is not started of if Spring Cloud Contract stubs are not available, etc) the tests are still being run and obviously they all fail. Is there any way of skipping all tests if ApplicationContext is not loaded?
Is there any way of skipping all tests if ApplicationContext is not loaded?
No. There is no way to skip tests automatically if the ApplicationContext does not load.
However, you could make use of JUnit 4's assumption support and abort the execution of the tests based on some boolean condition that you choose. For example, if you can check whether the docker image started, you could then do something similar to the following.
public static boolean dockerImagedStarted() {
// return true if the Docker image started...
}
#BeforeClass
public static void ensureDockerImageStarted() {
org.junit.Assume.assumeTrue(dockerImagedStarted());
}
p.s. please note that there is an open JIRA issue requesting a built-in feature to avoid repeated attempts to load an ApplicationContext. See SPR-9548 for details.
You can create a runner that will ignore your tests in case that the ApplicationContext is not loaded
public class CustomRunner extends BlockJUnit4ClassRunner {
public CustomRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
#Override
protected boolean isIgnored(FrameworkMethod child) {
return shouldIgnore() || super.isIgnored(child);
}
/**
*
* #return if your test should be ignored or not
*/
private boolean shouldIgnore() {
// Some check if your docker is up
return true;
}
}
And the use #RunWith with the CustomRunner you have created
#RunWith(CustomRunner.class)
#ActiveProfiles("test")
#SpringBootTest(classes = {WebServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class BaseWebServiceTest {
//...
}

JUnit Test cases failing while running using Maven but works with Eclipse

I have a Junit test case which doesn't work if I run using Maven. But the same test case works when I run using Eclipse.
My Junit class is like this.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:/test-config.xml"} )
public class TestDaoImpl {
private final static Logger logger = Logger.getLogger(TestDaoImpl.class);
#Autowired
private MyDaoImpl myDao;
#Test
public void testMyDao() throws Exception {
logger.info("Called testMyDao()================");
// here myDao is null and throwing NullPointerException in sunfire log.
// But this works when I run using Eclipse.
List<MyObj> objList = myDao.getList();
}
#Test
public void testMyCode() throws Exception {
logger.info("Called testMyCode()================");
// this test case works with Maven
List<MyObj> objList = MyClass.getList();
}
}
The sunfire plugin was missing. When added it started working.

Resources